-
Notifications
You must be signed in to change notification settings - Fork 0
/
rss.js
159 lines (139 loc) · 5.13 KB
/
rss.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
// Invoke 'strict' JavaScript mode
'use strict';
const limit = 20; // GC
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': 'RSS Reader',
'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) {
if(err){
callback(err, null);
return;
}
else{
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 list = json.rss.channel;
var rss = {item:[]};
if(util.isArray(json.rss.channel))
list = json.rss.channel[0];
if (list['itunes:image']){
var mainimage = list['itunes:image'][0].href[0];
//var mainimage = list['itunes:image'][0];
//var mainimage = 'fuck';
}
if (list.title) {
rss.title = list.title[0];
}
if (list.description) {
rss.description = list.description[0];
}
if (list.link) {
rss.url = list.link[0];
}
if (list.item) {
if (!util.isArray(list.item)) {
list.item = [list.item];
}
// GC
list.item.slice(0, limit).forEach(function(val){
// list.item.slice(-20).forEach(function(val){
// list.item.forEach(function(val){
var obj = {};
obj.service = 'podcast';
obj.title = !util.isNullOrUndefined(val.title)?val.title[0]:'';
obj.description = !util.isNullOrUndefined(val.description)?val.description[0]:'';
// GC Add content:encoded
if (val['content:encoded']) {
obj.content = val['content:encoded'];
}
obj.url = obj.link = !util.isNullOrUndefined(val.link)?val.link[0]:'';
if (val.category) {
obj.group = val.category[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'];
obj.thumb_square = val['media:content']['media:thumbnail'][0].url[0];
}else if(mainimage){
obj.thumb_square = mainimage;
}
if (val['media:thumbnail']) {
obj.media = val.media || {};
obj.media.thumbnail = val['media:thumbnail'];
//obj.thumb_square = val.media.thumbnail[0].url[0];
//obj.thumb_square = val['media:content'][0]['media:thumbnail'][0].url[0];
//obj.thumb_square = 'TEST_IMAGE';
//obj.thumb_square = val.media.content['media:thumbnail'][0].url[0];
}
if (val['itunes:duration']){
obj.duration = val['itunes:duration'][0];
}
// GC
//if (val['media:thumbnail']) {
//obj.media = val.media.thumbnail || {};
//obj.thumb_square = val.media.thumbnail.url[0];
//obj.thumb_square = 'TEST_IMAGE';
//}
if(!util.isArray(val.enclosure)){
obj.media_url = val.enclosure[0].url[0];
obj.type = val.enclosure[0].type[0];
}
else if(val.enclosure){
if(val.enclosure.length == 1){
obj.media_url = val.enclosure[0].url[0];
obj.type = val.enclosure[0].type[0];
}
}
rss.item.push(obj);
});
}
return rss;
},
read: function(url, callback){
return this.load(url, callback);
}
};