-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss-ori.js
120 lines (109 loc) · 3.2 KB
/
rss-ori.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
// Invoke 'strict' JavaScript mode
'use strict';
var util = require('util'),
xml2js = require('xml2js'),
request = require('request');
function copyEntry(obj) {
var result = {};
for (var x in obj) {
result[x] = obj[x][0];
}
return result;
}
module.exports = {
load: function(url, callback) {
var $ = this;
request({
url: url,
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Firefox/45.0',
'accept': 'text/html,application/xhtml+xml'
//'accept': '*/*'
},
pool: false,
followRedirect: true
}, function(error, response, xml) {
if (!error && response.statusCode == 200) {
var parser = new xml2js.Parser({
trim: false,
normalize: true,
mergeAttrs: true
});
parser.addListener("error", function(err) {
callback(err, null);
});
parser.parseString(xml, function(err, result) {
var rss = $.parser(result);
callback(null, rss);
});
} else {
console.log("Error", error);
callback(new Error(
'Error loading feed' +
((response && response.statusCode) ? (' : ' + response.statusCode) : '')
));
this.emit('error', new Error('Bad status code'));
}
});
},
parser: function(json) {
var channel = json.rss.channel;
var rss = {
item: []
};
if (util.isArray(json.rss.channel))
channel = json.rss.channel[0];
if (channel.title) {
rss.title = channel.title[0];
}
if (channel.description) {
rss.description = channel.description[0];
}
if (channel.link) {
rss.url = channel.link[0];
}
if (channel.item) {
if (!util.isArray(channel.item)) {
channel.item = [channel.item];
}
channel.item.forEach(function(val) {
var obj = {};
obj.title = !util.isNullOrUndefined(val.title) ? val.title[0] : '';
obj.description = !util.isNullOrUndefined(val.description) ? val.description[0] : '';
obj.url = obj.link = !util.isNullOrUndefined(val.link) ? val.link[0] : '';
if (val.pubDate) {
//obj.created = Date.parse(val.pubDate[0]);
obj.pubDate = val.pubDate[0];
}
if (val['media:content']) {
obj.media = val.media || {};
obj.media.content = val['media:content'];
}
if (val['media:thumbnail']) {
obj.media = val.media || {};
obj.media.thumbnail = val['media:thumbnail'];
}
if (val['itunes:duration']) {
obj.duration = val['itunes:duration'][0];
}
if (val.enclosure) {
if (!util.isArray(val.enclosure)) {
obj.enclosure = copyEntry(val.enclosure);
} else if (val.enclosure.length == 1) {
obj.enclosure = copyEntry(val.enclosure[0]);
} else {
obj.enclosure = [];
val.enclosure.forEach(function(enclosure) {
obj.enclosure.push(copyEntry(enclosure));
});
}
}
rss.item.push(obj);
});
}
return rss;
},
read: function(url, callback) {
return this.load(url, callback);
}
};