/
wikipedia.js
executable file
·109 lines (106 loc) · 3.03 KB
/
wikipedia.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* eslint-disablemax-len:0 */
const themes = require('../themes')
const tools = require('../tools')
const _ = require('lodash')
const gg = require('good-guy-http')
const noon = require('noon')
const http = gg({
cache: false,
defaultCache: {
cached: false
}
})
const CFILE = `${process.env.HOME}/.iloa.noon`
exports.command = 'wikipedia <query>'
exports.aliases = ['wiki', 'wp']
exports.desc = 'Wikipedia summaries'
exports.builder = {
out: {
alias: 'o',
desc: 'Write cson, json, noon, plist, yaml, xml',
default: '',
type: 'string'
},
force: {
alias: 'f',
desc: 'Force overwriting outfile',
default: false,
type: 'boolean'
},
intro: {
alias: 'i',
desc: 'Just intro or all sections',
default: false,
type: 'boolean'
},
save: {
alias: 's',
desc: 'Save options to config file',
default: false,
type: 'boolean'
}
}
exports.handler = (argv) => {
tools.checkConfig(CFILE)
let config = noon.load(CFILE)
const userConfig = {
wiki: {
intro: argv.i
}
}
if (config.merge) config = _.merge({}, config, userConfig)
if (argv.s && config.merge) noon.save(CFILE, config)
if (argv.s && !config.merge) throw new Error("Can't save user config, set option merge to true.")
const theme = themes.loadTheme(config.theme)
if (config.verbose) themes.label(theme, 'down', 'Wikipedia')
const wcont = []
wcont.push(argv.query)
if (argv._.length > 1) {
for (let i = 0; i <= argv._ - 1; i++) {
if (argv._[i] !== 'wp' && argv._[i] !== 'wiki' && argv._[i] !== 'wikipedia') wcont.push(argv._[i])
}
}
let words = ''
if (wcont.length > 1) {
words = wcont.join('+')
} else {
words = wcont[0]
}
let prefix = 'https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&indexpageids&redirects=1&continue=&explaintext='
if (argv.i) prefix = `${prefix}&exintro=`
let url = `${prefix}&titles=${words}`
url = encodeURI(url)
const tofile = {
type: 'wiki',
source: 'http://www.wikipedia.org/',
url
}
http({ url }, (error, response) => {
if (!error && response.statusCode === 200) {
const body = JSON.parse(response.body)
if (body.query.pageids[0] === '-1') {
if (body.query.normalized) {
const fixed = body.query.normalized[0]
console.log(`Query normalized from ${fixed.from} to ${fixed.to}, try searching again.`)
process.exit(0)
} else {
console.log(`No Wikipedia article found for ${wcont.join(' ')}, try searching again.`)
process.exit(0)
}
}
const pageID = body.query.pageids[0]
const page = body.query.pages[pageID]
const plain = page.extract.trim()
const wrapped = tools.wrapStr(plain, true, true)
if (config.verbose) {
themes.label(theme, 'down', 'Summary', wrapped)
} else {
console.log(wrapped)
}
tofile.summary = plain
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
} else {
throw new Error(`HTTP ${error.statusCode}: ${error.reponse.body}`)
}
})
}