-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_helper.js
executable file
·43 lines (37 loc) · 1.42 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
const NodeHelper = require("node_helper");
const https = require("https");
const cheerio = require("cheerio");
console.log("MMM-Scrapey node helper is starting...");
module.exports = NodeHelper.create({
start: function () {
console.log("Starting node helper for module: " + this.name);
},
socketNotificationReceived: function (notification, payload) {
if (notification === "FETCH_SCRAPE_DATA") {
this.fetchData(payload.url, payload.cssSelector, payload.instanceId);
}
},
fetchData: function (scrapeURL, cssSelector, instanceId) {
fetch(scrapeURL)
.then((response) => response.text())
.then((body) => {
const $ = cheerio.load(body);
const scrapedData = $(cssSelector).html();
if (!scrapedData) {
console.error(cssSelector + " not found in HTML.");
this.sendSocketNotification("SCRAPE_DATA", {
instanceId: instanceId,
data: [["Scrape target not found"]]
});
return;
}
this.sendSocketNotification("SCRAPE_DATA", {
instanceId: instanceId,
data: scrapedData
});
})
.catch((error) => {
console.error("Error fetching data: ", error);
});
}
});