Skip to content

Commit 414d30d

Browse files
committed
issue 21 fix
1 parent 0be9841 commit 414d30d

File tree

1 file changed

+54
-60
lines changed

1 file changed

+54
-60
lines changed

lib/feed.js

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var xml2js = require('xml2js'),
44
request = require('request'),
55
URL = require('url'),
66
iconv = require('iconv-lite');
7-
87
/**
98
All you need to do is send a feed URL that can be opened via fs
109
Options are optional, see xml2js for extensive list
@@ -17,13 +16,19 @@ function parseURL(feedURL, options, callback) {
1716
callback = options;
1817
options = {};
1918
}
20-
var defaults = {uri: feedURL, jar: false, proxy: false, followRedirect: true, timeout: 1000 * 30};
19+
var defaults = {
20+
uri: feedURL,
21+
jar: false,
22+
proxy: false,
23+
followRedirect: true,
24+
timeout: 1000 * 30
25+
};
2126
options = _.extend(defaults, options);
2227
//check that the protocal is either http or https
2328
var u = URL.parse(feedURL);
2429
if (u.protocol === 'http:' || u.protocol === 'https:') {
2530
//make sure to have a 30 second timeout
26-
var req = request(options, function (err, response, xml) {
31+
var req = request(options, function(err, response, xml) {
2732
if (err || xml === null) {
2833
if (err) {
2934
callback(err, null);
@@ -32,29 +37,35 @@ function parseURL(feedURL, options, callback) {
3237
}
3338
} else {
3439
if ((typeof response !== "undefined" && response !== null ? response.statusCode : void 0) != null) {
35-
if (response.statusCode >= 400) {
36-
callback("Failed to retrieve source! Invalid response code (" + response.statusCode + ")!", null);
37-
} else {
38-
var encodedXml = iconv.decode(new Buffer(xml), 'ISO-8859-1');
39-
parseString(encodedXml, options, callback);
40-
//issues22
41-
}
40+
if (response.statusCode >= 400) {
41+
callback("Failed to retrieve source! Invalid response code (" + response.statusCode + ")!", null);
42+
} else {
43+
var encodedXml = iconv.decode(new Buffer(xml), 'ISO-8859-1');
44+
parseString(encodedXml, options, callback);
45+
//issues22
46+
}
4247
} else {
43-
callback("Failed to retrieve source! No response code!!", null);
48+
callback("Failed to retrieve source! No response code!!", null);
4449
}
4550
}
4651
});
4752
} else {
48-
callback({error: "Only http or https protocols are accepted"}, null);
53+
callback({
54+
error: "Only http or https protocols are accepted"
55+
}, null);
4956
}
5057
}
5158
module.exports.parseURL = parseURL;
5259

5360
function parseString(xml, options, callback) {
5461
// we need to check that the input in not a null input
5562
if (xml.split('<').length >= 3) {
56-
var parser = new xml2js.Parser({trim: false, normalize: true, mergeAttrs: true});
57-
parser.addListener('end', function (jsonDOM) {
63+
var parser = new xml2js.Parser({
64+
trim: false,
65+
normalize: true,
66+
mergeAttrs: true
67+
});
68+
parser.addListener('end', function(jsonDOM) {
5869
if (jsonDOM) {
5970
//console.log(jsonDOM.rss.channel[0]);
6071
jsonDOM = normalize(jsonDOM);
@@ -69,7 +80,7 @@ function parseString(xml, options, callback) {
6980
callback("failed to parse xml", null);
7081
}
7182
});
72-
parser.addListener("error", function (err) {
83+
parser.addListener("error", function(err) {
7384
callback(err, null);
7485
});
7586
parser.parseString(xml);
@@ -78,20 +89,17 @@ function parseString(xml, options, callback) {
7889
}
7990
}
8091
module.exports.parseString = parseString;
81-
8292
//detects if RSS, otherwise assume atom
8393
function isRSS(json) {
8494
return (json.channel != null);
8595
}
86-
8796
// normalizes input to make feed burner work
8897
function normalize(json) {
8998
if (json.rss) {
9099
return json.rss;
91100
}
92101
return json;
93102
}
94-
95103
//xml2js will return commented material in a # tag which can be a pain
96104
//this will remove the # tag and set its child text in it's place
97105
//ment to work on a feed item, so will iterate over json's and check
@@ -103,17 +111,17 @@ function flattenComments(json) {
103111
}
104112
return json;
105113
}
106-
107114
//formats the RSS feed to the needed outpu
108115
//also parses FeedBurner
109116
function formatRSS(json) {
110-
var output = {'type': 'rss', items: []};
117+
var output = {
118+
'type': 'rss',
119+
items: []
120+
};
111121
var channel = json.channel;
112-
113122
if (_.isArray(json.channel)) {
114123
channel = json.channel[0];
115124
}
116-
117125
if (channel.title) {
118126
output.title = channel.title[0];
119127
}
@@ -138,21 +146,18 @@ function formatRSS(json) {
138146
if (!_.isArray(channel.item)) {
139147
channel.item = [channel.item];
140148
}
141-
_.each(channel.item, function (val, index) {
149+
_.each(channel.item, function(val, index) {
142150
val = flattenComments(val);
143151
var obj = {}, _ref;
144152
//Tx PaulFreund
145-
obj.title = (_ref = val.title) != undefined && _ref.length>0 ? _ref[0] : void 0;
146-
obj.summary = (_ref = val.description) != undefined && _ref.length>0 ? _ref[0] : void 0;
147-
obj.url = (_ref = val.link) != undefined && _ref.length>0 ? _ref[0] : void 0;
148-
obj.categories = (_ref = val.category) != undefined && _ref.length>0 ? _ref[0] : void 0;
149-
150-
153+
obj.title = (_ref = val.title) != undefined && _ref.length > 0 ? _ref[0] : void 0;
154+
obj.summary = (_ref = val.description) != undefined && _ref.length > 0 ? _ref[0] : void 0;
155+
obj.url = (_ref = val.link) != undefined && _ref.length > 0 ? _ref[0] : void 0;
156+
obj.categories = (_ref = val.category) != undefined && _ref.length > 0 ? _ref[0] : void 0;
151157
// Put the comments instead of the description if there is no description
152158
if (!(obj.summary != null) || obj.summary === '') {
153-
obj.summary = val.comments[0] != null ? val.comments[0] : '';
159+
obj.summary = (_ref = val.comments[0]) ? _ref : '';
154160
}
155-
156161
//since we are going to format the date, we want to make sure it exists
157162
if (val.pubDate) {
158163
//lets try basis js date parsing for now
@@ -163,20 +168,20 @@ function formatRSS(json) {
163168
if (val['dc:creator']) {
164169
obj.author = val['dc:creator'][0];
165170
}
166-
167171
if (val.author) {
168172
obj.author = val.author[0];
169173
}
170-
171174
//now lets handle the GUID
172175
if (val.guid) {
173176
//xml2js parses this kina odd...
174177
var link = val.guid[0]._;
175178
var param = val.guid[0].isPermaLink;
176179
var isPermaLink = true;
177-
obj.guid = {'link': link, isPermaLink: param};
180+
obj.guid = {
181+
'link': link,
182+
isPermaLink: param
183+
};
178184
}
179-
180185
if (val['media:content']) {
181186
obj.media = val.media || {};
182187
obj.media.content = val['media:content'];
@@ -185,19 +190,19 @@ function formatRSS(json) {
185190
obj.media = val.media || {};
186191
obj.media.thumbnail = val['media:thumbnail'];
187192
}
188-
189193
//now push the obj onto the stack
190194
output.items.push(obj);
191195
});
192196
}
193197
return output;
194198
}
195-
196199
//formats the ATOM feed to the needed output
197200
function formatATOM(json) {
198-
var output = {'type': 'atom', items: []};
201+
var output = {
202+
'type': 'atom',
203+
items: []
204+
};
199205
var channel = json.feed || json;
200-
201206
if (channel.title) {
202207
output.title = channel.title[0]._;
203208
}
@@ -209,11 +214,9 @@ function formatATOM(json) {
209214
} else {
210215
output.desc = channel.subtitle;
211216
}
212-
213217
if (channel.link)
214218
if (_.isArray(channel.link)) {
215-
_.each(channel.link, function (val, index) {
216-
219+
_.each(channel.link, function(val, index) {
217220
if (val.type && val.type.indexOf("html") > 0) {
218221
output.link = val.href;
219222
}
@@ -222,36 +225,31 @@ function formatATOM(json) {
222225
}
223226
});
224227
}
225-
226228
if (channel.id) {
227229
output.id = channel.id[0];
228230
}
229-
230231
if (channel.updated) {
231232
output.last_modified = new Date(channel.updated[0]).toString();
232233
}
233-
234234
if (channel.author) {
235235
output.author = channel.author[0].name[0];
236236
}
237-
238237
//just double check that it exists and that it is an array
239238
if (channel.entry) {
240239
if (!_.isArray(channel.entry)) {
241240
channel.entry = [channel.entry];
242241
}
243-
_.each(channel.entry, function (val, index) {
242+
_.each(channel.entry, function(val, index) {
244243
val = flattenComments(val);
245244
var obj = {}, _ref;
246245
obj.id = val.id[0];
247-
obj.title = (_ref = val.title) != undefined && _ref.length>0 ? _ref[0]._ : void 0;
248-
obj.summary = (_ref = val.content[0]) != undefined && _ref.length>0 ? _ref[0]._ : void 0;
249-
246+
obj.title = (_ref = val.title) != undefined && _ref.length > 0 ? _ref[0]._ : void 0;
247+
obj.summary = (_ref = val.content[0]) != undefined && _ref.length > 0 ? _ref[0]._ : void 0;
250248
var categories = [];
251249
//just grab the category text
252250
if (val.category) {
253251
if (_.isArray(val.category)) {
254-
_.each(val.category, function (val, i) {
252+
_.each(val.category, function(val, i) {
255253
categories.push(val['term']);
256254
});
257255
} else {
@@ -263,7 +261,7 @@ function formatATOM(json) {
263261
//just get the alternate link
264262
if (val.link) {
265263
if (_.isArray(val.link)) {
266-
_.each(val.link, function (val, i) {
264+
_.each(val.link, function(val, i) {
267265
if (val.rel === 'self') {
268266
link = val.href;
269267
}
@@ -279,7 +277,6 @@ function formatATOM(json) {
279277
obj.published_at = Date.parse(val.published[0]);
280278
obj.time_ago = DateHelper.time_ago_in_words(obj.published_at);
281279
}
282-
283280
if (val['media:content']) {
284281
obj.media = val.media || {};
285282
obj.media.content = val['media:content'];
@@ -294,22 +291,20 @@ function formatATOM(json) {
294291
}
295292
return output;
296293
}
297-
298294
var DateHelper = {
299295
// Takes the format of "Jan 15, 2007 15:45:00 GMT" and converts it to a relative time
300296
// Ruby strftime: %b %d, %Y %H:%M:%S GMT
301-
time_ago_in_words_with_parsing: function (from) {
297+
time_ago_in_words_with_parsing: function(from) {
302298
var date = new Date();
303299
date.setTime(Date.parse(from));
304300
return this.time_ago_in_words(date);
305301
},
306302
// Takes a timestamp and converts it to a relative time
307303
// DateHelper.time_ago_in_words(1331079503000)
308-
time_ago_in_words: function (from) {
304+
time_ago_in_words: function(from) {
309305
return this.distance_of_time_in_words(new Date(), from);
310306
},
311-
312-
distance_of_time_in_words: function (to, from) {
307+
distance_of_time_in_words: function(to, from) {
313308
var distance_in_seconds = ((to - from) / 1000);
314309
var distance_in_minutes = Math.floor(distance_in_seconds / 60);
315310
var tense = distance_in_seconds < 0 ? " from now" : " ago";
@@ -346,5 +341,4 @@ var DateHelper = {
346341
}
347342
return 'over ' + Math.floor(distance_in_minutes / 525960) + ' years';
348343
}
349-
};
350-
344+
};

0 commit comments

Comments
 (0)