forked from meetDeveloper/freeDictionaryAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
117 lines (85 loc) · 3.91 KB
/
Copy pathapp.js
File metadata and controls
117 lines (85 loc) · 3.91 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
var express = require("express"),
app = express(),
cheerio = require("cheerio"),
request = require('request'),
path = require("path");
app.use(express.static('public'));
app.get("/", function(req, res){
if(!req.query.define){
res.sendFile(path.join(__dirname+'/views//index.html'));
} else {
if(encodeURIComponent(req.query.define).includes("%")){
console.log("yes");
res.header("Access-Control-Allow-Origin", "*");
return res.status(404);
}
var url = 'https://www.google.co.in/search?hl=en&q=defina+' + req.query.define;
if(req.query.lang){
url = url.replace('en', req.query.lang);
}
url = encodeURI(url);
request({
method: 'GET',
url: url,
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 = {};
//return res.send(body);
var $ = cheerio.load(body);
var word = $("div.dDoNo span").first().text();
//console.log(word);
if(word.length < 1){
res.header("Access-Control-Allow-Origin", "*");
return res.status(200).send(JSON.stringify({error: 404}));
}
dictionary.word = $("div.dDoNo span").first().text();
if(!$('.lr_dct_spkr.lr_dct_spkr_off audio')){
dictionary.pronunciation = "https:" + $('.lr_dct_spkr.lr_dct_spkr_off audio')[0].attribs.src;
dictionary.pronunciation = dictionary.pronunciation.replace('--_gb', '--_us');
}
dictionary.phonetic = [];
$(".lr_dct_ph.XpoqFe").first().find('span').each(function(i, element){
dictionary.phonetic.push($(this).text());
});
dictionary.meaning = {};
var definitions = $(".lr_dct_ent.vmod.XpoqFe");
var mainPart = definitions.first().find(".lr_dct_sf_h");
var meaning = {};
mainPart.each(function(i, element){
var type = $(this).find('i').text();
meaning[type] = [];
var selector = $(".lr_dct_sf_sens").eq(i).find("div[style='margin-left:20px'] > .PNlCoe");
selector.each(function(i, 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();
});
if(example.length > 0)
newDefinition.example = example.replace(/(^")|("$)/g, '');
if(synonyms.length > 0)
newDefinition.synonyms = synonyms;
meaning[type].push(newDefinition);
});
}) ;
dictionary.meaning = meaning;
Object.keys(dictionary).forEach(key => {(Array.isArray(dictionary[key]) && !dictionary[key].length) && delete dictionary[key]})
res.header("Content-Type",'application/json');
res.header("Access-Control-Allow-Origin", "*");
res.send(JSON.stringify(dictionary, null, 4));
});
}
});
// app.get("/languageCode.txt", function(req, res){
// res.sendFile(__dirname + "/languageCode.txt");
// })
app.listen(3000, 'localhost', function(){
console.log("I am listening in port 3000...");
});