-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeed.js
49 lines (37 loc) · 1.04 KB
/
feed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
var FeedParser = require('feedparser');
var request = require('request');
exports.getFeeds = function (cb) {
var req = request('https://news.ycombinator.com/rss');
var feedparser = new FeedParser();
var all_feeds = [];
req.on('error', function (error) {
// handle any request errors
});
req.on('response', function (res) {
var stream = this;
if (res.statusCode != 200) {
return this.emit('error', new Error('Bad status code'));
}
stream.pipe(feedparser);
});
feedparser.on('error', function(error) {
// always handle errors
});
feedparser.on('readable', function() {
// This is where the action is!
var stream = this
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);
all_feeds.push(item);
}
});
feedparser.on('end', function done(err) {
if (err) {
console.error(err, err.stack);
} else {
return cb(all_feeds);
}
});
}