From edb24bad867807602ea9ca471ab0a3ffa94c27ae Mon Sep 17 00:00:00 2001 From: EryouHao Date: Fri, 3 Nov 2017 20:58:27 +0800 Subject: [PATCH 1/4] Add phrase and sentence search --- index.js | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 4398b27..1172a4a 100644 --- a/index.js +++ b/index.js @@ -12,12 +12,9 @@ const home = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'] const configFile = home + "/config.json"; let color = 'white'; - spinner.setSpinnerString('|/-\\'); spinner.start(); - - const readFile = (filename, encoding) => { try { @@ -30,10 +27,11 @@ const readFile = (filename, encoding) => { const config = JSON.parse(readFile(configFile,"utf8")); -const word = process.argv.slice(2)[0]; +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/${word}` -//const URL = 'http://dict.youdao.com/w/eng/%E5%A5%BD' +const URL = isCn ? `http://dict.youdao.com/w/eng/${urlencode(word)}`:`http://dict.youdao.com/w/${urlencode(word)}` + const options = { 'url':URL }; @@ -49,18 +47,28 @@ if(config){ const color_output = chalk.keyword(color); request(options,(error, response, body)=>{ + const $ = cheerio.load(body, { + ignoreWhitespace: true, + xmlMode: true + }); + let result = ''; - const $ = cheerio.load(body); - - //console.log(response); spinner.stop(true); if(isCn){ $('div.trans-container > ul').find('p.wordGroup').each(function(i,elm){ - var line = $(this).text().replace(/\s+/g," "); - console.log(color_output(line)); + result = $(this).text().replace(/\s+/g," "); }); }else{ - console.log(color_output($('div#phrsListTab > div.trans-container > ul').text())); + 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)); }); From 5856e42ed4a66e2e92f78bf476a8acd6938745c5 Mon Sep 17 00:00:00 2001 From: kenshinji Date: Sat, 4 Nov 2017 17:36:35 +0800 Subject: [PATCH 2/4] Update version and Changelog --- CHANGELOG.md | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4786fec..cc45f68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## v1.1.0 +*2017-11-04* +- Add phrase and sentence search + ## v1.0.5 *2017-09-10* - Fix error of "env: node\r: No such file or directory" diff --git a/package.json b/package.json index 1295f25..a3fe09b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "yddict", - "version": "1.0.5", + "version": "1.1.0", "description": "A CLI tool for querying youdao dict.", "main": "./index.js", "scripts": { From c9c6fdd9e9a1d069010f0ce14c41f2dc2f094191 Mon Sep 17 00:00:00 2001 From: wb-dbl257323 Date: Wed, 13 Dec 2017 16:21:48 +0800 Subject: [PATCH 3/4] =?UTF-8?q?add=EF=BC=9A=E6=B7=BB=E5=8A=A0commander?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 1 + package.json | 1 + 2 files changed, 2 insertions(+) diff --git a/index.js b/index.js index 1172a4a..d09909b 100644 --- a/index.js +++ b/index.js @@ -26,6 +26,7 @@ const readFile = (filename, encoding) => { }; const config = JSON.parse(readFile(configFile,"utf8")); +console.log(process.argv); const input = process.argv.slice(2) const word = input.join(' ') diff --git a/package.json b/package.json index a3fe09b..ebe581f 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "chalk": "^2.1.0", "cheerio": "^1.0.0-rc.2", "cli-spinner": "^0.2.6", + "commander": "^2.12.2", "is-chinese": "^1.2.5", "request": "^2.81.0", "urlencode": "^1.1.0" From 23817f01720b14cdef278223120b2b36c267e93f Mon Sep 17 00:00:00 2001 From: wb-dbl257323 Date: Wed, 13 Dec 2017 18:01:06 +0800 Subject: [PATCH 4/4] =?UTF-8?q?feat=EF=BC=9Achoose=20language?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/index.js | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 75 ---------------------------------------- package.json | 4 +-- 3 files changed, 100 insertions(+), 77 deletions(-) create mode 100644 bin/index.js delete mode 100644 index.js diff --git a/bin/index.js b/bin/index.js new file mode 100644 index 0000000..a135e5f --- /dev/null +++ b/bin/index.js @@ -0,0 +1,98 @@ +#!/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 program = require('commander'); +const spinner = new Spinner('努力查询中... %s'); +const pkg = require('../package.json'); +const home = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME']; +const configFile = home + "/config.json"; +let color = 'white'; + +spinner.setSpinnerString('|/-\\'); +spinner.start(); + +const readFile = (filename, encoding) => { + try { + return fs.readFileSync(filename).toString(encoding); + } + catch (e) { + return null; + } +}; + +const startRequest = (options, isCn, cb) => { + request(options,(error, response, body)=>{ + const $ = cheerio.load(body, { + ignoreWhitespace: true, + xmlMode: true + }); + let result = ''; + + 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(); + } + cb(result); + }); +} + +let input = process.argv.slice(2); +let language = 'eng'; +const options = {}; +const config = JSON.parse(readFile(configFile,"utf8")); +if(config){ + if(config.proxy){ + options.proxy = config.proxy; + } + if(config.color){ + color = config.color; + } +} +const color_output = chalk.keyword(color); + +const languageList = ['cn', 'eng', 'fr', 'ko', 'jap']; +const cmdOptions = [ + ['-l, --language', 'Choose langage of yddict'] +]; + +program.version(pkg.version); +cmdOptions.forEach((item) => { + program.option(...item); +}); +program.parse(process.argv); + +if (program.language) { + if (process.argv[3] && languageList.indexOf(process.argv[3]) > -1) { + language = process.argv.slice(3, 4); + input = process.argv.slice(4); + } else { + console.log(chalk.red('Can not find valid language!')); + } +} + +const word = input.join(' '); +const isCn = isChinese(word); +const URL = isCn ? `http://dict.youdao.com/w/${language}/${urlencode(word)}`:`http://dict.youdao.com/w/${urlencode(word)}`; +options.url = URL; + +startRequest(options, isCn, (result) => { + spinner.stop(true); + console.log(color_output(result)); +}); diff --git a/index.js b/index.js deleted file mode 100644 index d09909b..0000000 --- a/index.js +++ /dev/null @@ -1,75 +0,0 @@ -#!/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 = 'white'; - -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")); -console.log(process.argv); - -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)); - -}); diff --git a/package.json b/package.json index ebe581f..83b2163 100644 --- a/package.json +++ b/package.json @@ -2,14 +2,14 @@ "name": "yddict", "version": "1.1.0", "description": "A CLI tool for querying youdao dict.", - "main": "./index.js", + "main": "./bin/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "kenshinji ", "license": "MIT", "bin": { - "yd": "./index.js" + "yd": "./bin/index.js" }, "dependencies": { "chalk": "^2.1.0",