rss-to-json
Advanced tools
Comparing version 1.0.2 to 1.0.3
var feed = require('./index'); | ||
feed.read('https://codek.tv/feed/', function(err, rss){ | ||
feed.read('https://learnstartup.net/feed/', function(err, rss){ | ||
console.log(JSON.stringify(rss)); | ||
}); |
{ | ||
"name": "rss-to-json", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Rss to Json: RSS and Atom feed generator for Node.js", | ||
@@ -11,5 +11,13 @@ "author": { | ||
"dependencies": { | ||
"request": "^2.72.0", | ||
"xml2js": "^0.4.16" | ||
"request": "^2.83.0", | ||
"xml2js": "^0.4.19" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/nasa8x/rss-to-json.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/nasa8x/rss-to-json/issues" | ||
}, | ||
"homepage": "https://github.com/nasa8x/rss-to-json#readme", | ||
"keywords": [ | ||
@@ -40,4 +48,3 @@ "rss-to-json", | ||
"rss json" | ||
] | ||
} |
#About | ||
Util for parse ATOM and RSS feed resources and normalize them to JSON object. | ||
# Installation | ||
## Install | ||
Install via NPM | ||
```js | ||
npm install rss-to-json | ||
npm install rss-to-json --save | ||
``` | ||
# Example | ||
## Example | ||
@@ -26,3 +22,3 @@ ```js | ||
``` | ||
# Result | ||
## Result | ||
```js | ||
@@ -29,0 +25,0 @@ |
178
src/rss.js
// Invoke 'strict' JavaScript mode | ||
'use strict'; | ||
var util = require('util'), | ||
xml2js = require('xml2js'), | ||
request = require('request'); | ||
var util = require('util'), | ||
xml2js = require('xml2js'), | ||
request = require('request'); | ||
module.exports = { | ||
load: function(url, callback){ | ||
var $ = this; | ||
request({ | ||
url: url, | ||
headers: { | ||
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Firefox/45.0', | ||
accept: 'text/html,application/xhtml+xml' | ||
}, | ||
pool: false, | ||
followRedirect: true | ||
load: function (url, callback) { | ||
var $ = this; | ||
request({ | ||
url: url, | ||
headers: { | ||
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Firefox/45.0', | ||
accept: 'text/html,application/xhtml+xml' | ||
}, | ||
pool: false, | ||
followRedirect: true | ||
}, function (error, response, xml) { | ||
if (!error && response.statusCode == 200) { | ||
var parser = new xml2js.Parser({trim: false, normalize: true, mergeAttrs: true}); | ||
parser.addListener("error", function(err) { | ||
callback(err, null); | ||
}); | ||
parser.parseString(xml, function (err, result) { | ||
var rss = $.parser(result); | ||
callback(null, rss); | ||
//console.log(JSON.stringify(result.rss.channel)); | ||
}); | ||
}, function (error, response, xml) { | ||
if (!error && response.statusCode == 200) { | ||
var parser = new xml2js.Parser({ trim: false, normalize: true, mergeAttrs: true }); | ||
parser.addListener("error", function (err) { | ||
callback(err, null); | ||
}); | ||
parser.parseString(xml, function (err, result) { | ||
}else { | ||
this.emit('error', new Error('Bad status code')); | ||
} | ||
}); | ||
callback(null, $.parser(result)); | ||
//console.log(JSON.stringify(result.rss.channel)); | ||
}); | ||
}, | ||
parser: function(json){ | ||
var channel = json.rss.channel; | ||
var rss = {items:[]}; | ||
if(util.isArray(json.rss.channel)) | ||
channel = json.rss.channel[0]; | ||
} else { | ||
this.emit('error', new Error('Bad status code')); | ||
} | ||
}); | ||
if (channel.title) { | ||
rss.title = channel.title[0]; | ||
} | ||
if (channel.description) { | ||
rss.description = channel.description[0]; | ||
} | ||
if (channel.link) { | ||
rss.url = channel.link[0]; | ||
} | ||
if (channel.item) { | ||
if (!util.isArray(channel.item)) { | ||
channel.item = [channel.item]; | ||
} | ||
channel.item.forEach(function(val){ | ||
var obj = {}; | ||
obj.title = !util.isNullOrUndefined(val.title)?val.title[0]:''; | ||
obj.description = !util.isNullOrUndefined(val.description)?val.description[0]:''; | ||
obj.url = obj.link = !util.isNullOrUndefined(val.link)?val.link[0]:''; | ||
}, | ||
parser: function (json) { | ||
var channel = json.rss.channel; | ||
var rss = { items: [] }; | ||
if (util.isArray(json.rss.channel)) | ||
channel = json.rss.channel[0]; | ||
if (val.pubDate) { | ||
//lets try basis js date parsing for now | ||
obj.created = Date.parse(val.pubDate[0]); | ||
} | ||
if (val['media:content']) { | ||
obj.media = val.media || {}; | ||
obj.media.content = val['media:content']; | ||
} | ||
if (val['media:thumbnail']) { | ||
obj.media = val.media || {}; | ||
obj.media.thumbnail = val['media:thumbnail']; | ||
} | ||
if(val.enclosure){ | ||
obj.enclosures = []; | ||
if(!util.isArray(val.enclosure)) | ||
val.enclosure = [val.enclosure]; | ||
val.enclosure.forEach(function(enclosure){ | ||
var enc = {}; | ||
for (var x in enclosure) { | ||
enc[x] = enclosure[x][0]; | ||
} | ||
obj.enclosures.push(enc); | ||
}); | ||
if (channel.title) { | ||
rss.title = channel.title[0]; | ||
} | ||
if (channel.description) { | ||
rss.description = channel.description[0]; | ||
} | ||
if (channel.link) { | ||
rss.url = channel.link[0]; | ||
} | ||
} | ||
rss.items.push(obj); | ||
// add rss.image via @dubyajaysmith | ||
if (channel.image) { | ||
rss.image = channel.image[0].url | ||
} | ||
}); | ||
if (!rss.image && channel["itunes:image"]) { | ||
rss.image = channel['itunes:image'][0].href | ||
} | ||
} | ||
return rss; | ||
rss.image = rss.image && Array.isArray(rss.image) ? rss.image[0] : ''; | ||
}, | ||
read: function(url, callback){ | ||
return this.load(url, callback); | ||
if (channel.item) { | ||
if (!util.isArray(channel.item)) { | ||
channel.item = [channel.item]; | ||
} | ||
channel.item.forEach(function (val) { | ||
var obj = {}; | ||
obj.title = !util.isNullOrUndefined(val.title) ? val.title[0] : ''; | ||
obj.description = !util.isNullOrUndefined(val.description) ? val.description[0] : ''; | ||
obj.url = obj.link = !util.isNullOrUndefined(val.link) ? val.link[0] : ''; | ||
if (val.pubDate) { | ||
//lets try basis js date parsing for now | ||
obj.created = Date.parse(val.pubDate[0]); | ||
} | ||
if (val['media:content']) { | ||
obj.media = val.media || {}; | ||
obj.media.content = val['media:content']; | ||
} | ||
if (val['media:thumbnail']) { | ||
obj.media = val.media || {}; | ||
obj.media.thumbnail = val['media:thumbnail']; | ||
} | ||
if (val.enclosure) { | ||
obj.enclosures = []; | ||
if (!util.isArray(val.enclosure)) | ||
val.enclosure = [val.enclosure]; | ||
val.enclosure.forEach(function (enclosure) { | ||
var enc = {}; | ||
for (var x in enclosure) { | ||
enc[x] = enclosure[x][0]; | ||
} | ||
obj.enclosures.push(enc); | ||
}); | ||
} | ||
rss.items.push(obj); | ||
}); | ||
} | ||
return rss; | ||
}, | ||
read: function (url, callback) { | ||
return this.load(url, callback); | ||
} | ||
}; |
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
18906
102
0
0
0
455
Updatedrequest@^2.83.0
Updatedxml2js@^0.4.19