forked from meetDeveloper/freeDictionaryAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
216 lines (184 loc) · 7.42 KB
/
app.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
var app = require("express")();
var cheerio = require("cheerio");
var request = require('request');
var path = require("path");
function extend() {
var options, name, src, copy, copyIsArray, clone, target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false,
toString = Object.prototype.toString,
hasOwn = Object.prototype.hasOwnProperty,
push = Array.prototype.push,
slice = Array.prototype.slice,
trim = String.prototype.trim,
indexOf = Array.prototype.indexOf,
class2type = {
"[object Boolean]": "boolean",
"[object Number]": "number",
"[object String]": "string",
"[object Function]": "function",
"[object Array]": "array",
"[object Date]": "date",
"[object RegExp]": "regexp",
"[object Object]": "object"
},
jQuery = {
isFunction: function (obj) {
return jQuery.type(obj) === "function"
},
isArray: Array.isArray ||
function (obj) {
return jQuery.type(obj) === "array"
},
isWindow: function (obj) {
return obj != null && obj == obj.window
},
isNumeric: function (obj) {
return !isNaN(parseFloat(obj)) && isFinite(obj)
},
type: function (obj) {
return obj == null ? String(obj) : class2type[toString.call(obj)] || "object"
},
isPlainObject: function (obj) {
if (!obj || jQuery.type(obj) !== "object" || obj.nodeType) {
return false
}
try {
if (obj.constructor && !hasOwn.call(obj, "constructor") && !hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) {
return false
}
} catch (e) {
return false
}
var key;
for (key in obj) {}
return key === undefined || hasOwn.call(obj, key)
}
};
if (typeof target === "boolean") {
deep = target;
target = arguments[1] || {};
i = 2;
}
if (typeof target !== "object" && !jQuery.isFunction(target)) {
target = {}
}
if (length === i) {
target = this;
--i;
}
for (i; i < length; i++) {
if ((options = arguments[i]) != null) {
for (name in options) {
src = target[name];
copy = options[name];
if (target === copy) {
continue
}
if (deep && copy && (jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)))) {
if (copyIsArray) {
copyIsArray = false;
clone = src && jQuery.isArray(src) ? src : []
} else {
clone = src && jQuery.isPlainObject(src) ? src : {};
}
// WARNING: RECURSION
target[name] = extend(deep, clone, copy);
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
return target;
}
app.get("/", function(req, res){
if(!req.query.define){
res.sendFile(path.join(__dirname+'/welcome.html'));
} else {
request({
method: 'GET',
url: 'https://www.google.co.in/search?q=define+' + req.query.define,
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0"
}
}, function(err, response, body) {
if(err){
return console.error(err);
}
var dictionary = {};
var $ = cheerio.load(body);
dictionary.word = $("div.dDoNo span").first().text();
dictionary.pronunciation = []
$('.lr_dct_spkr.lr_dct_spkr_off audio').each(function(i, element){
dictionary.pronunciation.push("https:" + $(this)[0].attribs.src);
});
// dictionary.pronunciation = dictionary.pronunciation.replace('--_gb', '--_us');
dictionary.phonetic = [];
$(".lr_dct_ph.XpoqFe").each(function(i, element){
var pho = []
$(this).find('span').each(function(i, element){
pho.push($(this).text())
});
dictionary.phonetic.push(pho);
});
dictionary.meaning = {};
if(dictionary.word.length < 1){
res.header("Access-Control-Allow-Origin", "*");
return res.status(404).sendFile(path.join(__dirname+'/404.html'));
}
var definitions = $(".lr_dct_ent.vmod.XpoqFe").eq(0);
var mainPart = definitions.first().find(".lr_dct_sf_h");
var origin = definitions.find(".vmod").first().next("div.xpdxpnd").find("div").last().text();
dictionary.origin = origin;
var meaning = {};
mainPart.each(function(i, element){
var type = $(this).text();
meaning[type] = [];
var alernaitions = $(this).next("div.xpdxpnd").first().text();
var selector = $(".lr_dct_sf_sens").eq(i).find("div[style='margin-left:20px'] > .PNlCoe");
selector.each(function(ia, element){
var newDefinition = {};
newDefinition.definition = $(this).find("div[data-dobid='dfn']").text();
var example = $(this).find("span.vmod .vk_gy").text();
var synonymsText = $(this).find("div.vmod td.lr_dct_nyms_ttl + td > span:not([data-log-string='synonyms-more-click'])").text();
var synonyms = synonymsText.split(/,|;/).filter(synonym => synonym!= ' ' && synonym).map(function(item) {
return item.trim();
});
var antonyms = antonymsText.split(/,|;/).filter(antonyms => antonyms!= ' ' && antonyms).map(function(item) {
return item.trim();
});
if(example.length > 0)
newDefinition.example = example.substring(1, example.length - 1);
if(synonyms.length > 0)
newDefinition.synonyms = synonyms;
if(antonyms.length > 0)
newDefinition.antonyms = antonyms;
if(alernaitions.length > 0){
if(ia == 0){
var pts = alernaitions.split("; ");
var alts = [];
pts.forEach(function (ent){
pt = ent.split(": ")
alts.push([pt[0],pt[1]]);
});
newDefinition = extend({"alernaitions":alts}, newDefinition);
meaning[type].push(newDefinition);
}else{
meaning[type].push(newDefinition);
}
}
});
}) ;
dictionary.meaning = meaning;
//console.log(dictionary.noun[0]);
res.header("Content-Type",'application/json');
res.header("Access-Control-Allow-Origin", "*");
res.send(JSON.stringify(dictionary, null, 4));
});
}
});
app.listen(process.env.PORT, process.env.IP, function(){
console.log("I am listening...");
});