Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev分支,新增选择语种翻译功能 #11

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
98 changes: 98 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -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));
});
66 changes: 0 additions & 66 deletions index.js

This file was deleted.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"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"
Expand Down