rss-parser
Advanced tools
Comparing version 2.7.0 to 2.9.0
48
index.js
@@ -11,3 +11,3 @@ var Entities = require("entities"); | ||
var TOP_FIELDS = [ | ||
var FEED_FIELDS = [ | ||
'title', | ||
@@ -106,7 +106,13 @@ 'description', | ||
var parseRSS2 = function(xmlObj, callback) { | ||
var parseRSS2 = function(xmlObj, options, callback) { | ||
options.customFields = options.customFields || {}; | ||
var itemFields = ITEM_FIELDS.concat(options.customFields.item || []); | ||
var feedFields = FEED_FIELDS.concat(options.customFields.feed || []); | ||
var json = {feed: {entries: []}}; | ||
var channel = xmlObj.rss.channel[0]; | ||
if (channel['atom:link']) json.feed.feedUrl = channel['atom:link'][0].$.href; | ||
TOP_FIELDS.forEach(function(f) { | ||
feedFields.forEach(function(f) { | ||
if (channel[f]) json.feed[f] = channel[f][0]; | ||
@@ -117,3 +123,3 @@ }) | ||
var entry = {}; | ||
ITEM_FIELDS.forEach(function(f) { | ||
itemFields.forEach(function(f) { | ||
if (item[f]) entry[f] = item[f][0]; | ||
@@ -192,3 +198,8 @@ }) | ||
Parser.parseString = function(xml, callback) { | ||
Parser.parseString = function(xml, settings, callback) { | ||
if (!callback) { | ||
callback = settings; | ||
settings = {}; | ||
} | ||
XML2JS.parseString(xml, function(err, result) { | ||
@@ -199,3 +210,3 @@ if (err) return callback(err); | ||
} else if (result.rss && result.rss.$.version && result.rss.$.version.indexOf('2') === 0) { | ||
return parseRSS2(result, callback); | ||
return parseRSS2(result, settings, callback); | ||
} else { | ||
@@ -207,9 +218,9 @@ return parseRSS1(result, callback); | ||
Parser.parseURL = function(feedUrl, settings, callback) { | ||
Parser.parseURL = function(feedUrl, options, callback) { | ||
if (!callback) { | ||
callback = settings; | ||
settings = {}; | ||
callback = options; | ||
options = {}; | ||
} | ||
settings.__redirectCount = settings.__redirectCount || 0; | ||
if (settings.maxRedirects === undefined) settings.maxRedirects = 1; | ||
options.__redirectCount = options.__redirectCount || 0; | ||
if (options.maxRedirects === undefined) options.maxRedirects = 1; | ||
@@ -220,2 +231,3 @@ var xml = ''; | ||
var req = get({ | ||
auth: parsedUrl.auth, | ||
protocol: parsedUrl.protocol, | ||
@@ -227,6 +239,6 @@ hostname: parsedUrl.hostname, | ||
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers['location']) { | ||
if (settings.maxRedirects === 0) return callback(new Error("Status code " + res.statusCode)); | ||
if (settings.__redirectCount === settings.maxRedirects) return callback(new Error("Too many redirects")); | ||
settings.__redirectCount++; | ||
return Parser.parseURL(res.headers['location'], settings, callback); | ||
if (options.maxRedirects === 0) return callback(new Error("Status code " + res.statusCode)); | ||
if (options.__redirectCount === options.maxRedirects) return callback(new Error("Too many redirects")); | ||
options.__redirectCount++; | ||
return Parser.parseURL(res.headers['location'], options, callback); | ||
} | ||
@@ -238,3 +250,3 @@ res.setEncoding('utf8'); | ||
res.on('end', function() { | ||
return Parser.parseString(xml, callback); | ||
return Parser.parseString(xml, options, callback); | ||
}) | ||
@@ -245,6 +257,6 @@ }) | ||
Parser.parseFile = function(file, callback) { | ||
Parser.parseFile = function(file,options,callback) { | ||
FS.readFile(file, 'utf8', function(err, contents) { | ||
return Parser.parseString(contents, callback); | ||
return Parser.parseString(contents, options, callback); | ||
}) | ||
} |
{ | ||
"name": "rss-parser", | ||
"version": "2.7.0", | ||
"version": "2.9.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -13,6 +13,7 @@ # rss-parser | ||
You can parse RSS from a URL, local file (NodeJS only), or a string. | ||
* `parseString(xml, callback)` | ||
* `parseFile(filename, callback)` | ||
* `parseString(xml, [options,], callback)` | ||
* `parseFile(filename, [options,], callback)` | ||
* `parseURL(url, [options,] callback)` | ||
Check out the output format in [test/output/reddit.json](test/output/reddit.json) | ||
@@ -31,3 +32,2 @@ | ||
``` | ||
### Web | ||
@@ -56,2 +56,19 @@ ```html | ||
### Custom Fields | ||
```js | ||
var options = { | ||
customFields: { | ||
feed: ['otherTitle', 'extendedDescription'], | ||
item: ['coAuthor','subtitle'], | ||
} | ||
} | ||
parser.parseURL('https://www.reddit.com/.rss', options, function(err, parsed) { | ||
console.log(parsed.feed.extendedDescription); | ||
parsed.feed.entries.forEach(function(entry) { | ||
console.log(entry.coAuthor + ':' + entry.subtitle); | ||
}) | ||
}) | ||
``` | ||
## Contributing | ||
@@ -73,8 +90,8 @@ Contributions welcome! | ||
```bash | ||
npm version minor # or major/patch | ||
# change version in package.json | ||
grunt build | ||
git commit -a -m "browserify" | ||
git commit -a -m "vX.X.X" | ||
git tag vX.X.X | ||
npm publish | ||
git push --follow-tags | ||
``` | ||
@@ -36,2 +36,3 @@ { | ||
"itunes": { | ||
"image": "https://www.narro.co/images/narro-icon-lg.png", | ||
"owner": { | ||
@@ -41,3 +42,2 @@ "name": "foobar", | ||
}, | ||
"image": "https://www.narro.co/images/narro-icon-lg.png", | ||
"author": "foobar@gmail.com", | ||
@@ -44,0 +44,0 @@ "subtitle": "foobar uses Narro to create a podcast of articles transcribed to audio.", |
@@ -69,2 +69,23 @@ var FS = require('fs'); | ||
}); | ||
it('should parse custom fields', function(done) { | ||
var options = { | ||
customFields: { | ||
feed: ['language', 'copyright'], | ||
item: ['subtitle'] | ||
} | ||
}; | ||
Parser.parseFile(__dirname + '/input/customfields.rss',options, function(err, parsed) { | ||
Expect(err).to.equal(null); | ||
let str = JSON.stringify(parsed, null, 2); | ||
let outfile = OUT_DIR + '/customfields.json'; | ||
if (process.env.WRITE_GOLDEN) { | ||
FS.writeFileSync(outfile, str); | ||
} else { | ||
var expected = FS.readFileSync(outfile, 'utf8'); | ||
Expect(str).to.equal(expected); | ||
} | ||
done(); | ||
}); | ||
}); | ||
}) |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
1452657
31
17423
94
10