-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
100 lines (83 loc) · 2.83 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
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);
},
getWordOfTheDay: function ($) {
let element = {}, todayWord = [];
$(".r101-wotd-widget__word").each((index, p) => {
element.id = index;
element.word = p.firstChild.data;
todayWord.push(element.word);
})
return todayWord;
},
getEngData: function ($) {
let lement = {}, englishWords = [];
$(".r101-wotd-widget__english").each((index, p) => {
lement.id = index;
lement.word = p.firstChild.data;
englishWords.push(lement.word);
})
return englishWords;
},
getWotdData: async function (languages) {
var lang = "";
const promises = languages.map(language => {
if(language === "english") {
lang = language + "class101"
} else {
lang = language + "pod101"
}
return axios.get(`https://www.${lang}.com/${language}-phrases/`).then(({data}) => {
const $ = cheerio.load(data, null, true);
// get the word of the day
const wordOftheDay = this.getWordOfTheDay($);
// get the english translation
const english = this.getEngData($);
return {
"language": language,
"word": wordOftheDay[0],
"translation": english[0],
"examples": {
"wordex": wordOftheDay[1],
"wordextr": english[1],
"wordextr2": english[2]
}
}
})
})
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)
}
})