Created
February 4, 2014 16:25
-
-
Save digilord/8807063 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
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 () { | |
var Future = Npm.require('fibers/future'); | |
var future = new Future(); | |
future.return(this); | |
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 stream = future.wait() | |
var item; | |
var bool = true; | |
while (item = stream.read() && 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); | |
} | |
} | |
}); | |
The first attempt had all kind |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment