Skip to content

Commit

Permalink
issue 21 fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tk120404 committed Jul 20, 2014
1 parent 0be9841 commit 414d30d
Showing 1 changed file with 54 additions and 60 deletions.
114 changes: 54 additions & 60 deletions lib/feed.js
Expand Up @@ -4,7 +4,6 @@ var xml2js = require('xml2js'),
request = require('request'),
URL = require('url'),
iconv = require('iconv-lite');

/**
All you need to do is send a feed URL that can be opened via fs
Options are optional, see xml2js for extensive list
Expand All @@ -17,13 +16,19 @@ function parseURL(feedURL, options, callback) {
callback = options;
options = {};
}
var defaults = {uri: feedURL, jar: false, proxy: false, followRedirect: true, timeout: 1000 * 30};
var defaults = {
uri: feedURL,
jar: false,
proxy: false,
followRedirect: true,
timeout: 1000 * 30
};
options = _.extend(defaults, options);
//check that the protocal is either http or https
var u = URL.parse(feedURL);
if (u.protocol === 'http:' || u.protocol === 'https:') {
//make sure to have a 30 second timeout
var req = request(options, function (err, response, xml) {
var req = request(options, function(err, response, xml) {
if (err || xml === null) {
if (err) {
callback(err, null);
Expand All @@ -32,29 +37,35 @@ function parseURL(feedURL, options, callback) {
}
} else {
if ((typeof response !== "undefined" && response !== null ? response.statusCode : void 0) != null) {
if (response.statusCode >= 400) {
callback("Failed to retrieve source! Invalid response code (" + response.statusCode + ")!", null);
} else {
var encodedXml = iconv.decode(new Buffer(xml), 'ISO-8859-1');
parseString(encodedXml, options, callback);
//issues22
}
if (response.statusCode >= 400) {
callback("Failed to retrieve source! Invalid response code (" + response.statusCode + ")!", null);
} else {
var encodedXml = iconv.decode(new Buffer(xml), 'ISO-8859-1');
parseString(encodedXml, options, callback);
//issues22
}
} else {
callback("Failed to retrieve source! No response code!!", null);
callback("Failed to retrieve source! No response code!!", null);
}
}
});
} else {
callback({error: "Only http or https protocols are accepted"}, null);
callback({
error: "Only http or https protocols are accepted"
}, null);
}
}
module.exports.parseURL = parseURL;

function parseString(xml, options, callback) {
// we need to check that the input in not a null input
if (xml.split('<').length >= 3) {
var parser = new xml2js.Parser({trim: false, normalize: true, mergeAttrs: true});
parser.addListener('end', function (jsonDOM) {
var parser = new xml2js.Parser({
trim: false,
normalize: true,
mergeAttrs: true
});
parser.addListener('end', function(jsonDOM) {
if (jsonDOM) {
//console.log(jsonDOM.rss.channel[0]);
jsonDOM = normalize(jsonDOM);
Expand All @@ -69,7 +80,7 @@ function parseString(xml, options, callback) {
callback("failed to parse xml", null);
}
});
parser.addListener("error", function (err) {
parser.addListener("error", function(err) {
callback(err, null);
});
parser.parseString(xml);
Expand All @@ -78,20 +89,17 @@ function parseString(xml, options, callback) {
}
}
module.exports.parseString = parseString;

//detects if RSS, otherwise assume atom
function isRSS(json) {
return (json.channel != null);
}

// normalizes input to make feed burner work
function normalize(json) {
if (json.rss) {
return json.rss;
}
return json;
}

//xml2js will return commented material in a # tag which can be a pain
//this will remove the # tag and set its child text in it's place
//ment to work on a feed item, so will iterate over json's and check
Expand All @@ -103,17 +111,17 @@ function flattenComments(json) {
}
return json;
}

//formats the RSS feed to the needed outpu
//also parses FeedBurner
function formatRSS(json) {
var output = {'type': 'rss', items: []};
var output = {
'type': 'rss',
items: []
};
var channel = json.channel;

if (_.isArray(json.channel)) {
channel = json.channel[0];
}

if (channel.title) {
output.title = channel.title[0];
}
Expand All @@ -138,21 +146,18 @@ function formatRSS(json) {
if (!_.isArray(channel.item)) {
channel.item = [channel.item];
}
_.each(channel.item, function (val, index) {
_.each(channel.item, function(val, index) {
val = flattenComments(val);
var obj = {}, _ref;
//Tx PaulFreund
obj.title = (_ref = val.title) != undefined && _ref.length>0 ? _ref[0] : void 0;
obj.summary = (_ref = val.description) != undefined && _ref.length>0 ? _ref[0] : void 0;
obj.url = (_ref = val.link) != undefined && _ref.length>0 ? _ref[0] : void 0;
obj.categories = (_ref = val.category) != undefined && _ref.length>0 ? _ref[0] : void 0;


obj.title = (_ref = val.title) != undefined && _ref.length > 0 ? _ref[0] : void 0;
obj.summary = (_ref = val.description) != undefined && _ref.length > 0 ? _ref[0] : void 0;
obj.url = (_ref = val.link) != undefined && _ref.length > 0 ? _ref[0] : void 0;
obj.categories = (_ref = val.category) != undefined && _ref.length > 0 ? _ref[0] : void 0;
// Put the comments instead of the description if there is no description
if (!(obj.summary != null) || obj.summary === '') {
obj.summary = val.comments[0] != null ? val.comments[0] : '';
obj.summary = (_ref = val.comments[0]) ? _ref : '';
}

//since we are going to format the date, we want to make sure it exists
if (val.pubDate) {
//lets try basis js date parsing for now
Expand All @@ -163,20 +168,20 @@ function formatRSS(json) {
if (val['dc:creator']) {
obj.author = val['dc:creator'][0];
}

if (val.author) {
obj.author = val.author[0];
}

//now lets handle the GUID
if (val.guid) {
//xml2js parses this kina odd...
var link = val.guid[0]._;
var param = val.guid[0].isPermaLink;
var isPermaLink = true;
obj.guid = {'link': link, isPermaLink: param};
obj.guid = {
'link': link,
isPermaLink: param
};
}

if (val['media:content']) {
obj.media = val.media || {};
obj.media.content = val['media:content'];
Expand All @@ -185,19 +190,19 @@ function formatRSS(json) {
obj.media = val.media || {};
obj.media.thumbnail = val['media:thumbnail'];
}

//now push the obj onto the stack
output.items.push(obj);
});
}
return output;
}

//formats the ATOM feed to the needed output
function formatATOM(json) {
var output = {'type': 'atom', items: []};
var output = {
'type': 'atom',
items: []
};
var channel = json.feed || json;

if (channel.title) {
output.title = channel.title[0]._;
}
Expand All @@ -209,11 +214,9 @@ function formatATOM(json) {
} else {
output.desc = channel.subtitle;
}

if (channel.link)
if (_.isArray(channel.link)) {
_.each(channel.link, function (val, index) {

_.each(channel.link, function(val, index) {
if (val.type && val.type.indexOf("html") > 0) {
output.link = val.href;
}
Expand All @@ -222,36 +225,31 @@ function formatATOM(json) {
}
});
}

if (channel.id) {
output.id = channel.id[0];
}

if (channel.updated) {
output.last_modified = new Date(channel.updated[0]).toString();
}

if (channel.author) {
output.author = channel.author[0].name[0];
}

//just double check that it exists and that it is an array
if (channel.entry) {
if (!_.isArray(channel.entry)) {
channel.entry = [channel.entry];
}
_.each(channel.entry, function (val, index) {
_.each(channel.entry, function(val, index) {
val = flattenComments(val);
var obj = {}, _ref;
obj.id = val.id[0];
obj.title = (_ref = val.title) != undefined && _ref.length>0 ? _ref[0]._ : void 0;
obj.summary = (_ref = val.content[0]) != undefined && _ref.length>0 ? _ref[0]._ : void 0;

obj.title = (_ref = val.title) != undefined && _ref.length > 0 ? _ref[0]._ : void 0;
obj.summary = (_ref = val.content[0]) != undefined && _ref.length > 0 ? _ref[0]._ : void 0;
var categories = [];
//just grab the category text
if (val.category) {
if (_.isArray(val.category)) {
_.each(val.category, function (val, i) {
_.each(val.category, function(val, i) {
categories.push(val['term']);
});
} else {
Expand All @@ -263,7 +261,7 @@ function formatATOM(json) {
//just get the alternate link
if (val.link) {
if (_.isArray(val.link)) {
_.each(val.link, function (val, i) {
_.each(val.link, function(val, i) {
if (val.rel === 'self') {
link = val.href;
}
Expand All @@ -279,7 +277,6 @@ function formatATOM(json) {
obj.published_at = Date.parse(val.published[0]);
obj.time_ago = DateHelper.time_ago_in_words(obj.published_at);
}

if (val['media:content']) {
obj.media = val.media || {};
obj.media.content = val['media:content'];
Expand All @@ -294,22 +291,20 @@ function formatATOM(json) {
}
return output;
}

var DateHelper = {
// Takes the format of "Jan 15, 2007 15:45:00 GMT" and converts it to a relative time
// Ruby strftime: %b %d, %Y %H:%M:%S GMT
time_ago_in_words_with_parsing: function (from) {
time_ago_in_words_with_parsing: function(from) {
var date = new Date();
date.setTime(Date.parse(from));
return this.time_ago_in_words(date);
},
// Takes a timestamp and converts it to a relative time
// DateHelper.time_ago_in_words(1331079503000)
time_ago_in_words: function (from) {
time_ago_in_words: function(from) {
return this.distance_of_time_in_words(new Date(), from);
},

distance_of_time_in_words: function (to, from) {
distance_of_time_in_words: function(to, from) {
var distance_in_seconds = ((to - from) / 1000);
var distance_in_minutes = Math.floor(distance_in_seconds / 60);
var tense = distance_in_seconds < 0 ? " from now" : " ago";
Expand Down Expand Up @@ -346,5 +341,4 @@ var DateHelper = {
}
return 'over ' + Math.floor(distance_in_minutes / 525960) + ' years';
}
};

};

0 comments on commit 414d30d

Please sign in to comment.