Skip to content

Instantly share code, notes, and snippets.

@digilord
Created February 4, 2014 16:39
Show Gist options
  • Save digilord/8807361 to your computer and use it in GitHub Desktop.
Save digilord/8807361 to your computer and use it in GitHub Desktop.
processFeed = function(item, feedID, lastFeedInserted) {
console.log("last inserted: " + lastFeedInserted + " New feed: " + item.title);
Feeds.insert({
feedID: feedID,
title: item.title
});
};
Meteor.methods({
addFeeds: function() {
try {
var allowedUrls = AllowedUrls.find();
var Future = Npm.require('fibers/future');
allowedUrls.forEach(function(url) {
var lastFeedInserted = Feeds.findOne({ feedID: url._id });
request(url.feedUrl)
.pipe(new FeedParser())
.on('error', function(error) {
})// always handle errors
.on('meta', function (meta) {
})// do something
.on('readable', function () {
console.log("onreadable called");
//===============================================================
// Get the last inserted feed
// iterate through new fetched feeds
// check if the feed mathces last inserted feed
// if it does NOT, save it
//===============================================================
var future = new Future();
var stream = this;
future.return(stream.read());
var item;
var bool = true;
while (item = future.wait() && bool) {
console.log("bool: ", bool);
if (lastFeedInserted) {
if (lastFeedInserted.title !== item.title) {
console.log("processFeed");
var processFeedOptions = {
item: item,
feedID: url._id,
lastFeedInserted: lastFeedInserted
}
console.log("sucess!!!, feed iserted");
} else {
console.log("bool is being set to false");
bool = false;
break;
}
} else {
var processFeedOptions = {
item: item,
feedID: url._id,
lastFeedInserted: lastFeedInserted
}
console.log("brand new feed inserted");
}
// Calling a common processFeed so as to not duplicate code.
Meteor.bindEnvironment(function(processFeedOptions){
processFeed(processFeedOptions);
}, function(e){
throw e;
});
}
});
});
} catch(e) {
console.log("I should go home.", e);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment