Skip to content

Commit 5e9964e

Browse files
authored
Merge pull request #8 from kikiklang/random
Random
2 parents 3c81d13 + b65bafa commit 5e9964e

File tree

8 files changed

+81
-14
lines changed

8 files changed

+81
-14
lines changed

cli.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const cli = meow(help, {
2828
clear: {
2929
type: 'boolean',
3030
alias: 'c'
31+
},
32+
random: {
33+
type: 'boolean',
34+
alias: 'r'
3135
}
3236
}
3337
})

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ const wikipediaCLI = flags => {
1616
return wiclipedia.clearHistory()
1717
}
1818

19+
if (flags.random) {
20+
return wiclipedia.displayRandomArticlesList()
21+
}
22+
1923
return wiclipedia.launchProgram()
2024
}
2125

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wiclipedia",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "wikipedia articles summaries in your terminal",
55
"repository": "kikiklang/wiclipedia",
66
"homepage": "https://github.com/kikiklang/wiclipedia",

src/fetch-data.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const fetch = require('node-fetch')
77
const ora = require('ora')
88
const wtf = require('wtf_wikipedia')
99

10-
const getSuggestedTopic = async (userInput, lang) => {
10+
exports.getSuggestedTopic = async (userInput, lang) => {
1111
const domain = `https://${lang}.wikipedia.org`
1212
const path = '/w/api.php'
1313
const action = 'query'
@@ -27,12 +27,37 @@ const getSuggestedTopic = async (userInput, lang) => {
2727

2828
return result
2929
} catch (error) {
30-
console.log('sorry, there is a problem with that request')
30+
console.log('sorry, there is a problem with that request', error)
3131
process.exit(1)
3232
}
3333
}
3434

35-
const getArticle = async (userPick, lang) => {
35+
exports.getRandomSuggestions = async lang => {
36+
const domain = `https://${lang}.wikipedia.org`
37+
const path = '/w/api.php'
38+
const action = 'query'
39+
const format = 'json'
40+
const list = 'random'
41+
const rnlimit = 10
42+
const rnnamespace = 0
43+
const endpoint = `${domain}${path}?action=${action}&format=${format}&list=${list}&rnlimit=${rnlimit}&rnnamespace=${rnnamespace}`
44+
45+
try {
46+
const spinner = ora('waiting for the response...').start()
47+
const response = await fetch(endpoint)
48+
const data = await response.json()
49+
const result = Object.values(data.query.random)
50+
51+
spinner.stop()
52+
53+
return result
54+
} catch (error) {
55+
console.log('sorry, there is a problem with that request', error)
56+
process.exit(1)
57+
}
58+
}
59+
60+
exports.getArticle = async (userPick, lang) => {
3661
try {
3762
const spinner = ora('waiting for the article...').start()
3863
const doc = await wtf.fetch(userPick, lang)
@@ -46,11 +71,8 @@ const getArticle = async (userPick, lang) => {
4671

4772
return result
4873
} catch (error) {
49-
console.log('sorry, there is a problem with that request')
74+
console.log('sorry, there is a problem with that request', error)
5075
process.exit(1)
5176
}
5277
}
5378

54-
exports.getSuggestedTopic = getSuggestedTopic
55-
exports.getArticle = getArticle
56-

src/help.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ module.exports = `
1010
--lang, -l set language for wikipedia articles (english by default)
1111
--previous, -p display and use previous searches
1212
--clear, -c clear search history
13+
--random, -r suggest random articles
1314
1415
Examples
1516
$ wicli
1617
$ wicli --lang
1718
$ wicli --previous
19+
$ wicli --random
1820
`

src/prompt.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ exports.historyInteractive = {
4444
symbol: '>',
4545
menu: []
4646
}
47+
48+
exports.randomInteractive = {
49+
type: 'interactive',
50+
query: bold().blue(' - here is a list of 10 random topics...'),
51+
handle: 'userPick',
52+
symbol: '>',
53+
menu: []
54+
}

src/wiclipedia.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,25 @@ const options = require('./boxen-options')
1919
* Check specifically if the user decided to quit the program or wanted to make another search
2020
* @param {String} input The picked choice after prompt
2121
*/
22-
function _checkUserAnswers(input) {
22+
function _checkUserAnswers(input, lang) {
2323
if (input.userPick.includes('(Try another search)')) {
2424
process.stdout.write('\u001Bc') // Clear the console
2525
header.logAppName()
2626
prompt.topicInteractive.menu = []
2727
return _search()
2828
}
2929

30+
if (input.userPick.includes('(Try another random)')) {
31+
process.stdout.write('\u001Bc') // Clear the console
32+
prompt.randomInteractive.menu = []
33+
return displayRandomArticlesList()
34+
}
35+
3036
if (input.userPick.includes('(Quit)')) {
3137
process.exit(1)
3238
}
39+
40+
config.storeSearches(input.userPick, lang)
3341
}
3442

3543
/**
@@ -41,7 +49,9 @@ function _fillInteractiveTopicsName(topics, promptName) {
4149
topics.forEach(item => {
4250
prompt[promptName].menu.push(item.title)
4351
})
44-
prompt[promptName].menu.push(yellow('(Try another search)'))
52+
promptName === 'randomInteractive' ?
53+
prompt[promptName].menu.push(yellow('(Try another random)')) :
54+
prompt[promptName].menu.push(yellow('(Try another search)'))
4555
prompt[promptName].menu.push(red('(Quit)'))
4656
}
4757

@@ -117,17 +127,16 @@ async function _askForATopic() {
117127
*/
118128
async function _refineTopics() {
119129
const input = await qoa.interactive(prompt.topicInteractive)
120-
await _checkUserAnswers(input)
121130
const lang = await config.checkLang()
131+
await _checkUserAnswers(input, lang)
122132
const response = await fetch.getArticle(input.userPick, lang)
123133

124-
config.storeSearches(input.userPick, lang)
125134
_displayArticle(response)
126135
prompt.topicInteractive.menu = []
127136
}
128137

129138
/**
130-
* Just calling other functions
139+
* Just calling other functions ¯\_(ツ)_/¯
131140
*/
132141
async function _search() {
133142
await _askForATopic()
@@ -199,3 +208,21 @@ exports.displayPreviousSearches = async () => {
199208
_displayArticle(response)
200209
prompt.historyInteractive.menu = []
201210
}
211+
212+
/**
213+
* Allow the user to display a list of random articles
214+
* Pick one of them, trigger an api call and display the response
215+
*/
216+
const displayRandomArticlesList = async () => {
217+
await header.logAppName()
218+
const lang = await config.checkLang()
219+
const suggestedTopics = await fetch.getRandomSuggestions(lang)
220+
_fillInteractiveTopicsName(suggestedTopics, 'randomInteractive')
221+
const input = await qoa.interactive(prompt.randomInteractive)
222+
await _checkUserAnswers(input, lang)
223+
const response = await fetch.getArticle(input.userPick, lang)
224+
_displayArticle(response)
225+
prompt.historyInteractive.menu = []
226+
}
227+
228+
exports.displayRandomArticlesList = displayRandomArticlesList

0 commit comments

Comments
 (0)