-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_helper.js
49 lines (45 loc) · 1.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
const NodeHelper = require("node_helper");
const cheerio = require("cheerio");
const axios = require("axios");
module.exports = NodeHelper.create({
start: function() {
console.log("Starting node helper for: " + this.name);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "GET_RUNNING_FORECAST") {
this.getRunningForecast(payload);
}
},
breakIntoLines: function(text, maxLength) {
let result = "";
while (text.length > 0) {
if (text.length > maxLength) {
let splitIndex = text.lastIndexOf(" ", maxLength);
if (splitIndex === -1) splitIndex = maxLength;
result += text.substring(0, splitIndex) + "<br>";
text = text.substring(splitIndex).trim();
} else {
result += text;
break;
}
}
return result;
},
getRunningForecast: function(url) {
var self = this;
axios.get(url)
.then(response => {
const $ = cheerio.load(response.data);
let runningForecast = ""; // Extract necessary data from the webpage
// Adjust this selector based on the structure of the page
$("p.ActivitiesHubForecastBarGraph--subHeading--pib24").each((i, element) => {
let text = $(element).text();
runningForecast += "<div class='running-forecast-item'>" + self.breakIntoLines(text, 30) + "</div><br/>";
});
self.sendSocketNotification("RUNNING_FORECAST_DATA", runningForecast);
})
.catch(error => {
console.error("Error fetching running forecast: ", error);
});
}
});