-
Notifications
You must be signed in to change notification settings - Fork 15
/
data.js
210 lines (189 loc) · 6.02 KB
/
data.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
const cheerio = require("cheerio");
const request = require("request");
const express = require("express");
const axios = require("axios");
const app = express();
const cors = require("cors");
const fetchVerbs = (wiki) => {
return new Promise((resolve, reject) => {
axios
.get(wiki)
.then((response) => {
const $$ = cheerio.load(response.data);
const verb = $$("tr > td > p ").text();
const lines = verb
.split("\n")
.map((line) => line.trim())
.filter(Boolean);
const verbs = [];
for (let i = 0; i < lines.length; i += 2) {
if (verbs.includes({ type: lines[i], text: lines[i + 1] })) {
break;
}
const type = lines[i];
const text = lines[i + 1];
if (type && text) {
verbs.push({ id: verbs.length, type, text });
} else {
verbs.push();
}
}
resolve(verbs);
})
.catch((error) => {
resolve();
});
});
};
app.use(cors({ origin: "*" }));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.get("/api/dictionary/:language/:entry", (req, res, next) => {
const entry = req.params.entry;
const slugLanguage = req.params.language;
let nation = "us";
if (slugLanguage === "en") {
language = "english";
} else if (slugLanguage === "uk") {
language = "english";
nation = "uk";
} else if (slugLanguage === "en-tw") {
language = "english-chinese-traditional";
} else if (slugLanguage === "en-cn") {
language = "english-chinese-simplified";
}
const url = `https://dictionary.cambridge.org/${nation}/dictionary/${language}/${entry}`;
request(url, async (error, response, html) => {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html);
const siteurl = "https://dictionary.cambridge.org";
const wiki = `https://simple.wiktionary.org/wiki/${entry}`;
// get verbs
const verbs = await fetchVerbs(wiki);
// basic
const word = $(".hw.dhw").first().text();
const getPos = $(".pos.dpos") // part of speech
.map((index, element) => {
return $(element).text();
})
.get();
const pos = getPos.filter(
(item, index) => getPos.indexOf(item) === index,
);
// Phonetics audios
const audio = [];
for (const s of $(".pos-header.dpos-h")) {
const posNode = s.childNodes.find(
(c) =>
c.attribs && c.attribs.class && c.attribs.class.includes("dpos-g"),
);
if (!posNode || posNode.childNodes.length === 0) continue;
const p = $(posNode.childNodes[0]).text();
const nodes = s.childNodes.filter(
(c) =>
c.name === "span" &&
c.attribs &&
c.attribs.class &&
c.attribs.class.includes("dpron-i"),
);
if (nodes.length === 0) continue;
for (const node of nodes) {
if (node.childNodes.length < 3) continue;
const lang = $(node.childNodes[0]).text();
const aud = node.childNodes[1].childNodes.find(
(c) => c.name === "audio",
);
if (!aud) continue;
const src = aud.childNodes.find((c) => c.name === "source");
if (!src) continue;
const url = siteurl + $(src).attr("src");
const pron = $(node.childNodes[2]).text();
audio.push({ pos: p, lang: lang, url: url, pron: pron });
}
}
// definition & example
const exampleCount = $(".def-body.ddef_b")
.map((index, element) => {
const exampleElements = $(element).find(".examp.dexamp");
return exampleElements.length;
})
.get();
for (let i = 0; i < exampleCount.length; i++) {
if (i == 0) {
exampleCount[i] = exampleCount[i];
} else {
exampleCount[i] = exampleCount[i] + exampleCount[i - 1];
}
}
const exampletrans = $(
".examp.dexamp > .trans.dtrans.dtrans-se.hdb.break-cj",
); // translation of the example
const example = $(".examp.dexamp > .eg.deg")
.map((index, element) => {
return {
id: index,
text: $(element).text(),
translation: exampletrans.eq(index).text(),
};
})
.get();
const source = (element) => {
const defElement = $(element);
const parentElement = defElement.closest(".pr.dictionary");
const dataId = parentElement.attr("data-id");
return dataId;
};
const defPos = (element) => {
const defElement = $(element);
const partOfSpeech = defElement
.closest(".pr.entry-body__el")
.find(".pos.dpos")
.first()
.text(); // Get the part of speech
return partOfSpeech;
};
const getExample = (element) => {
const ex = $(element)
.find(".def-body.ddef_b > .examp.dexamp")
.map((index, element) => {
return {
id: index,
text: $(element).find(".eg.deg").text(),
translation: $(element).find(".trans.dtrans").text(),
};
});
return ex.get();
};
const definition = $(".def-block.ddef_block")
.map((index, element) => {
return {
id: index,
pos: defPos(element),
source: source(element),
text: $(element).find(".def.ddef_d.db").text(),
translation: $(element)
.find(".def-body.ddef_b > span.trans.dtrans")
.text(),
example: getExample(element),
};
})
.get();
// api response
if (word === "") {
res.status(404).json({
error: "word not found",
});
} else {
res.status(200).json({
word: word,
pos: pos,
verbs: verbs,
pronunciation: audio,
definition: definition,
});
}
}
});
});
module.exports = app;