[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / social / 32-feedparse.js
1 /**
2  * Copyright 2013 IBM Corp.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  **/
16
17 module.exports = function(RED) {
18     "use strict";
19     var FeedParser = require("feedparser");
20     var request = require("request");
21
22     function FeedParseNode(n) {
23         RED.nodes.createNode(this,n);
24         this.url = n.url;
25         this.interval = (parseInt(n.interval)||15)*60000;
26         var node = this;
27         this.interval_id = null;
28         this.seen = {};
29         if (this.url !== "") {
30             var getFeed = function() {
31                 request(node.url,function(err) {
32                         if (err) node.error(err);
33                 })
34                     .pipe(new FeedParser({feedurl:node.url}))
35                     .on('error', function(error) {
36                             node.error(error);
37                     })
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;
44                                     var msg = {
45                                         topic:article.origlink||article.link,
46                                         payload: article.description,
47                                         article: article
48                                     };
49                                     node.send(msg);
50                                 }
51                             }
52                     })
53                     .on('end', function () {
54                     });
55             };
56             this.interval_id = setInterval(getFeed,node.interval);
57             getFeed();
58
59         } else {
60             this.error("Invalid url");
61         }
62     }
63
64     RED.nodes.registerType("feedparse",FeedParseNode);
65
66     FeedParseNode.prototype.close = function() {
67         if (this.interval_id != null) {
68             clearInterval(this.interval_id);
69         }
70     }
71 }