2 * Copyright 2013 IBM Corp.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 module.exports = function(RED) {
19 var FeedParser = require("feedparser");
20 var request = require("request");
22 function FeedParseNode(n) {
23 RED.nodes.createNode(this,n);
25 this.interval = (parseInt(n.interval)||15)*60000;
27 this.interval_id = null;
29 if (this.url !== "") {
30 var getFeed = function() {
31 request(node.url,function(err) {
32 if (err) node.error(err);
34 .pipe(new FeedParser({feedurl:node.url}))
35 .on('error', function(error) {
38 .on('meta', function (meta) {})
39 .on('readable', function () {
40 var stream = this, article;
41 while (article = stream.read()) {
42 if (!(article.guid in node.seen) || ( node.seen[article.guid] != 0 && node.seen[article.guid] != article.date.getTime())) {
43 node.seen[article.guid] = article.date?article.date.getTime():0;
45 topic:article.origlink||article.link,
46 payload: article.description,
53 .on('end', function () {
56 this.interval_id = setInterval(getFeed,node.interval);
60 this.error("Invalid url");
64 RED.nodes.registerType("feedparse",FeedParseNode);
66 FeedParseNode.prototype.close = function() {
67 if (this.interval_id != null) {
68 clearInterval(this.interval_id);