Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adicionando busca automática de termos no Google Trends e generalizando a função de user input #21

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,6 @@ typings/

# credentials
credentials/*.json

# webstorm ide files
.idea
51 changes: 32 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
const readline = require('readline-sync')
const prompts = require('prompts')
const robots = {
text: require('./robots/text.js')
}
const trends = require('./searchHotTrends');

async function askAndReturnAnswers() {
const questions = [
{
type: 'select',
name: 'searchTerm',
message: 'Choose one search term:',
choices: await trends.searchHotTrends(count=9),
validate: value => typeof value === 'string' ? value.trim() !== '' : false,
},
{
type: 'select',
name: 'prefix',
message: 'Choose one option:',
choices: ['Who is', 'What is', 'The history of'],
validate: value => typeof value === 'string' ? value.trim() !== '' : false,
}
];

return new Promise(async (resolve, reject) => {
const promptOptions = {
onCancel: () => reject(new Error('The user has stopped answering'))
}
const response = await prompts(questions, promptOptions)
resolve(response)
});
}

async function start() {
const content = {}

content.searchTerm = askAndReturnSearchTerm()
content.prefix = askAndReturnPrefix()
const content = await askAndReturnAnswers()

await robots.text(content)

function askAndReturnSearchTerm() {
return readline.question('Type a Wikipedia search term: ')
}

function askAndReturnPrefix() {
const prefixes = ['Who is', 'What is', 'The history of']
const selectedPrefixIndex = readline.keyInSelect(prefixes, 'Choose one option: ')
const selectedPrefixText = prefixes[selectedPrefixIndex]

return selectedPrefixText
}

console.log(content)
console.log(content);
}

start()
start();
24 changes: 19 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"homepage": "https://github.com/filipedeschamps/video-maker#readme",
"dependencies": {
"algorithmia": "^0.3.10",
"readline-sync": "^1.4.9",
"prompts": "^2.0.3",
"sbd": "^1.0.14"
}
}
4 changes: 2 additions & 2 deletions robots/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ async function robot(content) {
async function fetchContentFromWikipedia(content) {
const algorithmiaAuthenticated = algorithmia(algorithmiaApiKey)
const wikipediaAlgorithm = algorithmiaAuthenticated.algo('web/WikipediaParser/0.1.2')
const wikipediaResponde = await wikipediaAlgorithm.pipe(content.searchTerm)
const wikipediaContent = wikipediaResponde.get()
const wikipediaResponse = await wikipediaAlgorithm.pipe(content.searchTerm)
const wikipediaContent = wikipediaResponse.get()

content.sourceContentOriginal = wikipediaContent.content
}
Expand Down
21 changes: 21 additions & 0 deletions searchHotTrends.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const googleTrends = require('google-trends-api');

module.exports = {
searchHotTrends: function (count) {
const trendsSettings = {
trendDate: new Date(),
geo: 'BR',
hl: "pt-BR"
};

return googleTrends.realTimeTrends(trendsSettings)
Pacheco95 marked this conversation as resolved.
Show resolved Hide resolved
.then(JSON.parse)
.then(parsedResult => parsedResult.storySummaries.trendingStories)
.then(hotTrendStories => hotTrendStories.map(story => story['entityNames']))
.then(hotTrends => [].concat.apply([], hotTrends).slice(0, count))
.catch(error => {
console.error('An error occurs: ', error);
return [];
});
}
};