forked from keenwon/eazydict-google
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
119 lines (100 loc) · 2.55 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'use strict'
const debug = require('./lib/debug')
const querystring = require('querystring')
const fetch = require('./lib/fetch')
const getToken = require('./lib/token')
const parser = require('./lib/parser')
const assign = require('lodash.assign')
const defaultConfigs = require('./defaultConfig')
const pkg = require('./package.json')
const {
EDOutput,
CODES
} = require('eazydict-standard-output')
// 构造请求数据
const getFetchData = (text, token) => {
/**
* 全中文,则翻译为英文
* 否则统一翻译为中文
*
* 注意:
*
* 使用 auto 的时候,会识别拼音。例如 you 翻译为 “有”
* 所以强制设为英汉互译
*/
let from, to
if (/[\u4e00-\u9fa5]/.test(text)) {
from = 'zh-CN'
to = 'en'
} else {
from = 'en'
to = 'zh-CN'
}
return {
client: 't',
sl: from,
tl: to,
hl: 'zh-CN',
dt: [
'at', 'bd', 'ex', 'ld', 'md',
'qca', 'rw', 'rm', 'ss', 't'
],
ie: 'UTF-8',
oe: 'UTF-8',
otf: 1,
ssel: 0,
tsel: 0,
kc: 4,
tk: token,
q: text
}
}
// 入口
function main (words, userConfigs) {
debug('run with arguments %O', {
words,
userConfigs
})
let configs = assign({}, defaultConfigs, userConfigs)
debug('use configs %O', configs)
if (!words) {
return Promise.reject(new Error('请输入要查询的文字'))
}
const baseUrl = 'https://translate.google.cn'
let fetchBody, url, api
return getToken(words, configs)
.then(token => {
fetchBody = getFetchData(words, token)
url = `${baseUrl}/#view=home&op=translate&sl=${fetchBody.sl}&tl=${fetchBody.tl}&text=${encodeURIComponent(words)}`
api = `${baseUrl}/translate_a/single?${querystring.stringify(fetchBody)}`
debug(`fetch url: ${url.replace(/%/g, '%%')}`)
debug(`fetch api: ${api.replace(/%/g, '%%')}`)
return fetch(api, configs)
})
.then(data => parser(data, words))
.catch(error => {
debug(error)
if (error.name === 'FetchError') {
return new EDOutput(CODES.NETWORK_ERROR)
}
return new EDOutput(CODES.OTHER)
})
.then(output => {
// 添加插件信息
output.pluginName = 'Google'
output.packageName = pkg.name
output.words = words
output.url = url
debug('output: %O', output)
return output
})
}
if (require.main === module) {
// istanbul ignore next
let word = process.argv.slice(2).join(' ')
main(word).then(result => {
console.log(result) // eslint-disable-line no-console
})
} else {
module.exports = main
}