-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathyd.js
More file actions
131 lines (114 loc) · 4.62 KB
/
yd.js
File metadata and controls
131 lines (114 loc) · 4.62 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
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
var request = require('request');
// select * from html where url='https://tw.dictionary.yahoo.com/dictionary?p=女朋友' and xpath='//span[@id=\'term\'] | //div[contains(@class, \'explain\')]'
// https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D'https%3A%2F%2Ftw.dictionary.yahoo.com%2Fdictionary%3Fp%3Djizz'%20and%20xpath%3D'%2F%2Fspan%5B%40id%3D%5C'term%5C'%5D%20%7C%20%2F%2Fdiv%5Bcontains(%40class%2C%20%5C'explain%5C')%5D'&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys
var queryWord = "girlfriend";
//var queryWord = "taiwan";
//var queryWord = "女朋友";
//var queryWord = "work";
String.prototype.escapeSQL = function() {
return this.replace(/'/g, "\\\'");
}
var queryDictionary = function(word, callback) {
var yahooDicURL = "https://tw.dictionary.yahoo.com/dictionary?p=" + encodeURIComponent(word);
var xpath = "//span[@id='term'] | //div[contains(@class, 'explain')]";
var q = "select * from html where url='" + yahooDicURL.escapeSQL() + "' and xpath='" + xpath.escapeSQL() + "'";
var queryURL = "https://query.yahooapis.com/v1/public/yql?q=" + encodeURIComponent(q) + "&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
var defaultResult = {
"q": word
};
request(queryURL, function(error, response, body){
if (error) {
callback(true, defaultResult);
} else if (response.body.error) {
callback(true, defaultResult);
} else {
var error = false;
var result;
try {
result = JSON.parse(body);
result = result["query"]["results"];
} catch (e) {
console.log("QAQ");
error = true;
callback(true, defaultResult);
}
if (!error && result) {
//console.log(result);
var resultWord = result["span"]["content"];
var explains = result["div"];
if (explains) {
var a = explains["div"];
var b = {};
var c = explains["ul"];
var d = "";
var e = [];
if (a) {
if (a[0] === undefined) {
var n = a["h3"]["span"]["content"];
if (n) {
b[0] = n;
}
} else {
for (var i = 0; i < a.length; i++) {
var gg = a[i];
var n = gg["h3"]["span"]["content"];
if (n) {
b[i] = n;
}
}
}
//console.log(b);
}
if (c) {
if (c[0] === undefined) {
c = [c];
}
for (var i = 0; i < c.length; i ++) {
var gg = c[i];
gg = gg["li"];
if (b && b[i] && gg) {
d = d + b[i] + "\n";
}
//console.log(gg);
if (gg[0] == undefined) {
gg = [gg];
}
//console.log(gg);
for (var j = 0; j < gg.length; j++ ) {
var jizz = gg[j]["h4"];
//console.log(jizz);
if (jizz) {
d = d + jizz + "\n";
}
}
e.push(d);
d = "";
}
//console.log(c);
}
//console.log(d);
//console.log(e);
}
defaultResult["r"] = resultWord;
defaultResult["e"] = e;
//console.log(defaultResult);
callback(false, defaultResult);
} else {
callback(true, defaultResult);
}
}
});
};
// test
/*
queryDictionary(queryWord, function(error, result){
//console.log("callback called");
if (error) {
console.log("GG");
} else {
}
});
*/
module.exports = {
"queryDictionary": queryDictionary
};