Created
June 16, 2017 14:23
-
-
Save drguildo/07760ddbf0e207ecd88687b705b54c05 to your computer and use it in GitHub Desktop.
Hacker News post fetcher
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
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