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/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 4398b27..0000000 --- a/index.js +++ /dev/null @@ -1,66 +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")); - -const word = process.argv.slice(2)[0]; -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 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); - - //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)); - }); - }else{ - console.log(color_output($('div#phrsListTab > div.trans-container > ul').text())); - } - -}); diff --git a/package.json b/package.json index 1295f25..83b2163 100644 --- a/package.json +++ b/package.json @@ -1,20 +1,21 @@ { "name": "yddict", - "version": "1.0.5", + "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", "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"