-
Notifications
You must be signed in to change notification settings - Fork 58
/
index.js
138 lines (119 loc) · 3.61 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const https = require('https')
const md5 = require('./md5')
// Need to be filled out
const appKey = ''
// Need to be filled out
const secret = ''
const salt = 1
const interval = 0
let words = []
let triggered = false
let remainBuf = ''
let cnt = 1
process.stdin.setEncoding('utf8')
process.stdin.on('readable', () => {
let chunk = process.stdin.read()
if (chunk !== null) {
if (remainBuf) {
chunk = remainBuf + chunk
}
if (!chunk.endsWith('\n')) {
let offset = chunk.lastIndexOf('\n')
remainBuf = chunk.slice(offset + 1)
chunk = chunk.slice(0, offset + 1)
}
words = words.concat(chunk.split('\n').filter(w => w !== ''))
if (!triggered) {
triggered = true
triggerNext()
}
}
})
let triggerNext = (function () {
let i = 0
return function () {
if (i >= words.length) process.exit(0)
getData(words[i++], processDataCb)
}
})()
function getData (word, cb) {
word = word.trim()
const sign = md5(appKey + word + salt + secret)
const options = {
hostname: 'openapi.youdao.com',
port: 443,
path: `/api?q=${word}&from=en&to=zh-CHS&appKey=${appKey}&salt=1&sign=${sign}`,
method: 'GET'
}
const req = https.request(options, (res) => {
let data = ''
res.on('data', (d) => {
data += d
})
res.on('end', () => {
data = data.toString('utf8')
data = JSON.parse(data)
cb(data)
})
})
req.on('close', () => {
setTimeout(triggerNext, interval)
})
req.on('error', (e) => {
console.error('req err: ' + e)
})
req.end()
}
/*
* example JSON object:
* { tSpeakUrl: 'http://openapi.youdao.com/ttsapi?q=%E7%8C%AB&langType=zh-CHS&sign=33435BD941CCFA50EB490FC8E4B8C3D0&salt=1540286120196&voice=4&format=mp3&appKey=260d76ea9aaff3f2',
* web:
* [ { value: [Array], key: 'CAT' },
* { value: [Array], key: 'JUNGLE CAT' },
* { value: [Array], key: 'PERSIAN CAT' } ],
* query: 'cat',
* translation: [ '猫' ],
* errorCode: '0',
* dict: { url: 'yddict://m.youdao.com/dict?le=eng&q=cat' },
* webdict: { url: 'http://m.youdao.com/dict?le=eng&q=cat' },
* basic:
* { exam_type: [ '初中' ],
* 'us-phonetic': 'kæt',
* phonetic: 'kæt',
* 'uk-phonetic': 'kæt',
* 'uk-speech': 'http://openapi.youdao.com/ttsapi?q=cat&langType=en&sign=025F3551DDB2C3D7D54261708D6CF05F&salt=1540286120196&voice=5&format=mp3&appKey=260d76ea9aaff3f2',
* explains: [ 'n. 猫,猫科动物' ],
* 'us-speech': 'http://openapi.youdao.com/ttsapi?q=cat&langType=en&sign=025F3551DDB2C3D7D54261708D6CF05F&salt=1540286120196&voice=6&format=mp3&appKey=260d76ea9aaff3f2' },
* l: 'en2zh-CHS',
* speakUrl: 'http://openapi.youdao.com/ttsapi?q=cat&langType=en&sign=025F3551DDB2C3D7D54261708D6CF05F&salt=1540286120196&voice=4&format=mp3&appKey=260d76ea9aaff3f2' }
*/
function processDataCb (data) {
if (!data) return
if (data.query) {
process.stdout.write(data.query)
process.stdout.write(' ')
process.stdout.write((cnt++).toString())
process.stdout.write('\n')
}
if (data.basic) {
if (data.basic.phonetic) {
process.stdout.write(data.basic.phonetic)
process.stdout.write(' ')
}
if (data.basic['us-phonetic']) {
process.stdout.write(data.basic['us-phonetic'])
process.stdout.write(' ')
}
if (data.basic['uk-phonetic']) {
process.stdout.write(data.basic['uk-phonetic'])
process.stdout.write(' ')
}
process.stdout.write('\n')
if (data.basic.explains) {
process.stdout.write(' ' + data.basic.explains + '\n')
} else {
console.error(data)
}
}
process.stdout.write('\n')
}