-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
57 lines (48 loc) · 1.41 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env node
/**
* Module dependencies.
*/
var program = require('commander');
var http = require('http');
var querystring = require('querystring');
var md5 = require('md5');
program
.version('0.0.1')
.description('🍻 欢迎使用 Airing 翻译小助手 🍻')
.option('-e, --en', 'Add English word')
.option('-z, --zh', 'Add Chinese word')
.parse(process.argv);
console.log('=== 🍻 欢迎使用 Airing 翻译小助手 🍻 ===');
if (program.en || program.zh) {
var timestamp = new Date().getTime();
var appid = "2015063000000001";
var key = '12345678';
var sign = md5(appid + program.args[0] + timestamp + key);
var params = {
'q': program.args[0],
'from': program.en ? 'en' : 'zh',
'to': program.en ? 'zh' : 'en',
'appid': appid,
'salt': timestamp,
'sign': sign
};
var content = querystring.stringify(params);
var options = {
hostname: 'api.fanyi.baidu.com',
port: 80,
path: '/api/trans/vip/translate?' + content,
method: 'GET'
};
var req = http.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (data) {
console.log(program.args[0] + ' 的翻译结果为:');
console.log(JSON.parse(data).trans_result[0].dst);
console.log('=== 🍻 翻译成功 🍻 ===')
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.end();
}