-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
131 lines (108 loc) · 4.39 KB
/
node_helper.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
var NodeHelper = require('node_helper');
const axios = require('axios');
var cheerio = require('cheerio');
module.exports = NodeHelper.create({
requiresVersion: '2.22.0',
start: function () {
console.log("Starting node_helper for: " + this.name);
},
getData: function ($) {
let lement = {};
// Get the data-wordday attribute from the #word_page element
const wordDayData = $("#word_page").attr("data-wordday");
if (wordDayData) {
// Parse the JSON string into an object
const wordDayObj = JSON.parse(wordDayData);
// Extract the required fields
lement.text = wordDayObj.text || null;
lement.english = wordDayObj.english || null;
lement.meaning = wordDayObj.meaning || null;
// Get the first two samples, if available
if (wordDayObj.samples && wordDayObj.samples.length >= 2) {
lement.samples = wordDayObj.samples.slice(0, 2).map(sample => ({
text: sample.text,
english: sample.english,
audio: sample.audio_english
}));
} else {
lement.samples = []; // Handle cases where there are no or less than 2 samples
}
} else {
console.error("No data-wordday attribute found on #word_page.");
}
//console.log("Extracted Data:", lement);
return lement;
},
getWotdData: async function (languages) {
var lang = "";
const promises = languages.map(language => {
if (language === "english") {
lang = language + "class101"
return axios.get(`https://www.${lang}.com/${language}-phrases/`).then(({
data
}) => {
const $ = cheerio.load(data, null, true);
// get the data
const english = this.getData($);
return {
"language": language,
"word": english.text,
"translation": english.english,
"examples": {
"wordex": "",
"wordextr": english.samples[0].text,
"wordextr2": english.samples[1].text
}
}
})
} else {
lang = language + "pod101"
return axios.get(`https://www.${lang}.com/${language}-phrases/`).then(({
data
}) => {
const $ = cheerio.load(data, null, true);
// get the data
const english = this.getData($);
return {
"language": language,
"word": english.text,
"translation": english.english,
"examples": {
"wordex": english.meaning,
"wordextr": english.samples[0].text,
"wordextr2": english.samples[0].english
}
}
})
}
})
const results = await Promise.all(promises);
const combinedResults = {};
results.forEach(result => {
if (!combinedResults[result.language]) {
combinedResults[result.language] = {
"language": result.language,
"data": []
}
}
combinedResults[result.language].data.push(result);
})
return Object.values(combinedResults);
},
fetchData: function (languages) {
mydata = [];
mydata = this.getWotdData(languages)
.then((result) => {
result.forEach(languageResult => {})
var self = this;
this.sendSocketNotification("WOTD_DATA", result)
})
.catch((error) => {
console.error(error)
})
},
socketNotificationReceived: function (notification, payload) {
var languages = payload.language
this.fetchData(languages)
}
})