Skip to content

Commit 5bd1eb8

Browse files
author
Ubuntu
committed
throw error for 1.0
1 parent 9afbe3d commit 5bd1eb8

File tree

2 files changed

+39
-23
lines changed

2 files changed

+39
-23
lines changed

index.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,41 @@ var stripHtml = function(str) {
1919
return str.replace(/<(?:.|\n)*?>/gm, '');
2020
}
2121

22+
var parseRSS1 = function(xmlObj, callback) {
23+
callback("RSS 1.0 parsing not yet implemented.")
24+
}
25+
26+
var parseRSS2 = function(xmlObj, callback) {
27+
var json = {feed: {entries: []}};
28+
var channel = xmlObj.rss.channel[0];
29+
if (channel['atom:link']) json.feed.feedUrl = channel['atom:link'][0].href;
30+
TOP_FIELDS.forEach(function(f) {
31+
if (channel[f]) json.feed[f] = channel[f][0];
32+
})
33+
var items = channel.item;
34+
(items || []).forEach(function(item) {
35+
var entry = {};
36+
ITEM_FIELDS.forEach(function(f) {
37+
if (item[f]) entry[f] = item[f][0];
38+
})
39+
if (item.description) {
40+
entry.content = item.description[0];
41+
entry.contentSnippet = Entities.decode(stripHtml(entry.content));
42+
}
43+
if (item.guid) {
44+
entry.guid = item.guid[0]._;
45+
}
46+
if (item.category) entry.categories = item.category;
47+
json.feed.entries.push(entry);
48+
})
49+
callback(null, json);
50+
}
51+
2252
Parser.parseString = function(xml, callback) {
2353
XML2JS.parseString(xml, function(err, result) {
2454
if (err) throw err;
25-
var json = {feed: {entries: []}};
26-
var channel = result.rss.channel[0];
27-
if (channel['atom:link']) json.feed.feedUrl = channel['atom:link'][0].href;
28-
TOP_FIELDS.forEach(function(f) {
29-
if (channel[f]) json.feed[f] = channel[f][0];
30-
})
31-
var items = channel.item;
32-
(items || []).forEach(function(item) {
33-
var entry = {};
34-
ITEM_FIELDS.forEach(function(f) {
35-
if (item[f]) entry[f] = item[f][0];
36-
})
37-
if (item.description) {
38-
entry.content = item.description[0];
39-
entry.contentSnippet = Entities.decode(stripHtml(entry.content));
40-
}
41-
if (item.guid) {
42-
entry.guid = item.guid[0]._;
43-
}
44-
if (item.category) entry.categories = item.category;
45-
json.feed.entries.push(entry);
46-
})
47-
callback(null, json);
55+
if (result.rss && result.rss.$.version && result.rss.$.version.indexOf('2') === 0) return parseRSS2(result, callback);
56+
else return parseRSS1(result, callback);
4857
});
4958
}
5059

test/parser.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,11 @@ describe('Parser', function() {
2121
done();
2222
})
2323
})
24+
25+
it('should parse craigslist', function(done) {
26+
Parser.parseURL('https://seattle.craigslist.org/search/act?format=rss', function(err, parsed) {
27+
Expect(err).to.not.equal(null);
28+
done();
29+
})
30+
})
2431
})

0 commit comments

Comments
 (0)