forked from afc163/fanyi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (71 loc) · 2.03 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
const request = require('request');
const SOURCE = require('./lib/source');
const print = require('./lib/print');
const Entities = require('html-entities').AllHtmlEntities;
const entities = new Entities();
const parseString = require('xml2js').parseString;
const isChinese = require('is-chinese');
const ora = require('ora');
module.exports = function (word, options, callback) {
console.log('');
const spinner = ora().start();
// say it
try {
if (!process.env.CI) {
// require('say').speak(word, isChinese(word) ? 'Ting-Ting' : null);
}
} catch (e) {
// do nothing
}
let count = 0;
const callbackAll = () => {
count += 1;
if (count >= 1) {
spinner.stop();
callback && callback();
}
};
word = encodeURIComponent(word);
// iciba
// request.get(SOURCE.iciba.replace('${word}', word), function (error, response, body) {
// if (!error && response.statusCode == 200) {
// parseString(body, function (err, result) {
// if (err) {
// return;
// }
// print.iciba(result.dict, options);
// });
// }
// callbackAll();
// });
// youdao
request.get(SOURCE.youdao.replace('${word}', word), function (error, response, body) {
if (!error && response.statusCode == 200) {
try {
const data = JSON.parse(entities.decode(body));
print.youdao(data, options);
} catch (e) {
// 来自您key的翻译API请求异常频繁,为保护其他用户的正常访问,只能暂时禁止您目前key的访问
}
}
callbackAll();
});
// dictionaryapi
// request.get(SOURCE.dictionaryapi.replace('${word}', word), { timeout: 6000 }, function (
// error,
// response,
// body,
// ) {
// if (error) {
// return callbackAll();
// }
// if (response.statusCode == 200) {
// parseString(body, function (err, result) {
// if (!err) {
// print.dictionaryapi(result.entry_list.entry, word, options);
// }
// });
// }
// callbackAll();
// });
};