MagicMirror/modules/default/newsfeed/newsfeedfetcher.js

158 lines
3.7 KiB
JavaScript
Raw Normal View History

/* MagicMirror²
2020-07-12 12:45:32 +02:00
* Node Helper: Newsfeed - NewsfeedFetcher
2016-03-30 14:49:37 +02:00
*
2020-04-28 23:05:28 +02:00
* By Michael Teeuw https://michaelteeuw.nl
2016-03-30 14:49:37 +02:00
* MIT Licensed.
*/
2021-02-18 19:14:53 +01:00
const Log = require("logger");
2020-05-31 22:12:26 +02:00
const FeedMe = require("feedme");
2021-04-24 09:14:08 +02:00
const NodeHelper = require("node_helper");
const fetch = require("fetch");
2020-05-31 22:12:26 +02:00
const iconv = require("iconv-lite");
2016-03-30 14:49:37 +02:00
2020-08-03 11:36:29 +02:00
/**
2016-03-30 14:49:37 +02:00
* Responsible for requesting an update on the set interval and broadcasting the data.
*
2020-08-03 11:36:29 +02:00
* @param {string} url URL of the news feed.
* @param {number} reloadInterval Reload interval in milliseconds.
* @param {string} encoding Encoding of the feed.
* @param {boolean} logFeedWarnings If true log warnings when there is an error parsing a news article.
* @class
2016-03-30 14:49:37 +02:00
*/
2020-07-12 12:45:32 +02:00
const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings) {
let reloadTimer = null;
let items = [];
let fetchFailedCallback = function () {};
let itemsReceivedCallback = function () {};
2016-03-30 14:49:37 +02:00
if (reloadInterval < 1000) {
reloadInterval = 1000;
}
2016-03-30 14:49:37 +02:00
/* private methods */
2020-08-03 11:36:29 +02:00
/**
* Request the new items.
2016-03-30 14:49:37 +02:00
*/
2021-03-16 21:15:19 +01:00
const fetchNews = () => {
2016-03-30 14:49:37 +02:00
clearTimeout(reloadTimer);
reloadTimer = null;
items = [];
2020-07-12 12:45:32 +02:00
const parser = new FeedMe();
2021-03-16 21:15:19 +01:00
parser.on("item", (item) => {
2020-07-12 12:45:32 +02:00
const title = item.title;
let description = item.description || item.summary || item.content || "";
const pubdate = item.pubdate || item.published || item.updated || item["dc:date"];
const url = item.url || item.link || "";
if (title && pubdate) {
2020-07-12 12:45:32 +02:00
const regex = /(<([^>]+)>)/gi;
2017-11-26 11:52:01 +01:00
description = description.toString().replace(regex, "");
items.push({
title: title,
description: description,
pubdate: pubdate,
url: url
});
} else if (logFeedWarnings) {
2020-06-01 16:40:20 +02:00
Log.warn("Can't parse feed item:");
Log.warn(item);
Log.warn("Title: " + title);
Log.warn("Description: " + description);
Log.warn("Pubdate: " + pubdate);
}
});
2021-03-16 21:15:19 +01:00
parser.on("end", () => {
this.broadcastItems();
2016-03-30 14:49:37 +02:00
scheduleTimer();
});
2021-03-16 21:15:19 +01:00
parser.on("error", (error) => {
fetchFailedCallback(this, error);
2016-03-30 14:49:37 +02:00
scheduleTimer();
});
2020-07-12 12:45:32 +02:00
const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
2021-03-02 00:17:13 +01:00
const headers = {
"User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version,
2021-03-02 00:17:13 +01:00
"Cache-Control": "max-age=0, no-cache, no-store, must-revalidate",
Pragma: "no-cache"
2020-07-12 12:45:32 +02:00
};
2021-03-02 00:17:13 +01:00
fetch(url, { headers: headers })
2021-04-24 09:14:08 +02:00
.then(NodeHelper.checkFetchStatus)
.then((response) => {
response.body.pipe(iconv.decodeStream(encoding)).pipe(parser);
})
2021-03-02 21:26:25 +01:00
.catch((error) => {
fetchFailedCallback(this, error);
2017-01-02 21:31:21 +00:00
scheduleTimer();
2021-03-02 00:17:13 +01:00
});
2016-03-30 14:49:37 +02:00
};
2020-08-03 11:36:29 +02:00
/**
2016-03-30 14:49:37 +02:00
* Schedule the timer for the next update.
*/
2020-07-12 12:45:32 +02:00
const scheduleTimer = function () {
2016-03-30 14:49:37 +02:00
clearTimeout(reloadTimer);
reloadTimer = setTimeout(function () {
2016-03-30 14:49:37 +02:00
fetchNews();
}, reloadInterval);
};
/* public methods */
2020-08-03 11:36:29 +02:00
/**
2016-03-30 14:49:37 +02:00
* Update the reload interval, but only if we need to increase the speed.
*
2020-08-03 11:36:29 +02:00
* @param {number} interval Interval for the update in milliseconds.
2016-03-30 14:49:37 +02:00
*/
this.setReloadInterval = function (interval) {
2016-03-30 14:49:37 +02:00
if (interval > 1000 && interval < reloadInterval) {
reloadInterval = interval;
}
};
2020-08-03 11:36:29 +02:00
/**
2016-03-30 14:49:37 +02:00
* Initiate fetchNews();
*/
this.startFetch = function () {
2016-03-30 14:49:37 +02:00
fetchNews();
};
2020-08-03 11:36:29 +02:00
/**
* Broadcast the existing items.
2016-03-30 14:49:37 +02:00
*/
this.broadcastItems = function () {
2016-03-30 14:49:37 +02:00
if (items.length <= 0) {
2020-06-01 16:40:20 +02:00
Log.info("Newsfeed-Fetcher: No items to broadcast yet.");
2016-03-30 14:49:37 +02:00
return;
}
2020-06-01 16:40:20 +02:00
Log.info("Newsfeed-Fetcher: Broadcasting " + items.length + " items.");
2021-03-16 21:15:19 +01:00
itemsReceivedCallback(this);
2016-03-30 14:49:37 +02:00
};
this.onReceive = function (callback) {
2016-03-30 14:49:37 +02:00
itemsReceivedCallback = callback;
};
this.onError = function (callback) {
2016-03-30 14:49:37 +02:00
fetchFailedCallback = callback;
};
this.url = function () {
2016-03-30 14:49:37 +02:00
return url;
};
this.items = function () {
2016-03-30 14:49:37 +02:00
return items;
};
};
2020-07-12 08:10:43 +02:00
module.exports = NewsfeedFetcher;