Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed Atom parser

  • Loading branch information...
commit fd8436567ba21dabe71fb5948f888ede6fb5e27c 1 parent af09a01
@cs138 cs138 authored
Showing with 84 additions and 59 deletions.
  1. +81 −56 lib/feed.js
  2. +1 −1  npm-shrinkwrap.json
  3. +2 −2 package.json
View
137 lib/feed.js
@@ -12,18 +12,18 @@ var URL = require('url');
The returned formats will be structurally the same, but you should still check the 'format' property
**/
function parseURL(feedURL, options, callback) {
- if (typeof options == 'function' && !callback) {
+ if (typeof options === 'function' && !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:') {
+ if (u.protocol === 'http:' || u.protocol === 'https:') {
//make sure to have a 30 second timeout
var req = request(options, function (err, response, xml) {
- if (err || xml == null) {
+ if (err || xml === null) {
if (err) {
callback(err, null);
} else {
@@ -34,7 +34,7 @@ function parseURL(feedURL, options, callback) {
}
});
} else {
- callback({error:"Only http or https protocalls are accepted"}, null);
+ callback({error: "Only http or https protocols are accepted"}, null);
}
}
module.exports.parseURL = parseURL;
@@ -42,7 +42,7 @@ 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});
+ var parser = new xml2js.Parser({trim: false, normalize: true, mergeAttrs: true});
parser.addListener('end', function (jsonDOM) {
if (jsonDOM) {
//console.log(jsonDOM.rss.channel[0]);
@@ -98,7 +98,7 @@ function flattenComments(json) {
function formatRSS(json) {
//var output = {'type':'rss', metadata:{}, items:[]};
//Start with the metadata for the feed
- var output = {'type':'rss',items:[]};
+ var output = {'type': 'rss', items: []};
var metadata = {};
var channel = json.channel;
@@ -124,8 +124,6 @@ function formatRSS(json) {
if (channel.ttl) {
output.ttl = channel.ttl[0];
}
-
-
//ok, now lets get into the meat of the feed
//just double check that it exists
if (channel.item) {
@@ -148,12 +146,11 @@ function formatRSS(json) {
obj.time_ago = DateHelper.time_ago_in_words(obj.published_at);
}
///wordpress author
- if(val['dc:creator']){
+ if (val['dc:creator']) {
obj.author = val['dc:creator'][0];
}
- if(val.author)
- {
+ if (val.author) {
obj.author = val.author[0];
}
@@ -164,10 +161,7 @@ function formatRSS(json) {
var link = val.guid[0]._;
var param = val.guid[0].isPermaLink;
var isPermaLink = true;
- //if(param){
- // isPermaLink = param.isPermaLink;
- //}
- obj.guid = {'link':link, isPermaLink:param};
+ obj.guid = {'link': link, isPermaLink: param};
}
//now push the obj onto the stack
output.items.push(obj);
@@ -177,32 +171,49 @@ function formatRSS(json) {
}
//formats the ATOM feed to the needed output
-//yes, this is a shamless copy-pasta of the RSS code (its all the same structure!)
function formatATOM(json) {
- var output = {'type':'atom', metadata:{}, items:[]};
+ var output = {'type': 'atom', items: []};
//Start with the metadata for the feed
var metadata = {};
- var channel = json;
+ var channel = json.feed;
+ if (typeof channel.title === "object") {
+ console.log(channel.title);
+ }
if (channel.title) {
- metadata.title = channel.title;
+ output.title = channel.title[0]._;
}
- if (channel.subtitle) {
- metadata.desc = channel.subtitle;
+ if (_.isArray(channel.subtitle)) {
+ if (channel.subtitle[0]._) {
+ output.desc = channel.subtitle[0]._;
+ }
+ } else {
+ output.desc = channel.subtitle;
}
- if (channel.link) {
- metadata.url = channel.link;
+
+ if (_.isArray(channel.link)) {
+ _.each(channel.link, function (val, index) {
+
+ if (val.type && val.type.indexOf("html") > 0) {
+ output.link = val.href;
+ }
+ if (val.rel === "hub") {
+ output.hub = val.href;
+ }
+ });
}
+
if (channel.id) {
- metadata.id = channel.id;
+ output.id = channel.id[0];
}
- if (channel.update) {
- metadata.update = channel.update;
+
+ if (channel.updated) {
+ output.last_modified = new Date(channel.updated[0]).toString();
}
+
if (channel.author) {
- metadata.author = channel.author;
+ output.author = channel.author[0].name[0];
}
- output.metadata = metadata;
//just double check that it exists and that it is an array
if (channel.entry) {
if (!_.isArray(channel.entry)) {
@@ -211,15 +222,13 @@ function formatATOM(json) {
_.each(channel.entry, function (val, index) {
val = flattenComments(val);
var obj = {};
- obj.id = val.id;
+ obj.id = val.id[0];
if (!val.title) {
console.log(json);
}
- obj.title = val.title;
+ obj.title = val.title[0]._;
if (val.content) {
- obj.desc = val.content;
- } else if (val.summary) {
- obj.desc = val.summary;
+ obj.summary = val.content[0]._;
}
var categories = [];
//just grab the category text
@@ -238,7 +247,7 @@ function formatATOM(json) {
if (val.link) {
if (_.isArray(val.link)) {
_.each(val.link, function (val, i) {
- if (val.rel == 'alternate') {
+ if (val.rel === 'self') {
link = val.href;
}
});
@@ -250,11 +259,8 @@ function formatATOM(json) {
//since we are going to format the date, we want to make sure it exists
if (val.published) {
//lets try basis js date parsing for now
- obj.date = Date.parse(val.published);
- }
- if (val.updated) {
- //lets try basis js date parsing for now
- obj.updated = Date.parse(val.updated);
+ obj.published_at = Date.parse(val.published[0]);
+ obj.time_ago = DateHelper.time_ago_in_words(obj.published_at);
}
//now push the obj onto the stack
output.items.push(obj);
@@ -266,33 +272,52 @@ function formatATOM(json) {
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) {
- var date = new Date;
+ 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) {
- return this.distance_of_time_in_words(new Date, 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";
distance_in_minutes = Math.abs(distance_in_minutes);
- if (distance_in_minutes == 0) { return 'less than a minute'+tense; }
- if (distance_in_minutes == 1) { return 'a minute'+tense; }
- if (distance_in_minutes < 45) { return distance_in_minutes + ' minutes'+tense; }
- if (distance_in_minutes < 90) { return 'about an hour'+tense; }
- if (distance_in_minutes < 1440) { return 'about ' + Math.floor(distance_in_minutes / 60) + ' hours'+tense; }
- if (distance_in_minutes < 2880) { return 'a day'+tense; }
- if (distance_in_minutes < 43200) { return Math.floor(distance_in_minutes / 1440) + ' days'+tense; }
- if (distance_in_minutes < 86400) { return 'about a month'+tense; }
- if (distance_in_minutes < 525960) { return Math.floor(distance_in_minutes / 43200) + ' months'+tense; }
- if (distance_in_minutes < 1051199) { return 'about a year'+tense; }
-
+ if (distance_in_minutes === 0) {
+ return 'less than a minute' + tense;
+ }
+ if (distance_in_minutes === 1) {
+ return 'a minute' + tense;
+ }
+ if (distance_in_minutes < 45) {
+ return distance_in_minutes + ' minutes' + tense;
+ }
+ if (distance_in_minutes < 90) {
+ return 'about an hour' + tense;
+ }
+ if (distance_in_minutes < 1440) {
+ return 'about ' + Math.floor(distance_in_minutes / 60) + ' hours' + tense;
+ }
+ if (distance_in_minutes < 2880) {
+ return 'a day' + tense;
+ }
+ if (distance_in_minutes < 43200) {
+ return Math.floor(distance_in_minutes / 1440) + ' days' + tense;
+ }
+ if (distance_in_minutes < 86400) {
+ return 'about a month' + tense;
+ }
+ if (distance_in_minutes < 525960) {
+ return Math.floor(distance_in_minutes / 43200) + ' months' + tense;
+ }
+ if (distance_in_minutes < 1051199) {
+ return 'about a year' + tense;
+ }
return 'over ' + Math.floor(distance_in_minutes / 525960) + ' years';
}
};
View
2  npm-shrinkwrap.json
@@ -1,6 +1,6 @@
{
"name": "rssparser",
- "version": "0.0.15",
+ "version": "0.0.2"
"dependencies": {
"xml2js": {
"version": "0.2.0",
View
4 package.json
@@ -1,6 +1,6 @@
{
"name": "rssparser",
- "version": "0.0.1",
+ "version": "0.0.2",
"description": "rssparser is an all purpose RSS/ATOM feed parser that parses feeds into a JSON format.",
"keywords": [
"rss",
@@ -26,6 +26,6 @@
"main": "./lib/feed.js",
"readme": "node-rssparser",
"readmeFilename": "README.md",
- "_id": "rssparser@0.0.1",
+ "_id": "rssparser@0.0.2",
"_from": "rssparser"
}
Please sign in to comment.
Something went wrong with that request. Please try again.