node_helper. But need to find different approach.
const NodeHelper = require("node_helper");
const cheerio = require("cheerio");
const request = require('request');
var self;
module.exports = NodeHelper.create({
start: function () {
self = this;
console.log("Starting node_helper for: " + this.name);
},
getData: function () {
const url = 'https://wol.jw.org/hr/wol/h/r19/lp-c';
request({
url: url,
method: 'GET'
}, (error, response, body) => {
if (error) {
return console.error("ERROR: ",error)
}
if (!error && response.statusCode == 200) {
var $ = cheerio.load(body);
//console.log($.html());
const data = $('div[class="tabContent"]').first();//
let header = $(data).find('h2').text();
let title = $(data).find('p').first().text();
let textData = $(data).find('div.pGroup>p').text();
//console.log(header,title,text);
var recivedData = {
header,
title,
textData
}
//console.log('recivedData: ',recivedData);
self.sendSocketNotification('TEXT_RESULT', recivedData);
}
});
},
socketNotificationReceived: function (notification, payload) {
var self = this;
if (notification === 'GET_TEXT_DATA') {
//console.log(notification,payload);
self.getData();
}
},
});
MMM-Dnevni_citat.js. getDom need some styling.
Module.register("MMM-Dnevni_citat", {
defaults: {
updateInterval: 10 * 60 * 1000, // minute ,seconds ,milliseconds
//retryDelay: 5000 // not needed for now
},
start: function () {
Log.info("Starting module: " + this.name);
requiresVersion: "2.1.0";
this.loaded = false;
this.scheduleUpdate();
},
scheduleUpdate: function () {
setInterval(() => {
this.getData();
}, this.config.updateInterval);
this.getData();
},
getData: function () {
console.log(' GET_TEXT_DATA ', this.config)
this.sendSocketNotification('GET_TEXT_DATA', this.config);
},
socketNotificationReceived: function (notification, payload) {
if (notification === "TEXT_RESULT") {
this.textDataRecived = payload;
console.log(this.textDataRecived);
this.loaded = true;
}
this.updateDom();
},
getDom: function () {
var wrapper = document.createElement("div");
if (!this.loaded) {
wrapper.innerHTML = "LOADING";
return wrapper;
}
if (this.loaded) {
var header = document.createElement("header");
header.innerHTML = this.textDataRecived.header;
wrapper.appendChild(header);
var table = document.createElement('tr');
table.classList.add("xsmall", "normal");
var title = document.createElement('tr');
title.innerHTML = this.textDataRecived.title;
table.appendChild(title);
var textData = document.createElement('tr');
textData.innerHTML = this.textDataRecived.textData;
table.appendChild(textData);
table.appendChild(title);
wrapper.appendChild(table);
}
return wrapper;
},
});
config.js
{ module: "MMM-Dnevni_citat",
position: "middle_center"
},