-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
315 lines (284 loc) · 8.31 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
'use strict';
var Entities = require("entities");
var FS = require('fs');
var url = require('url');
var XML2JS = require('xml2js');
const moment = require('moment')
const _get = require('lodash.get')
var HTTP = require('http');
var HTTPS = require('https');
const RFC3339 = 'YYYY-MM-DDTHH:mm:ssZ'
var Parser = module.exports = {};
var FEED_FIELDS = [
['author', 'creator'],
['dc:publisher', 'publisher'],
['dc:creator', 'creator'],
['dc:source', 'source'],
['dc:title', 'title'],
['dc:type', 'type'],
'title',
'description',
'author',
'pubDate',
'webMaster',
'managingEditor',
'generator',
'link',
];
var ITEM_FIELDS = [
['author', 'creator'],
['dc:creator', 'creator'],
['dc:date', 'date'],
['dc:language', 'language'],
['dc:rights', 'rights'],
['dc:source', 'source'],
['dc:title', 'title'],
'title',
'link',
'pubDate',
'author',
'content:encoded',
'enclosure',
'dc:creator',
'dc:date',
];
var mapItunesField = function(f) {
return ['itunes:' + f, f];
}
var PODCAST_FEED_FIELDS = ([
'author',
'subtitle',
'summary',
'explicit'
]).map(mapItunesField);
var PODCAST_ITEM_FIELDS = ([
'author',
'subtitle',
'summary',
'explicit',
'duration',
'image'
]).map(mapItunesField);
var stripHtml = function(str) {
return str.replace(/<(?:.|\n)*?>/gm, '');
}
var getSnippet = function(str) {
return Entities.decode(stripHtml(str)).trim();
}
var getContent = function(content) {
if (typeof content._ === 'string') {
return content._;
} else if (typeof content === 'object') {
var builder = new XML2JS.Builder({headless: true, explicitRoot: true, rootName: 'div', renderOpts: {pretty: false}});
return builder.buildObject(content);
} else {
return content;
}
}
var parseAtomFeed = function(xmlObj, options, callback) {
var feed = xmlObj.feed;
var json = {version: '1.0.0', items: []};
if (feed.link) {
if (feed.link[0] && feed.link[0].$.href) json.home_page_url = feed.link[0].$.href;
if (feed.link[1] && feed.link[1].$.href) json.feed_url = feed.link[1].$.href;
}
if (feed.title) {
var title = feed.title[0] || '';
if (title._) title = title._
if (title) json.title = title;
}
var entries = feed.entry;
(entries || []).forEach(function (entry) {
var item = {};
if (entry.title) {
var title = entry.title[0] || '';
if (title._) title = title._;
if (title) item.title = title;
}
if (entry.link && entry.link.length) item.url = entry.link[0].$.href;
if (entry.updated && entry.updated.length) item.date_published = moment.utc(entry.updated[0]).format(RFC3339);
if (entry.author && entry.author.length) item.author = {name: entry.author[0].name[0]};
if (entry.content && entry.content.length) {
item.content_html = getContent(entry.content[0]);
}
if (entry.id) {
item.id = entry.id[0];
}
json.items.push(item);
});
callback(null, json);
}
var parseRSS1 = function(xmlObj, options, callback) {
xmlObj = xmlObj['rdf:RDF'];
var channel = xmlObj.channel[0];
var items = xmlObj.item;
return parseRSS(channel, items, options, callback);
}
var parseRSS2 = function(xmlObj, options, callback) {
var channel = xmlObj.rss.channel[0];
var items = channel.item;
return parseRSS(channel, items, options, function(err, data) {
if (err) return callback(err);
if (xmlObj.rss.$['xmlns:itunes']) {
decorateItunes(data, channel);
}
callback(null, data);
});
}
var parseRSS = function(channel, items, options, callback) {
items = items || [];
options.customFields = options.customFields || {};
var itemFields = ITEM_FIELDS.concat(options.customFields.item || []);
var feedFields = FEED_FIELDS.concat(options.customFields.feed || []);
const feed_url = _get(channel, 'atom:link.$.href', null)
|| _get(channel, '$.rdf:about', null)
var json = {
version: "1.0.0",
title: channel['title'][0],
home_page_url: channel['link'][0],
feed_url,
items: []
};
if (channel['atom:link']) json.feed_url = channel['atom:link'][0].$.href;
items.forEach(function(item) {
var jsonItem = {};
if (item.enclosure) {
entry.enclosure = item.enclosure[0].$;
}
if (item.description) {
jsonItem.content_html = getContent(item.description[0]);
jsonItem.summary = getSnippet(jsonItem.content_html);
}
if (item.title) {
jsonItem.title = item.title[0]
}
if (item.link) {
jsonItem.url = item.link[0]
}
if (item.guid) {
jsonItem.id = _get(item, 'guid[0]._', null) || _get(item, 'guid[0]', null)
}
if (item.category) {
jsonItem.tags = item.category
}
const date = _get(item, 'dc:date[0]', null)
|| _get(item, 'dcterms:issued[0]', null)
|| _get(item, 'pubDate[0]', null);
if (date) {
try {
jsonItem.date_published = moment(date.trim()).format(RFC3339);
} catch (e) {
// Ignore bad date format
}
}
json.items.push(jsonItem);
})
callback(null, json);
}
var copyFromXML = function(xml, dest, fields) {
fields.forEach(function(f) {
var from = f;
var to = f;
if (Array.isArray(f)) {
from = f[0];
to = f[1];
}
if (xml[from] !== undefined) dest[to] = xml[from][0];
})
}
/**
* Add iTunes specific fields from XML to extracted JSON
*
* @access public
* @param {object} json extracted
* @param {object} channel parsed XML
*/
var decorateItunes = function decorateItunes(json, channel) {
var items = channel.item || [],
entry = {};
json.feed.itunes = {}
if (channel['itunes:owner']) {
var owner = {},
image;
if(channel['itunes:owner'][0]['itunes:name']) {
owner.name = channel['itunes:owner'][0]['itunes:name'][0];
}
if(channel['itunes:owner'][0]['itunes:email']) {
owner.email = channel['itunes:owner'][0]['itunes:email'][0];
}
if(channel['itunes:image']) {
image = channel['itunes:image'][0].$.href
}
if(image) {
json.feed.itunes.image = image;
}
json.feed.itunes.owner = owner;
}
copyFromXML(channel, json.feed.itunes, PODCAST_FEED_FIELDS);
items.forEach(function(item, index) {
var entry = json.feed.entries[index];
entry.itunes = {};
copyFromXML(item, entry.itunes, PODCAST_ITEM_FIELDS);
var image = item['itunes:image'];
if (image && image[0] && image[0].$ && image[0].$.href) {
entry.itunes.image = image[0].$.href;
}
});
}
Parser.parseString = function(xml, options, callback) {
if (!callback) {
callback = options;
options = {};
}
XML2JS.parseString(xml, function(err, result) {
if (err) return callback(err);
debugger
if (result.feed) {
return parseAtomFeed(result, options, callback)
} else if (result.rss && result.rss.$.version && result.rss.$.version.indexOf('2') === 0) {
return parseRSS2(result, options, callback);
} else if (result['rdf:RDF']) {
return parseRSS1(result, options, callback);
} else {
return callback(new Error("Feed not recognized as RSS 1 or 2."))
}
});
}
Parser.parseURL = function(feedUrl, options, callback) {
if (!callback) {
callback = options;
options = {};
}
options.__redirectCount = options.__redirectCount || 0;
if (options.maxRedirects === undefined) options.maxRedirects = 1;
var xml = '';
var get = feedUrl.indexOf('https') === 0 ? HTTPS.get : HTTP.get;
var parsedUrl = url.parse(feedUrl);
var req = get({
auth: parsedUrl.auth,
protocol: parsedUrl.protocol,
hostname: parsedUrl.hostname,
path: parsedUrl.path,
headers: {'User-Agent': 'rss-parser'}
}, function(res) {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers['location']) {
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);
}
res.setEncoding('utf8');
res.on('data', function(chunk) {
xml += chunk;
});
res.on('end', function() {
return Parser.parseString(xml, options, callback);
})
})
req.on('error', callback);
}
Parser.parseFile = function(file, options, callback) {
FS.readFile(file, 'utf8', function(err, contents) {
return Parser.parseString(contents, options, callback);
})
}