├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Airing 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Screenshot 2 | 3 | ![](https://airing.ursb.me/image/cover/airing-translator.jpeg) 4 | 5 | ## Installation 6 | 7 | ``` 8 | npm install airing-translator -g 9 | ``` 10 | 11 | ## Help 12 | 13 | 14 | ``` 15 | Usage: airing [options] 16 | 17 | 🍻 欢迎使用 Airing 翻译小助手 🍻 18 | 19 | 20 | Options: 21 | 22 | -V, --version output the version number 23 | -e, --en Add English word 24 | -z, --zh Add Chinese word 25 | -h, --help output usage information 26 | ``` 27 | 28 | ## Example 29 | 30 | ### en -> zh 31 | 32 | ``` 33 | airing -e apple 34 | ``` 35 | 36 | Output: 37 | 38 | ``` 39 | === 🍻 欢迎使用 Airing 翻译小助手 🍻 === 40 | apple 的翻译结果为: 41 | 苹果 42 | === 🍻 翻译成功 🍻 === 43 | ``` 44 | 45 | ### zh -> en 46 | 47 | 48 | ``` 49 | airing -z 苹果 50 | ``` 51 | 52 | Output: 53 | 54 | ``` 55 | === 🍻 欢迎使用 Airing 翻译小助手 🍻 === 56 | 苹果 的翻译结果为: 57 | Apple 58 | === 🍻 翻译成功 🍻 === 59 | ``` -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | /** 4 | * Module dependencies. 5 | */ 6 | 7 | var program = require('commander'); 8 | var http = require('http'); 9 | var querystring = require('querystring'); 10 | var md5 = require('md5'); 11 | 12 | program 13 | .version('0.0.1') 14 | .description('🍻 欢迎使用 Airing 翻译小助手 🍻') 15 | .option('-e, --en', 'Add English word') 16 | .option('-z, --zh', 'Add Chinese word') 17 | .parse(process.argv); 18 | 19 | console.log('=== 🍻 欢迎使用 Airing 翻译小助手 🍻 ==='); 20 | 21 | if (program.en || program.zh) { 22 | var timestamp = new Date().getTime(); 23 | var appid = "2015063000000001"; 24 | var key = '12345678'; 25 | var sign = md5(appid + program.args[0] + timestamp + key); 26 | var params = { 27 | 'q': program.args[0], 28 | 'from': program.en ? 'en' : 'zh', 29 | 'to': program.en ? 'zh' : 'en', 30 | 'appid': appid, 31 | 'salt': timestamp, 32 | 'sign': sign 33 | }; 34 | var content = querystring.stringify(params); 35 | var options = { 36 | hostname: 'api.fanyi.baidu.com', 37 | port: 80, 38 | path: '/api/trans/vip/translate?' + content, 39 | method: 'GET' 40 | }; 41 | var req = http.request(options, function (res) { 42 | res.setEncoding('utf8'); 43 | res.on('data', function (data) { 44 | console.log(program.args[0] + ' 的翻译结果为:'); 45 | console.log(JSON.parse(data).trans_result[0].dst); 46 | console.log('=== 🍻 翻译成功 🍻 ===') 47 | }); 48 | }); 49 | 50 | req.on('error', function (e) { 51 | console.log('problem with request: ' + e.message); 52 | }); 53 | 54 | req.end(); 55 | } 56 | 57 | 58 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "airing-translator", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "dependencies": { 6 | "charenc": { 7 | "version": "0.0.2", 8 | "resolved": "http://registry.npm.taobao.org/charenc/download/charenc-0.0.2.tgz", 9 | "integrity": "sha1-wKHS86cJLgN3S/qD8UwPxXkKhmc=" 10 | }, 11 | "commander": { 12 | "version": "2.11.0", 13 | "resolved": "http://registry.npm.taobao.org/commander/download/commander-2.11.0.tgz", 14 | "integrity": "sha1-FXFS/R56bI2YpbcVzzdt+SgARWM=" 15 | }, 16 | "crypt": { 17 | "version": "0.0.2", 18 | "resolved": "http://registry.npm.taobao.org/crypt/download/crypt-0.0.2.tgz", 19 | "integrity": "sha1-iNf/fsDfuG9xPch7u0LQRNPmxBs=" 20 | }, 21 | "http": { 22 | "version": "0.0.0", 23 | "resolved": "http://registry.npm.taobao.org/http/download/http-0.0.0.tgz", 24 | "integrity": "sha1-huYybSnF0Dnen6xYSkVon5KfT3I=" 25 | }, 26 | "is-buffer": { 27 | "version": "1.1.5", 28 | "resolved": "http://registry.npm.taobao.org/is-buffer/download/is-buffer-1.1.5.tgz", 29 | "integrity": "sha1-Hzsm72E7IUuIy8ojzGwB2Hlh7sw=" 30 | }, 31 | "md5": { 32 | "version": "2.2.1", 33 | "resolved": "http://registry.npm.taobao.org/md5/download/md5-2.2.1.tgz", 34 | "integrity": "sha1-U6s41f48iJG6RlMp6iP6wFQBJvk=" 35 | }, 36 | "querystring": { 37 | "version": "0.2.0", 38 | "resolved": "http://registry.npm.taobao.org/querystring/download/querystring-0.2.0.tgz", 39 | "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=" 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "airing-translator", 3 | "version": "1.0.4", 4 | "description": "命令行翻译小助手airing", 5 | "main": "index.js", 6 | "bin": { 7 | "airing": "./index.js" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "git+https://airingursb@github.com/airingursb/airing-translator.git" 15 | }, 16 | "keywords": [ 17 | "commander" 18 | ], 19 | "author": { 20 | "name": "Airing", 21 | "email" :"airing@ursb.me", 22 | "url" : "http://me.ursb.me" 23 | }, 24 | "license": "MIT", 25 | "bugs": { 26 | "url": "https://github.com/airingursb/airing-translator/issues", 27 | "email" :"airing@ursb.me" 28 | }, 29 | "homepage": "https://github.com/airingursb/airing-translator#readme", 30 | "dependencies": { 31 | "commander": "^2.11.0", 32 | "http": "0.0.0", 33 | "md5": "^2.2.1", 34 | "querystring": "^0.2.0" 35 | } 36 | } 37 | --------------------------------------------------------------------------------