-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·102 lines (93 loc) · 2.99 KB
/
index.js
File metadata and controls
executable file
·102 lines (93 loc) · 2.99 KB
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
#!/usr/bin/env node
const program = require('commander');
const chalk = require('chalk');
const cheerio = require('cheerio');
const version = require('./package.json').version;
const exec = require('child_process').exec;
const request = require('request');
const ora = require('ora');
const spinner = ora('Loading...');
const baseUrl = 'http://cn.bing.com/dict/search?q=';
program
.version(version)
.name('bdic')
.arguments('<word>')
.usage('[options] <word>')
.option('-c --complete', 'Complete definition')
.option('-b --browser', 'Search in browser')
.on('--help', () => {console.log()})
.action((word, options) => {
let url = baseUrl + encodeURIComponent(word);
if (options.browser) {
openInBrowser(url);
} else {
searchWord(url, options.complete);
}
})
.parse(process.argv);
/**
* Open in browser
* @param {*} url
*/
function openInBrowser(url) {
if (process.platform === 'darwin') {
exec(`open ${url}`);
} else if (process.platform === 'win32') {
exec(`start ${url}`);
}
}
/**
* Search target word
* @param {*} url
* @param {*} complete_mode
*/
function searchWord(url, complete_mode) {
spinner.start();
request(url, (err, res, body) => {
spinner.stop();
parseContent(body, complete_mode);
});
}
/**
* Parse document content
* @param {*} body
* @param {*} complete_mode
*/
function parseContent(body, complete_mode) {
let $ = cheerio.load(body);
let $lf_area = $('.content .lf_area');
let headword = $lf_area.find('#headword').text();
if (!headword) {
console.log(chalk.red('没有找到相关的结果'));
return;
}
console.log();
// headword and phonetic symbol
if (complete_mode) {
let phonetic_symbol = $lf_area.find('.hd_tf_lh').text();
console.log(` ${chalk.whiteBright.bold.underline(headword)} ${phonetic_symbol}\n`);
}
// definitions
$lf_area.find('.qdef ul li').each((i, v) => {
let pos = $(v).find('.pos').text();
let def = $(v).find('.def').text();
let line = ` ${chalk.cyanBright(pos)} ${chalk.whiteBright(def)}`;
console.log(line);
});
console.log();
// sentences
if (complete_mode) {
console.log(` ${chalk.bgWhite.black(' 例句 ')}\n`);
$lf_area.find('#sentenceSeg .se_li').each((i, v) => {
let num = $(v).find('.se_n_d').text();
let sen_en = $(v).find('.sen_en').text();
let sen_cn = $(v).find('.sen_cn').text();
let sen_en_word = $(v).find('.sen_en .p1-7').text();
let sen_cn_word = $(v).find('.sen_cn .p1-7').text();
sen_en = sen_en.replace(sen_en_word, chalk.cyan(sen_en_word));
sen_cn = sen_cn.replace(sen_cn_word, chalk.cyan(sen_cn_word));
if (i >= 3) return;
console.log(` ${chalk.whiteBright(num)} ${chalk.whiteBright(sen_en)}\n ${chalk.white(sen_cn)}\n`);
});
}
}