forked from afc163/fanyi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (34 loc) · 1.12 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
var request = require('request');
var parser = require('xml2json');
var SOURCE = require('./lib/source.json');
var print = require('./lib/print');
var spawn = require('child_process').spawn;
var Entities = require('html-entities').AllHtmlEntities;
entities = new Entities();
module.exports = function(word) {
// avoid no say command
process.on('uncaughtException', function(err) {
if (err.toString().indexOf('spawn ENOENT') < 0) {
console.log(err);
}
});
// say it
spawn('say', [word]);
// iciba
request.get(SOURCE['iciba'] + encodeURIComponent(word), function (error, response, body) {
if (!error && response.statusCode == 200) {
// escape " -> '
body = body.replace(/"/g, ''');
// iciba need twice decode ...
var data = JSON.parse(entities.decode(entities.decode(parser.toJson(body)))).dict;
print.iciba(data);
}
});
// youdao
request.get(SOURCE['youdao'] + encodeURIComponent(word), function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(entities.decode(body));
print.youdao(data);
}
});
};