裝逼說(shuō)明
不喜歡安裝有道詞典的應(yīng)用程序,因?yàn)樘馁Y源還一堆廣告,但是用瀏覽器打開(kāi)dict.youdao.com也挺麻煩的,不是嗎?快來(lái)試試有道詞典命令行工具吧!
看圖裝逼

example.gif
自已裝逼
安裝
npm install yddict -g
使用說(shuō)明
yd <要查詢的單詞>
在/usr/local/lib/node_modules/yddict/目錄下,可以進(jìn)行一些自定義,或者更改查詢的來(lái)源,
主要源碼 index.js:
#!/usr/bin/env node
const request = require('request');
const cheerio = require('cheerio');
const chalk = require('chalk');
const fs = require('fs');
const Spinner = require('cli-spinner').Spinner;
const isChinese = require('is-chinese')
const urlencode = require('urlencode');
const spinner = new Spinner('努力查詢中... %s');
const home = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
const configFile = home + "/config.json";
let color = 'gray';//更改查詢結(jié)果的文字顏色
spinner.setSpinnerString('|/-\\');
spinner.start();
const readFile = (filename, encoding) => {
try {
return fs.readFileSync(filename).toString(encoding);
}
catch (e) {
return null;
}
};
const config = JSON.parse(readFile(configFile,"utf8"));
const input = process.argv.slice(2)
const word = input.join(' ')
const isCn = isChinese(word);
const URL = isCn ? `http://dict.youdao.com/w/eng/${urlencode(word)}`:`http://dict.youdao.com/w/${urlencode(word)}`
const options = {
'url':URL
};
if(config){
if(config.proxy){
options.proxy = config.proxy;
}
if(config.color){
color = config.color;
}
}
const color_output = chalk.keyword(color);
request(options,(error, response, body)=>{
const $ = cheerio.load(body, {
ignoreWhitespace: true,
xmlMode: true
});
let result = '';
spinner.stop(true);
if(isCn){
$('div.trans-container > ul').find('p.wordGroup').each(function(i,elm){
result = $(this).text().replace(/\s+/g," ");
});
}else{
result = $('div#phrsListTab > div.trans-container > ul').text();
}
// phrase
if (result === '') {
result = $('div#webPhrase > p.wordGroup').text();
}
// sentence
if (result === '') {
result = $('div#fanyiToggle > div.trans-container > p:nth-child(2)').text();
}
console.log(color_output(result));
});
裝逼指導(dǎo)
注:本文首發(fā)于 iHTCboy's blog,如若轉(zhuǎn)載,請(qǐng)注明來(lái)源。