裝逼神句———有道詞典命令行查詢工具

裝逼說(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)源。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容