-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathurban.js
88 lines (85 loc) · 2.37 KB
/
urban.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
/* eslint max-len:0 */
const themes = require('../themes')
const tools = require('../tools')
const _ = require('lodash')
const http = require('good-guy-http')()
const noon = require('noon')
const CFILE = `${process.env.HOME}/.leximaven.noon`
exports.command = 'urban <query>'
exports.aliases = ['urb', 'slang']
exports.desc = 'Urban Dictionary definitions'
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'
},
save: {
alias: 's',
desc: 'Save flags to config file',
default: false,
type: 'boolean'
},
limit: {
alias: 'l',
desc: 'Limit number of results',
default: 5,
type: 'number'
}
}
exports.handler = (argv) => {
tools.checkConfig(CFILE)
let config = noon.load(CFILE)
const userConfig = {
urban: {
limit: argv.l
}
}
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', 'Urban Dictionary')
const ucont = []
ucont.push(argv.query)
if (argv._.length > 1) {
for (let i = 0; i <= argv._.length - 1; i++) {
if (argv._[i] !== 'urban' && argv._[i] !== 'urb' && argv._[i] !== 'slang') ucont.push(argv._[i])
}
}
let words = ''
if (ucont.length > 1) {
words = ucont.join('+')
} else {
words = ucont[0]
}
let url = `http://api.urbandictionary.com/v0/define?term=${words}`
url = encodeURI(url)
const tofile = {
type: 'urban',
source: 'http://www.urbandictionary.com',
url
}
http({ url }, (error, response) => {
if (!error && response.statusCode === 200) {
const body = JSON.parse(response.body)
const limit = config.urban.limit
const list = body.list.slice(0, limit)
for (let i = 0; i <= list.length - 1; i++) {
const result = list[i]
themes.label(theme, 'down', 'Definition', result.definition)
tofile[[`definition${i}`]] = result.definition
}
if (argv.o) tools.outFile(argv.o, argv.f, tofile)
} else {
throw new Error(`HTTP ${error.statusCode}: ${error.reponse.body}`)
}
})
}