-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
87 lines (71 loc) · 2.5 KB
/
main.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
"use strict"
const cheerio = require('cheerio')
const axios = require('axios').default
class Babla {
/** CONSTANT MESSAGE VARIABLES FOR ERROR */
#CANNOT_FIND_WORD; #PHRASE_NOT_FOUND;
#baseUrl; #data;
constructor() {
this.#baseUrl = 'https://www.babla.vn/dong-tu/tieng-duc/'
this.#data = {}
this.#CANNOT_FIND_WORD = 'Wir können nicht das Wort finden!'
this.#PHRASE_NOT_FOUND = 'This phrase cannot be found!'
}
#t(title) {
return title.text().toLocaleLowerCase()
}
async phasesOfVerben(verben, specifiedPhase = '*') {
let tmp = specifiedPhase.toLocaleLowerCase().trim();
const response = await axios.get(`${this.#baseUrl}${verben}`)
if (response.status !== 200) {
return {}
}
const $ = cheerio.load(response.data);
try {
await $('.conj-tense-wrapper').each(async (_, parent) => {
$(parent).children('.conj-block.container.result-block').each(async (_, el) => {
let bro = $(parent).children('.conj-tense-block')
let cousinTitle = this.#t($(el))
this.#data[cousinTitle] = {}
await bro.each(async (_, brother) => {
let brotherTitle = this.#t($(brother).children('.conj-tense-block-header')).replace(cousinTitle, '').trim()
if (brotherTitle === '') {
this.#data[cousinTitle] = {}
} else {
this.#data[cousinTitle][brotherTitle] = {}
}
await bro
.children('.conj-item')
.each((_, kleine) => {
let nameKleine = this.#t($(kleine).children('.conj-person'))
let valueKleine = this.#t($(kleine).children('.conj-result'))
if (brotherTitle === '') {
this.#data[cousinTitle][nameKleine] = valueKleine
} else {
if (nameKleine === '') {
this.#data[cousinTitle][brotherTitle] = valueKleine
} else {
this.#data[cousinTitle][brotherTitle][nameKleine] = valueKleine
}
}
})
})
})
})
} catch (error) {
return this.#CANNOT_FIND_WORD
}
if (tmp === '*') {
return JSON.stringify(this.#data, null, 2)
} else {
let isIncluded = Object.keys(this.#data).indexOf(tmp) !== -1
if (isIncluded) {
return JSON.stringify({
[tmp]: this.#data[tmp]
}, null, 2)
}
return this.#PHRASE_NOT_FOUND
}
}
}
module.exports = new Babla()