Skip to content

Commit b212eed

Browse files
author
Ubuntu
committed
working
1 parent a3cd3f2 commit b212eed

File tree

5 files changed

+1320
-12
lines changed

5 files changed

+1320
-12
lines changed

index.js

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,54 @@
1+
var Entities = require("entities");
12
var FS = require('fs');
3+
var XML2JS = require('xml2js');
4+
25
var Parser = module.exports = {};
36

4-
Parser.parse = function(xml, callback) {
5-
callback(null, {});
7+
var TOP_FIELDS = ['title', 'description', 'author', 'link'];
8+
var ITEM_FIELDS = [
9+
'title',
10+
'link',
11+
'guid',
12+
'pubDate',
13+
'author',
14+
]
15+
16+
var stripHtml = function(str) {
17+
return str.replace(/<(?:.|\n)*?>/gm, '');
18+
}
19+
20+
Parser.parseString = function(xml, callback) {
21+
XML2JS.parseString(xml, function(err, result) {
22+
var json = {feed: {}};
23+
var channel = result.rss.channel[0];
24+
if (channel['atom:link']) json.feed.feedUrl = channel['atom:link'][0].href;
25+
TOP_FIELDS.forEach(function(f) {
26+
if (channel[f]) json.feed[f] = channel[f][0];
27+
})
28+
var items = channel.item;
29+
(items || []).forEach(function(item) {
30+
var entry = {};
31+
ITEM_FIELDS.forEach(function(f) {
32+
if (item[f]) entry[f] = item[f][0];
33+
})
34+
if (item.description) {
35+
entry.content = item.description[0];
36+
entry.contentSnippet = Entities.decode(stripHtml(entry.content));
37+
}
38+
entry.categories = item.category;
39+
json.entries = json.entries || [];
40+
json.entries.push(entry);
41+
})
42+
callback(null, json);
43+
});
644
}
745

846
Parser.parseURL = function(url, callback) {
9-
return Parser.parse('', callback);
47+
return Parser.parseString('', callback);
1048
}
1149

1250
Parser.parseFile = function(file, callback) {
1351
FS.readFile(file, 'utf8', function(err, contents) {
14-
return Parser.parse(contents, callback);
52+
return Parser.parseString(contents, callback);
1553
})
1654
}

package.json

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
{
22
"name": "rss-parser",
33
"version": "1.0.0",
4-
"description": "",
54
"main": "index.js",
65
"scripts": {
76
"test": "mocha"
87
},
9-
"author": "",
8+
"author": "Bobby Brennan",
109
"license": "MIT",
1110
"devDependencies": {
1211
"chai": "^3.4.1"
13-
}
12+
},
13+
"dependencies": {
14+
"entities": "^1.1.1",
15+
"xml2js": "^0.4.15"
16+
},
17+
"directories": {
18+
"test": "test"
19+
},
20+
"repository": {
21+
"type": "git",
22+
"url": "git+https://github.com/bobby-brennan/rss-parser.git"
23+
},
24+
"bugs": {
25+
"url": "https://github.com/bobby-brennan/rss-parser/issues"
26+
},
27+
"homepage": "https://github.com/bobby-brennan/rss-parser#readme",
28+
"description": ""
1429
}

0 commit comments

Comments
 (0)