const jsonFile = require('jsonfile'); const Parser = require('rss-parser'); class ListenerRss { name = undefined; address = undefined; timeloop = 5 * 60; // time in seconds customfields = []; // private fields parser = null; obj = null; loopRunning = false; constructor(info) { // set parser this.parser = new Parser({ customFields : { item: info._customfields.map((elt) => { return Array.isArray(elt[1]) ? elt[1][0] : elt[1]; }) } }); // Set data this.name = info._name === undefined ? info._address : info._name; // if name is undefined let's take the address this.address = info._address; this.timeloop = info._timeloop; this.customfields = info._customfields; } fetchRSS() { return this.parser.parseURL(this.address) .catch((err) => { throw new Error('bad address or no access : ' + err);}); } /** * @brief call the callback function each looptime * @param callback function who's going to be called with the latest get */ start(callback) { this.loopRunning = true; (async () => { while(this.loopRunning === true) { callback(await this.fetchRSS()); await new Promise(res => setTimeout(res, this.timeloop * 1000)); } })(); } /** * @brief stop the async function */ stop() { this.loopRunning = false; } } module.exports = ListenerRss