2016-03-30 14:49:37 +02:00
|
|
|
/* Magic Mirror
|
|
|
|
* Fetcher
|
|
|
|
*
|
|
|
|
* By Michael Teeuw http://michaelteeuw.nl
|
|
|
|
* MIT Licensed.
|
|
|
|
*/
|
|
|
|
|
2016-04-12 11:06:59 +02:00
|
|
|
var FeedMe = require("feedme");
|
|
|
|
var request = require("request");
|
|
|
|
var 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.
|
|
|
|
*/
|
|
|
|
|
2016-04-05 13:16:23 +02:00
|
|
|
var Fetcher = function(url, reloadInterval, encoding) {
|
2016-03-30 14:49:37 +02:00
|
|
|
var self = this;
|
|
|
|
if (reloadInterval < 1000) {
|
|
|
|
reloadInterval = 1000;
|
|
|
|
}
|
2016-04-03 19:52:13 +02:00
|
|
|
|
2016-03-30 14:49:37 +02:00
|
|
|
var reloadTimer = null;
|
|
|
|
var items = [];
|
|
|
|
|
|
|
|
var fetchFailedCallback = function() {};
|
|
|
|
var itemsReceivedCallback = function() {};
|
|
|
|
|
|
|
|
/* private methods */
|
|
|
|
|
|
|
|
/* fetchNews()
|
2016-04-12 11:06:59 +02:00
|
|
|
* Request the new items.
|
2016-03-30 14:49:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
var fetchNews = function() {
|
|
|
|
clearTimeout(reloadTimer);
|
|
|
|
reloadTimer = null;
|
2016-04-12 11:06:59 +02:00
|
|
|
items = [];
|
|
|
|
|
|
|
|
var parser = new FeedMe();
|
|
|
|
|
|
|
|
parser.on("item", function(item) {
|
|
|
|
|
2016-05-10 13:35:13 +02:00
|
|
|
var title = item.title;
|
2016-12-01 19:48:53 -03:00
|
|
|
var description = item.description || item.summary || item.content || "";
|
2016-05-10 13:35:13 +02:00
|
|
|
var pubdate = item.pubdate || item.published || item.updated;
|
2016-12-01 19:48:53 -03:00
|
|
|
var url = item.url || item.link || "";
|
2016-05-10 13:35:13 +02:00
|
|
|
|
2016-05-12 12:30:45 +02:00
|
|
|
if (title && pubdate) {
|
2016-05-10 13:35:13 +02:00
|
|
|
|
|
|
|
var regex = /(<([^>]+)>)/ig;
|
|
|
|
description = description.replace(regex, "");
|
|
|
|
|
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,
|
2016-11-07 17:49:32 +01:00
|
|
|
url: url,
|
2016-04-20 14:39:38 +02:00
|
|
|
});
|
2016-05-10 13:35:13 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2016-10-14 14:19:03 +02:00
|
|
|
// console.log("Can't parse feed item:");
|
|
|
|
// console.log(item);
|
|
|
|
// console.log('Title: ' + title);
|
|
|
|
// console.log('Description: ' + description);
|
|
|
|
// console.log('Pubdate: ' + pubdate);
|
2016-05-12 12:30:45 +02:00
|
|
|
|
2016-04-20 14:39:38 +02:00
|
|
|
}
|
2016-04-12 11:06:59 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
parser.on("end", function() {
|
2016-03-30 14:49:37 +02:00
|
|
|
self.broadcastItems();
|
|
|
|
scheduleTimer();
|
2016-04-12 11:06:59 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
parser.on("error", function(error) {
|
2016-03-30 14:49:37 +02:00
|
|
|
fetchFailedCallback(self, error);
|
|
|
|
scheduleTimer();
|
2016-04-12 11:06:59 +02:00
|
|
|
});
|
|
|
|
|
2016-12-31 21:16:09 +01:00
|
|
|
|
2016-12-08 01:29:55 -03:00
|
|
|
nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
|
|
|
|
headers = {"User-Agent": "Mozilla/5.0 (Node.js "+ nodeVersion + ") MagicMirror/" + global.version + " (https://github.com/MichMich/MagicMirror/)"}
|
|
|
|
|
2017-01-02 21:31:21 +00:00
|
|
|
request({uri: url, encoding: null, headers: headers})
|
|
|
|
.on("error", function(error) {
|
|
|
|
fetchFailedCallback(self, error);
|
|
|
|
scheduleTimer();
|
|
|
|
})
|
|
|
|
.pipe(iconv.decodeStream(encoding)).pipe(parser);
|
2016-04-12 11:06:59 +02:00
|
|
|
|
2016-03-30 14:49:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* scheduleTimer()
|
|
|
|
* Schedule the timer for the next update.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var scheduleTimer = function() {
|
|
|
|
//console.log('Schedule update timer.');
|
|
|
|
clearTimeout(reloadTimer);
|
|
|
|
reloadTimer = setTimeout(function() {
|
|
|
|
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) {
|
|
|
|
if (interval > 1000 && interval < reloadInterval) {
|
|
|
|
reloadInterval = interval;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* startFetch()
|
|
|
|
* Initiate fetchNews();
|
|
|
|
*/
|
|
|
|
this.startFetch = function() {
|
|
|
|
fetchNews();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* broadcastItems()
|
2016-06-04 20:32:55 -06:00
|
|
|
* Broadcast the existing items.
|
2016-03-30 14:49:37 +02:00
|
|
|
*/
|
|
|
|
this.broadcastItems = function() {
|
|
|
|
if (items.length <= 0) {
|
|
|
|
//console.log('No items to broadcast yet.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//console.log('Broadcasting ' + items.length + ' items.');
|
|
|
|
itemsReceivedCallback(self);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onReceive = function(callback) {
|
|
|
|
itemsReceivedCallback = callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.onError = function(callback) {
|
|
|
|
fetchFailedCallback = callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.url = function() {
|
|
|
|
return url;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.items = function() {
|
|
|
|
return items;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Fetcher;
|