Is there a way to stop reading the feed?
Opened this issue · 0 comments
snaka commented
Sorry for the silly question. (I'm not familiar with stream)
I wonder whether it is possible to stop reading before end of feed reached.
I had tried code below.
var myProductName = "feedParserDemo"; myVersion = "0.4.3";
const request = require ("request");
const FeedParser = require ("feedparser");
//const urlTestFeed = "http://www.guardian.co.uk/world/usa/rss";
const urlTestFeed = "http://feeds.backspace.fm/backspacefm"; // <-- ### CHANGE FEED URL ###
function getFeed (urlfeed, callback) {
var req = request (urlfeed);
var feedparser = new FeedParser ();
var feedItems = new Array ();
req.on ("response", function (response) {
var stream = this;
if (response.statusCode == 200) {
stream.pipe (feedparser);
}
});
req.on ("error", function (err) {
console.log ("getFeed: err.message == " + err.message);
});
feedparser.on ("readable", function () {
try {
var item = this.read (), flnew;
if (item !== null) { //2/9/17 by DW
feedItems.push (item);
}
feedparser.end(); // <--- ### ADD THIS LINE ###
}
catch (err) {
console.log ("getFeed: err.message == " + err.message);
}
});
feedparser.on ("end", function () {
callback (undefined, feedItems);
});
feedparser.on ("error", function (err) {
console.log ("getFeed: err.message == " + err.message);
callback (err);
});
}
console.log ("\n" + myProductName + " v" + myVersion + ".\n");
getFeed (urlTestFeed, function (err, feedItems) {
if (!err) {
function pad (num) {
var s = num.toString (), ctplaces = 3;
while (s.length < ctplaces) {
s = "0" + s;
}
return (s);
}
console.log ("There are " + feedItems.length + " items in the feed.\n");
for (var i = 0; i < feedItems.length; i++) {
console.log ("Item #" + pad (i) + ": " + feedItems [i].title + ".\n");
}
}
});
Then result was like below.
feedParserDemo v0.4.3.
There are 1 items in the feed.
Item #000: #254: <item title .... >
This output is expected.
But , It is kept waiting long until the program ends.
Is there a proper way to stop reading the feed?