-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeedparser_service.js
52 lines (42 loc) · 1.37 KB
/
feedparser_service.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
49
50
51
52
var FeedParser = require('feedparser'),
request = require('request');
// Feedparser constructor.
var FeedparserService = function() {
this.items = [];
};
// Parses the specified text.
FeedparserService.prototype.parse = function(url, successCallback, errorCallback) {
var self = this;
console.log("quering url: " + url);
var req = request({headers: {"user-agent": "node.js"}, uri: url}),
feedparser = new FeedParser([]);
req.on('error', function (error) {
errorCallback(error);
});
req.on('response', function (res) {
var stream = this;
console.log("res.statusCode: " + res.statusCode);
console.log("res.statusMessage: " + res.statusMessage);
if (res.statusCode != 200){
return this.emit('error', new Error(res.statusCode + " " + res.statusMessage));
}
stream.pipe(feedparser);
});
feedparser.on('error', function(error) {
errorCallback(error);
});
feedparser.on('readable', function() {
// This is where the action is!
var stream = this,
meta = this.meta, // **NOTE** the "meta" is always available in the context of the feedparser instance
item;
while (item = stream.read()) {
self.items.push(item);
}
});
feedparser.on('end', function(error) {
successCallback(self.items);
});
};
// Export the Feedparser constructor from this module.
module.exports = FeedparserService;