-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
83 lines (76 loc) · 2.43 KB
/
main.js
File metadata and controls
83 lines (76 loc) · 2.43 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#! /usr/bin/env node
const qs = require('querystring');
const chalk = require('chalk');
const {post} = require('./share');
const genBaiduParam = require('./baidu');
const genYoudaoParam = require('./youdao');
let [, , ...argv] = process.argv;
query = argv.join(' ');
const baiduParam = genBaiduParam(query);
const youdaoParam = genYoudaoParam(query);
const log = console.log;
async function run() {
log(chalk.yellow(`"${query}" 的翻译结果:`))
try {
const youdaoData = await post("https://openapi.youdao.com/api", qs.stringify(youdaoParam));
const data = youdaoData.data;
if (data.errorCode === '0') {
const { web, basic, translation = [], l } = data;
log('---------------------------------')
if (l === 'en2zh-CHS') {
if (basic) {
log(chalk.cyan('音标:'), `英[${basic['uk-phonetic'] || '无'}] 美[${basic['us-phonetic'] || '无'}]`);
log('---------------------------------')
log(chalk.cyan('基本解释:'))
basic.explains.forEach(item => { log(item) })
log('---------------------------------')
if (basic.wfs) {
let wf = '';
basic.wfs.forEach(item => {
wf += item.wf.name + ':' + item.wf.value + ';';
})
log(wf);
log('---------------------------------')
}
} else {
log(translation.join());
}
if (web) {
log(chalk.cyan('相关翻译:'));
web.forEach(item => {
log(`${item.key}: ${item.value.join(',')}`);
})
}
} else {
if (basic && basic.explains) {
log(chalk.cyan('基本翻译:'));
log(basic.explains.join(','));
log('---------------------------------')
} else {
log(translation.join());
}
if (web) {
log(chalk.cyan('相关翻译:'));
web.forEach(item => {
log(`${item.key}: ${item.value.join(',')}`);
})
}
}
}
} catch(e) {
throw e;
}
try {
const baiduData = await post("https://fanyi-api.baidu.com/api/trans/vip/translate", qs.stringify(baiduParam));
const trans = baiduData.data.trans_result;
log('---------------------------------')
log(chalk.cyan('百度翻译:'));
trans.forEach(item => {
log(item.dst);
})
log('---------------------------------')
} catch(e) {
throw e;
}
}
run();