Skip to content

Instantly share code, notes, and snippets.

@scripting
Last active January 16, 2018 18:09
Show Gist options
  • Save scripting/c3eab6e60073678325593c20c1f9fece to your computer and use it in GitHub Desktop.
Save scripting/c3eab6e60073678325593c20c1f9fece to your computer and use it in GitHub Desktop.
New way to read XML feeds in River5, see first comment for pointer to context
function readXmlFeed (urlfeed) { //1/16/18 by DW
//Instead of processing items as they come in, gather them into an array, and process them at the end.
var req = myRequestCall (urlfeed);
var feedparser = new FeedParser ();
var feedItems = new Array ();
req.on ("response", function (response) {
var stream = this;
serverStats.ctActiveThreads--;
if (response.statusCode == 200) {
stream.pipe (feedparser);
}
else {
feedError ("readFeed: response.statusCode == " + response.statusCode);
}
});
req.on ("error", function (res) {
feedError ();
});
feedparser.on ("readable", function () {
try {
var item = this.read (), flnew;
if (item !== null) { //2/9/17 by DW
processFeedItem (item);
feedItems.push (item);
}
}
catch (err) {
myConsoleLog ("readXmlFeed: error == " + err.message);
}
});
feedparser.on ("error", function () {
feedError ();
});
feedparser.on ("end", function () {
getFeedRiver (urlfeed, function (jstruct) { //make sure the feed's river is loaded in cache
myConsoleLog ("readXmlFeed: feedItems.length == " + feedItems.length);
for (var i = 0; i < feedItems.length; i++) {
processFeedItem (feedItems [i]);
}
finishFeedProcessing ();
});
});
}
@scripting
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment