1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Update feedparse in line with underlying npm.

This commit is contained in:
Dave C-J 2014-10-31 18:59:43 +00:00
parent 6a37a823df
commit 694649e8f9
2 changed files with 44 additions and 37 deletions

View File

@ -31,7 +31,7 @@
</script> </script>
<script type="text/x-red" data-help-name="feedparse"> <script type="text/x-red" data-help-name="feedparse">
<p>Monitors an RSS/atom feed for new entries.</p> <p>Monitors an RSS/atom feed for new entries.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -53,5 +53,4 @@
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
} }
}); });
</script> </script>

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2013 IBM Corp. * Copyright 2013,2014 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,50 +22,58 @@ module.exports = function(RED) {
function FeedParseNode(n) { function FeedParseNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.url = n.url; this.url = n.url;
this.interval = (parseInt(n.interval)||15)*60000; this.interval = (parseInt(n.interval)||15) * 60000;
var node = this; var node = this;
this.interval_id = null; this.interval_id = null;
this.seen = {}; this.seen = {};
if (this.url !== "") { if (this.url !== "") {
var getFeed = function() { var getFeed = function() {
request(node.url,function(err) { var req = request(node.url, {timeout: 10000, pool: false});
if (err) node.error(err); //req.setMaxListeners(50);
}) //req.setHeader('user-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36');
.pipe(new FeedParser({feedurl:node.url})) req.setHeader('accept', 'text/html,application/xhtml+xml');
.on('error', function(error) {
node.error(error);
})
.on('meta', function (meta) {})
.on('readable', function () {
var stream = this, article;
while (article = stream.read()) {
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);
}
}
})
.on('end', function () {
});
};
this.interval_id = setInterval(getFeed,node.interval);
getFeed();
var feedparser = new FeedParser();
req.on('error', function(err) { node.error(err); });
req.on('response', function(res) {
if (res.statusCode != 200) { node.warn('error - Bad status code'); }
else { res.pipe(feedparser); }
});
feedparser.on('error', function(error) { node.error(error); });
feedparser.on('readable', function () {
var stream = this, article;
while (article = stream.read()) {
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();
} else { } else {
this.error("Invalid url"); this.error("Invalid url");
} }
this.on("close", function() {
if (this.interval_id != null) {
clearInterval(this.interval_id);
}
});
} }
RED.nodes.registerType("feedparse",FeedParseNode); RED.nodes.registerType("feedparse",FeedParseNode);
FeedParseNode.prototype.close = function() {
if (this.interval_id != null) {
clearInterval(this.interval_id);
}
}
} }