1
- /*! rss-parser 2.8 .0 */
1
+ /*! rss-parser 2.9 .0 */
2
2
3
3
( function ( f ) { if ( typeof exports === "object" && typeof module !== "undefined" ) { module . exports = f ( ) } else if ( typeof define === "function" && define . amd ) { define ( [ ] , f ) } else { var g ; if ( typeof window !== "undefined" ) { g = window } else if ( typeof global !== "undefined" ) { g = global } else if ( typeof self !== "undefined" ) { g = self } else { g = this } g . RSSParser = f ( ) } } ) ( function ( ) { var define , module , exports ; return ( function e ( t , n , r ) { function s ( o , u ) { if ( ! n [ o ] ) { if ( ! t [ o ] ) { var a = typeof require == "function" && require ; if ( ! u && a ) return a ( o , ! 0 ) ; if ( i ) return i ( o , ! 0 ) ; var f = new Error ( "Cannot find module '" + o + "'" ) ; throw f . code = "MODULE_NOT_FOUND" , f } var l = n [ o ] = { exports :{ } } ; t [ o ] [ 0 ] . call ( l . exports , function ( e ) { var n = t [ o ] [ 1 ] [ e ] ; return s ( n ?n :e ) } , l , l . exports , e , t , n , r ) } return n [ o ] . exports } var i = typeof require == "function" && require ; for ( var o = 0 ; o < r . length ; o ++ ) s ( r [ o ] ) ; return s } ) ( { 1 :[ function ( require , module , exports ) {
4
4
var Entities = require ( "entities" ) ;
@@ -11,7 +11,7 @@ var HTTPS = require('https');
11
11
12
12
var Parser = module . exports = { } ;
13
13
14
- var TOP_FIELDS = [
14
+ var FEED_FIELDS = [
15
15
'title' ,
16
16
'description' ,
17
17
'author' ,
@@ -105,17 +105,23 @@ var parseRSS1 = function(xmlObj, callback) {
105
105
callback ( "RSS 1.0 parsing not yet implemented." )
106
106
}
107
107
108
- var parseRSS2 = function ( xmlObj , callback ) {
108
+ var parseRSS2 = function ( xmlObj , options , callback ) {
109
+
110
+ options . customFields = options . customFields || { } ;
111
+ var itemFields = ITEM_FIELDS . concat ( options . customFields . item || [ ] ) ;
112
+ var feedFields = FEED_FIELDS . concat ( options . customFields . feed || [ ] ) ;
113
+
109
114
var json = { feed : { entries : [ ] } } ;
110
115
var channel = xmlObj . rss . channel [ 0 ] ;
111
116
if ( channel [ 'atom:link' ] ) json . feed . feedUrl = channel [ 'atom:link' ] [ 0 ] . $ . href ;
112
- TOP_FIELDS . forEach ( function ( f ) {
117
+
118
+ feedFields . forEach ( function ( f ) {
113
119
if ( channel [ f ] ) json . feed [ f ] = channel [ f ] [ 0 ] ;
114
120
} )
115
121
var items = channel . item ;
116
122
( items || [ ] ) . forEach ( function ( item ) {
117
123
var entry = { } ;
118
- ITEM_FIELDS . forEach ( function ( f ) {
124
+ itemFields . forEach ( function ( f ) {
119
125
if ( item [ f ] ) entry [ f ] = item [ f ] [ 0 ] ;
120
126
} )
121
127
if ( item . enclosure ) {
@@ -189,26 +195,31 @@ var decorateItunes = function decorateItunes(json, channel) {
189
195
} ) ;
190
196
}
191
197
192
- Parser . parseString = function ( xml , callback ) {
198
+ Parser . parseString = function ( xml , settings , callback ) {
199
+ if ( ! callback ) {
200
+ callback = settings ;
201
+ settings = { } ;
202
+ }
203
+
193
204
XML2JS . parseString ( xml , function ( err , result ) {
194
205
if ( err ) return callback ( err ) ;
195
206
if ( result . feed ) {
196
207
return parseAtomFeed ( result , callback )
197
208
} else if ( result . rss && result . rss . $ . version && result . rss . $ . version . indexOf ( '2' ) === 0 ) {
198
- return parseRSS2 ( result , callback ) ;
209
+ return parseRSS2 ( result , settings , callback ) ;
199
210
} else {
200
211
return parseRSS1 ( result , callback ) ;
201
212
}
202
213
} ) ;
203
214
}
204
215
205
- Parser . parseURL = function ( feedUrl , settings , callback ) {
216
+ Parser . parseURL = function ( feedUrl , options , callback ) {
206
217
if ( ! callback ) {
207
- callback = settings ;
208
- settings = { } ;
218
+ callback = options ;
219
+ options = { } ;
209
220
}
210
- settings . __redirectCount = settings . __redirectCount || 0 ;
211
- if ( settings . maxRedirects === undefined ) settings . maxRedirects = 1 ;
221
+ options . __redirectCount = options . __redirectCount || 0 ;
222
+ if ( options . maxRedirects === undefined ) options . maxRedirects = 1 ;
212
223
213
224
var xml = '' ;
214
225
var get = feedUrl . indexOf ( 'https' ) === 0 ? HTTPS . get : HTTP . get ;
@@ -221,25 +232,25 @@ Parser.parseURL = function(feedUrl, settings, callback) {
221
232
headers : { 'User-Agent' : 'rss-parser' }
222
233
} , function ( res ) {
223
234
if ( res . statusCode >= 300 && res . statusCode < 400 && res . headers [ 'location' ] ) {
224
- if ( settings . maxRedirects === 0 ) return callback ( new Error ( "Status code " + res . statusCode ) ) ;
225
- if ( settings . __redirectCount === settings . maxRedirects ) return callback ( new Error ( "Too many redirects" ) ) ;
226
- settings . __redirectCount ++ ;
227
- return Parser . parseURL ( res . headers [ 'location' ] , settings , callback ) ;
235
+ if ( options . maxRedirects === 0 ) return callback ( new Error ( "Status code " + res . statusCode ) ) ;
236
+ if ( options . __redirectCount === options . maxRedirects ) return callback ( new Error ( "Too many redirects" ) ) ;
237
+ options . __redirectCount ++ ;
238
+ return Parser . parseURL ( res . headers [ 'location' ] , options , callback ) ;
228
239
}
229
240
res . setEncoding ( 'utf8' ) ;
230
241
res . on ( 'data' , function ( chunk ) {
231
242
xml += chunk ;
232
243
} ) ;
233
244
res . on ( 'end' , function ( ) {
234
- return Parser . parseString ( xml , callback ) ;
245
+ return Parser . parseString ( xml , options , callback ) ;
235
246
} )
236
247
} )
237
248
req . on ( 'error' , callback ) ;
238
249
}
239
250
240
- Parser . parseFile = function ( file , callback ) {
251
+ Parser . parseFile = function ( file , options , callback ) {
241
252
FS . readFile ( file , 'utf8' , function ( err , contents ) {
242
- return Parser . parseString ( contents , callback ) ;
253
+ return Parser . parseString ( contents , options , callback ) ;
243
254
} )
244
255
}
245
256
0 commit comments