Skip to content

Instantly share code, notes, and snippets.

@fb55
Created June 12, 2011 10:20
Show Gist options
  • Save fb55/1021408 to your computer and use it in GitHub Desktop.
Save fb55/1021408 to your computer and use it in GitHub Desktop.
A simple (not feature complete) feedparser for node.js. Requires sax.js
/*
Simple feedparser
Based upon:
- https://raw.github.com/drudge/node-easyrss
*/
var sax = require('./libs/sax');
module.exports.streamParser = function(){
var articles = this.articles = [];
var feed = this.feed = {
title: "",
link: "",
parseError: false,
articles: this.articles
};
var parser = sax.parser(false, {
trim : true,
normalize: true,
lowercasetags : true
});
parser.onerror = function(e){
this.feed.parseError = e || true;
};
var writeCache, closed;
this.evaluate = function(data){
if(!closed){
parser.write(data).close();
closed = true;
}
else
this.write(data);
};
this.write = function(data){
writeCache += data;
};
this.end = parser.onend = function(){
closed = false;
if(writeCache)
this.evaluate(writeCache);
writeCache = "";
};
var itemCount = 0,
inItem = false,
charCache = "";
parser.onopentag = function(tag){
if(tag.name === "item" || tag.name === "entry"){
inItem = true;
articles[itemCount] = {};
}
};
parser.onclosetag = function(tagName){
if(inItem)
switch(tagName){
case "pubdate":
articles[itemCount]["pubdate"] = new Date(charCache); break;
case "content": case "content:encoded":
articles[itemCount]["content"] = charCache; break;
case "dc:creator": case "author":
articles[itemCount]["author"] = charCache; break;
case "description": case "summary":
case "link": case "title":
case "comments":
case "guid":
articles[itemCount][tagName] = charCache; break;
case "item": case "entry":
inItem = false; itemCount++; break;
default: console.log(tagName);
}
else
//get feed info
switch(tagName){
case "title":
feed.title = charCache; break;
case "link":
feed.link = charCache; break;
}
charCache = "";
};
parser.ontext = parser.oncdata = function(text){
if(charCache === "")
charCache = text;
else
charCache += " " + text;
};
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment