2015-06-13 19:46:24 +02:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var FeedParser = require("feedparser");
|
|
|
|
var request = require("request");
|
2015-10-11 22:05:55 +02:00
|
|
|
var url = require('url');
|
2015-06-13 19:46:24 +02:00
|
|
|
|
|
|
|
function FeedParseNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.url = n.url;
|
2018-03-11 22:10:37 +01:00
|
|
|
if (n.interval > 35790) { this.warn(RED._("feedparse.errors.invalidinterval")) }
|
2015-06-13 19:46:24 +02:00
|
|
|
this.interval = (parseInt(n.interval)||15) * 60000;
|
|
|
|
var node = this;
|
|
|
|
this.interval_id = null;
|
|
|
|
this.seen = {};
|
2015-10-11 22:05:55 +02:00
|
|
|
var parsedUrl = url.parse(this.url);
|
|
|
|
if (!(parsedUrl.host || (parsedUrl.hostname && parsedUrl.port)) && !parsedUrl.isUnix) {
|
|
|
|
this.error(RED._("feedparse.errors.invalidurl"));
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-06-13 19:46:24 +02:00
|
|
|
var getFeed = function() {
|
2018-01-29 10:16:35 +01:00
|
|
|
var req = request(node.url, {timeout:10000, pool:false});
|
2015-06-13 19:46:24 +02:00
|
|
|
//req.setMaxListeners(50);
|
2018-01-29 10:16:35 +01:00
|
|
|
req.setHeader('user-agent', 'Mozilla/5.0 (Node-RED)');
|
2018-09-14 17:10:53 +02:00
|
|
|
req.setHeader('accept', 'application/rss+xml,text/html,application/xhtml+xml');
|
2015-06-13 19:46:24 +02:00
|
|
|
|
|
|
|
var feedparser = new FeedParser();
|
|
|
|
|
|
|
|
req.on('error', function(err) { node.error(err); });
|
|
|
|
|
|
|
|
req.on('response', function(res) {
|
2015-09-12 22:31:11 +02:00
|
|
|
if (res.statusCode != 200) { node.warn(RED._("feedparse.errors.badstatuscode")+" "+res.statusCode); }
|
2015-06-13 19:46:24 +02:00
|
|
|
else { res.pipe(feedparser); }
|
|
|
|
});
|
|
|
|
|
|
|
|
feedparser.on('error', function(error) { node.error(error); });
|
|
|
|
|
|
|
|
feedparser.on('readable', function () {
|
|
|
|
var stream = this, article;
|
2015-06-16 15:38:36 +02:00
|
|
|
while (article = stream.read()) { // jshint ignore:line
|
2015-06-13 19:46:24 +02:00
|
|
|
if (!(article.guid in node.seen) || ( node.seen[article.guid] !== 0 && node.seen[article.guid] != article.date.getTime())) {
|
|
|
|
node.seen[article.guid] = article.date?article.date.getTime():0;
|
|
|
|
var msg = {
|
|
|
|
topic: article.origlink || article.link,
|
|
|
|
payload: article.description,
|
|
|
|
article: article
|
|
|
|
};
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
feedparser.on('meta', function (meta) {});
|
|
|
|
feedparser.on('end', function () {});
|
|
|
|
};
|
|
|
|
this.interval_id = setInterval(function() { getFeed(); }, node.interval);
|
|
|
|
getFeed();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.on("close", function() {
|
|
|
|
if (this.interval_id != null) {
|
|
|
|
clearInterval(this.interval_id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
RED.nodes.registerType("feedparse",FeedParseNode);
|
|
|
|
}
|