Created
April 17, 2014 08:44
-
-
Save FrankGrimm/10965590 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var FeedParser, Iconv, Rss, request, server; | |
FeedParser = require('feedparser'); | |
request = require('request'); | |
Iconv = require('iconv'); | |
Rss = (function() { | |
var getParams; | |
function Rss() {} | |
Rss.prototype.getMessages = function(feed, cb) { | |
var feedparser, req; | |
this.items = []; | |
var self = this; // store a reference to our instance | |
req = request(feed, { | |
timeout: 10000, | |
pool: false | |
}); | |
req.setMaxListeners(50); | |
req.setHeader('user-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36'); | |
feedparser = new FeedParser(); | |
req.on('error', new Rss().done); | |
req.on('response', function(res) { | |
var charset, iconv, stream; | |
stream = this; | |
iconv = void 0; | |
charset = void 0; | |
if (res.statusCode !== 200) { | |
return this.emit("error", new Error("Bad status code")); | |
} | |
charset = getParams(res.headers["content-type"] || "").charset; | |
if (!iconv && charset && !/utf-*8/i.test(charset)) { | |
try { | |
iconv = new Iconv(charset, "utf-8"); | |
console.log("Converting from charset %s to utf-8", charset); | |
iconv.on("error", done); | |
stream = this.pipe(iconv); | |
} catch (err) { | |
this.emit("error", err); | |
} | |
} | |
return stream.pipe(feedparser); | |
}); | |
feedparser.on("error", cb); // use your cb as error handler | |
feedparser.on("end", function() { | |
// feed ended, call your callback with all data we buffered (might want to reset the buffer here) | |
console.log('end'); | |
cb(null, self.items); | |
}); | |
return feedparser.on("readable", function() { | |
console.log('readable'); | |
var post; // buffer all data in here w/o calling the cb function | |
post = void 0; | |
// this.items = []; - you would have resetted your buffer everytime you got new data here, also "this" refers to the feedparser instance, not getMessages. better use self here | |
while (post = this.read()) { | |
self.items.push(post.pubdate + "\n" + post.title.replace(/..\d{4}/, "") + "\n" + post.link + "\n"); | |
} | |
}); | |
}; | |
getParams = function(str) { | |
var params; | |
params = str.split(";").reduce(function(params, param) { | |
var parts; | |
parts = param.split("=").map(function(part) { | |
return part.trim(); | |
}); | |
if (parts.length === 2) params[parts[0]] = parts[1]; | |
return params; | |
}, {}); | |
return params; | |
}; | |
Rss.prototype.done = function(err) { | |
if (err) { | |
console.log(err, err.stack); | |
} | |
}; | |
return Rss; | |
})(); | |
if (require.main === module) { | |
server = require("http").createServer(function(req, res) { | |
var stream; | |
stream = require("fs").createReadStream(req.url); | |
res.setHeader("Content-Type", "text/xml; charset=Windows-1251"); | |
stream.pipe(res.title); | |
}); | |
server.listen(0, function(err, cb) { | |
var r = new Rss(); | |
return r.getMessages("http://feeds.feedburner.com/niebezpiecznik?format=xml", function(err, items) { | |
if (err) { | |
console.log(err, err.stack); | |
} else { | |
console.log('Items:') | |
console.log(items) | |
} | |
}); | |
}); | |
} | |
exports.Rss = Rss; | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment