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
152 lines (109 loc) · 5.73 KB
/
app.js
File metadata and controls
152 lines (109 loc) · 5.73 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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).sendFile(path.join(__dirname+'/views/404.html'));
}
var url = 'https://en.oxforddictionaries.com/search?filter=noad&query=' + req.query.define;
// if(req.query.lang){
// url = url.replace('en', req.query.lang);
// if(req.query.lang === "hi"){
// url = url.replace('define', 'matlab');
// }
// }
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 $ = cheerio.load(body);
if(!($(".hwg .hw").first()[0])){
console.log($(".searchHeading").first().text());
console.log(req.query.define + " is not present in Dictionary.");
res.header("Access-Control-Allow-Origin", "*");
return res.status(404).sendFile(path.join(__dirname+'/views/404.html'));
}
var dictionary = [];
var i,j = 0;
var entryHead = $(".entryHead.primary_homograph");
var array = [];
var entriesOfFirstEntryHead = $("#" + entryHead[0].attribs.id + " ~ .gramb").length;
//console.log(entriesOfFirstEntryHead);
for(i = 0; i < entryHead.length; i++){
array[i] = entriesOfFirstEntryHead - $("#" + entryHead[i].attribs.id + " ~ .gramb").length;
}
array[i] = entriesOfFirstEntryHead;
//console.log(array);
var grambs = $("section.gramb");
var numberOfentryGroup = array.length - 1;
for(i = 0; i < numberOfentryGroup; i++){
var entry = {};
var word = $(".hwg .hw")[i].childNodes[0].nodeValue;
entry.word = word;
//console.log(entry.word);
var phonetic = $(".pronSection.etym .pron .phoneticspelling")[i];
if(phonetic){
entry.phonetic = phonetic.childNodes[0].data;
}
entry.meaning = {};
//var numberOfGrambs = array[i + 1] - array[i];
var start = array[i];
var end = array[i + 1];
for(j = start; j < end; j++){
var partofspeech = $(grambs[j]).find(".ps.pos .pos").text();
$(grambs[j]).find(".semb").each(function(j, element){
var meaningArray = [];
$(element).find("> li").each(function(j, element){
var item = $(element).find("> .trg");
var definition = $(item).find(" > p > .ind").text();
if(definition.length === 0){
definition = $(item).find(".crossReference").first().text();
}
var example = $(item).find(" > .exg > .ex > em").first().text();
var synonymsText = $(item).find(" > .synonyms > .exg > .exs").first().text();
var synonyms = synonymsText.split(/,|;/).filter(synonym => synonym!= ' ' && synonym).map(function(item) {
return item.trim();
});
var newDefinition = {};
if(definition.length > 0)
newDefinition.definition = definition;
if(example.length > 0)
newDefinition.example = example.substring(1, example.length - 1);
if(synonyms.length > 0)
newDefinition.synonyms = synonyms;
meaningArray.push(newDefinition);
});
if(partofspeech.length === 0)
partofspeech = "crossReference";
entry.meaning[partofspeech] = meaningArray.slice();
});
}
dictionary.push(entry);
}
Object.keys(dictionary).forEach(key => {(Array.isArray(dictionary[key]) && !dictionary[key].length) && delete dictionary[key]});
if($(".hwg .hw").first()[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...");
});