Skip to content

Instantly share code, notes, and snippets.

@drguildo
Created June 16, 2017 14:23
Show Gist options
  • Save drguildo/07760ddbf0e207ecd88687b705b54c05 to your computer and use it in GitHub Desktop.
Save drguildo/07760ddbf0e207ecd88687b705b54c05 to your computer and use it in GitHub Desktop.
Hacker News post fetcher
var request = require("request");
var FeedParser = require("feedparser");
var req = request("https://news.ycombinator.com/rss");
var feedparser = new FeedParser();
req.on("error", function (error) {
console.log(error);
});
req.on("response", function (res) {
var stream = this; // `this` is `req`, which is a stream
if (res.statusCode !== 200) {
this.emit("error", new Error("Bad status code"));
} else {
stream.pipe(feedparser);
}
});
feedparser.on("error", function (error) {
console.log(error);
});
feedparser.on("readable", function () {
var stream = this; // `this` is `feedparser`, which is a stream
var meta = this.meta; // **NOTE** the "meta" is always available in the context of the feedparser instance
var item;
while (item = stream.read()) {
console.log(`${item.title}: ${item.link}`);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment