MagicMirror/modules/default/newsfeed/newsfeedfetcher.js

151 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-03-30 14:49:37 +02:00
/* Magic Mirror
* Fetcher
*
2020-04-28 23:05:28 +02:00
* By Michael Teeuw https://michaelteeuw.nl
2016-03-30 14:49:37 +02:00
* MIT Licensed.
*/
2020-05-31 22:12:26 +02:00
const Log = require("../../../js/logger.js");
const FeedMe = require("feedme");
const request = require("request");
const iconv = require("iconv-lite");
2016-03-30 14:49:37 +02:00
/* Fetcher
* Responsible for requesting an update on the set interval and broadcasting the data.
*
* attribute url string - URL of the news feed.
* attribute reloadInterval number - Reload interval in milliseconds.
* attribute logFeedWarnings boolean - Log warnings when there is an error parsing a news article.
2016-03-30 14:49:37 +02:00
*/
2020-07-12 08:10:43 +02:00
var NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings) {
2016-03-30 14:49:37 +02:00
var self = this;
if (reloadInterval < 1000) {
reloadInterval = 1000;
}
2016-03-30 14:49:37 +02:00
var reloadTimer = null;
var items = [];
var fetchFailedCallback = function () {};
var itemsReceivedCallback = function () {};
2016-03-30 14:49:37 +02:00
/* private methods */
/* fetchNews()
* Request the new items.
2016-03-30 14:49:37 +02:00
*/
var fetchNews = function () {
2016-03-30 14:49:37 +02:00
clearTimeout(reloadTimer);
reloadTimer = null;
items = [];
var parser = new FeedMe();
parser.on("item", function (item) {
var title = item.title;
var description = item.description || item.summary || item.content || "";
2018-03-21 11:58:09 +01:00
var pubdate = item.pubdate || item.published || item.updated || item["dc:date"];
var url = item.url || item.link || "";
if (title && pubdate) {
var 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);
}
});
parser.on("end", function () {
2016-03-30 14:49:37 +02:00
self.broadcastItems();
scheduleTimer();
});
parser.on("error", function (error) {
2016-03-30 14:49:37 +02:00
fetchFailedCallback(self, error);
scheduleTimer();
});
2020-05-02 10:39:09 +02:00
var nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
var headers = { "User-Agent": "Mozilla/5.0 (Node.js " + nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)", "Cache-Control": "max-age=0, no-cache, no-store, must-revalidate", Pragma: "no-cache" };
request({ uri: url, encoding: null, headers: headers })
.on("error", function (error) {
2017-01-02 21:31:21 +00:00
fetchFailedCallback(self, error);
scheduleTimer();
})
.pipe(iconv.decodeStream(encoding))
.pipe(parser);
2016-03-30 14:49:37 +02:00
};
/* scheduleTimer()
* Schedule the timer for the next update.
*/
var 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 */
/* setReloadInterval()
* Update the reload interval, but only if we need to increase the speed.
*
* attribute interval number - Interval for the update in milliseconds.
*/
this.setReloadInterval = function (interval) {
2016-03-30 14:49:37 +02:00
if (interval > 1000 && interval < reloadInterval) {
reloadInterval = interval;
}
};
/* startFetch()
* Initiate fetchNews();
*/
this.startFetch = function () {
2016-03-30 14:49:37 +02:00
fetchNews();
};
/* broadcastItems()
* 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.");
2016-03-30 14:49:37 +02:00
itemsReceivedCallback(self);
};
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;