-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathindex.js
81 lines (72 loc) · 2.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var Entities = require("entities");
var FS = require('fs');
var XML2JS = require('xml2js');
var HTTP = require('http');
var HTTPS = require('https');
var Parser = module.exports = {};
var TOP_FIELDS = ['title', 'description', 'author', 'link'];
var ITEM_FIELDS = [
'title',
'link',
'pubDate',
'author',
]
var stripHtml = function(str) {
return str.replace(/<(?:.|\n)*?>/gm, '');
}
var parseRSS1 = function(xmlObj, callback) {
callback("RSS 1.0 parsing not yet implemented.")
}
var parseRSS2 = function(xmlObj, callback) {
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) {
if (channel[f]) json.feed[f] = channel[f][0];
})
var items = channel.item;
(items || []).forEach(function(item) {
var entry = {};
ITEM_FIELDS.forEach(function(f) {
if (item[f]) entry[f] = item[f][0];
})
if (item.description) {
entry.content = item.description[0];
entry.contentSnippet = entry.content;
//entry.contentSnippet = Entities.decode(stripHtml(entry.content));
}
if (item.guid) {
entry.guid = item.guid[0]._;
}
if (item.category) entry.categories = item.category;
json.feed.entries.push(entry);
})
callback(null, json);
}
Parser.parseString = function(xml, callback) {
XML2JS.parseString(xml, function(err, result) {
if (err) throw err;
if (result.rss && result.rss.$.version && result.rss.$.version.indexOf('2') === 0) return parseRSS2(result, callback);
else return parseRSS1(result, callback);
});
}
Parser.parseURL = function(url, callback) {
var xml = '';
var get = url.indexOf('https') === 0 ? HTTPS.get : HTTP.get;
var req = get(url, function(res) {
if (res.statusCode >= 300) return callback(new Error("Status code " + res.statusCode))
res.setEncoding('utf8');
res.on('data', function(chunk) {
xml += chunk;
});
res.on('end', function() {
return Parser.parseString(xml, callback);
})
})
req.on('error', callback);
}
Parser.parseFile = function(file, callback) {
FS.readFile(file, 'utf8', function(err, contents) {
return Parser.parseString(contents, callback);
})
}