2016-03-30 14:49:37 +02:00
|
|
|
/* Magic Mirror
|
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");
|
2021-03-02 00:17:13 +01:00
|
|
|
const fetch = require("node-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-04-03 19:52:13 +02:00
|
|
|
|
2016-03-30 14:49:37 +02:00
|
|
|
/* private methods */
|
|
|
|
|
2020-08-03 11:36:29 +02:00
|
|
|
/**
|
2016-04-12 11:06:59 +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;
|
2016-04-12 11:06:59 +02:00
|
|
|
items = [];
|
|
|
|
|
2020-07-12 12:45:32 +02:00
|
|
|
const parser = new FeedMe();
|
2016-04-12 11:06:59 +02:00
|
|
|
|
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 || "";
|
2016-05-10 13:35:13 +02:00
|
|
|
|
2016-05-12 12:30:45 +02:00
|
|
|
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, "");
|
2016-05-10 13:35:13 +02:00
|
|
|
|
2016-04-20 14:39:38 +02:00
|
|
|
items.push({
|
2016-05-10 13:35:13 +02:00
|
|
|
title: title,
|
2016-04-20 14:39:38 +02:00
|
|
|
description: description,
|
2016-05-10 13:35:13 +02:00
|
|
|
pubdate: pubdate,
|
2020-05-11 22:22:32 +02:00
|
|
|
url: url
|
2016-04-20 14:39:38 +02:00
|
|
|
});
|
2018-06-26 20:01:28 -04:00
|
|
|
} 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);
|
2016-04-20 14:39:38 +02:00
|
|
|
}
|
2016-04-12 11:06:59 +02:00
|
|
|
});
|
|
|
|
|
2021-03-16 21:15:19 +01:00
|
|
|
parser.on("end", () => {
|
|
|
|
this.broadcastItems();
|
2016-03-30 14:49:37 +02:00
|
|
|
scheduleTimer();
|
2016-04-12 11:06:59 +02:00
|
|
|
});
|
|
|
|
|
2021-03-16 21:15:19 +01:00
|
|
|
parser.on("error", (error) => {
|
|
|
|
fetchFailedCallback(this, error);
|
2016-03-30 14:49:37 +02:00
|
|
|
scheduleTimer();
|
2016-04-12 11:06:59 +02:00
|
|
|
});
|
|
|
|
|
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 = {
|
2021-12-13 21:24:37 +01:00
|
|
|
"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)
|
2021-03-28 14:17:03 +02:00
|
|
|
.then((response) => {
|
|
|
|
response.body.pipe(iconv.decodeStream(encoding)).pipe(parser);
|
|
|
|
})
|
2021-03-02 21:26:25 +01:00
|
|
|
.catch((error) => {
|
2021-05-02 10:24:22 +02:00
|
|
|
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);
|
2020-05-11 22:22:32 +02:00
|
|
|
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
|
|
|
*/
|
2020-05-11 22:22:32 +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();
|
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
this.startFetch = function () {
|
2016-03-30 14:49:37 +02:00
|
|
|
fetchNews();
|
|
|
|
};
|
|
|
|
|
2020-08-03 11:36:29 +02:00
|
|
|
/**
|
2016-06-04 20:32:55 -06:00
|
|
|
* Broadcast the existing items.
|
2016-03-30 14:49:37 +02:00
|
|
|
*/
|
2020-05-11 22:22:32 +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
|
|
|
};
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
this.onReceive = function (callback) {
|
2016-03-30 14:49:37 +02:00
|
|
|
itemsReceivedCallback = callback;
|
|
|
|
};
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
this.onError = function (callback) {
|
2016-03-30 14:49:37 +02:00
|
|
|
fetchFailedCallback = callback;
|
|
|
|
};
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
this.url = function () {
|
2016-03-30 14:49:37 +02:00
|
|
|
return url;
|
|
|
|
};
|
|
|
|
|
2020-05-11 22:22:32 +02:00
|
|
|
this.items = function () {
|
2016-03-30 14:49:37 +02:00
|
|
|
return items;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-07-12 08:10:43 +02:00
|
|
|
module.exports = NewsfeedFetcher;
|