2013-09-05 16:02:48 +02:00
/ * *
2014-10-31 19:59:43 +01:00
* Copyright 2013 , 2014 IBM Corp .
2013-09-05 16:02:48 +02:00
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
* You may obtain a copy of the License at
*
* http : //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing , software
* distributed under the License is distributed on an "AS IS" BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
* See the License for the specific language governing permissions and
* limitations under the License .
* * /
2014-05-04 00:32:04 +02:00
module . exports = function ( RED ) {
2014-05-29 23:13:21 +02:00
"use strict" ;
2014-05-04 00:32:04 +02:00
var FeedParser = require ( "feedparser" ) ;
var request = require ( "request" ) ;
2014-05-29 23:13:21 +02:00
2014-05-04 00:32:04 +02:00
function FeedParseNode ( n ) {
RED . nodes . createNode ( this , n ) ;
this . url = n . url ;
2014-10-31 19:59:43 +01:00
this . interval = ( parseInt ( n . interval ) || 15 ) * 60000 ;
2014-05-04 00:32:04 +02:00
var node = this ;
this . interval _id = null ;
this . seen = { } ;
if ( this . url !== "" ) {
var getFeed = function ( ) {
2014-10-31 19:59:43 +01:00
var req = request ( node . url , { timeout : 10000 , pool : false } ) ;
//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');
req . setHeader ( 'accept' , 'text/html,application/xhtml+xml' ) ;
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 ( ) { } ) ;
2014-05-04 00:32:04 +02:00
} ;
2014-10-31 19:59:43 +01:00
this . interval _id = setInterval ( function ( ) { getFeed ( ) ; } , node . interval ) ;
2014-05-04 00:32:04 +02:00
getFeed ( ) ;
} else {
this . error ( "Invalid url" ) ;
}
2014-10-31 19:59:43 +01:00
this . on ( "close" , function ( ) {
if ( this . interval _id != null ) {
clearInterval ( this . interval _id ) ;
}
} ) ;
2013-09-05 16:02:48 +02:00
}
2014-05-29 23:13:21 +02:00
2014-05-04 00:32:04 +02:00
RED . nodes . registerType ( "feedparse" , FeedParseNode ) ;
2014-05-29 23:13:21 +02:00
}