Skip to content

Instantly share code, notes, and snippets.

@MeoMix
Created August 15, 2013 00:39
Show Gist options
  • Save MeoMix/6237234 to your computer and use it in GitHub Desktop.
Save MeoMix/6237234 to your computer and use it in GitHub Desktop.
getBulkRelatedVideoInformation: function(videoIds, callback) {
// TODO: Maybe abort if takes too long or debug this really well.
var bulkRelatedVideoInformation = [];
var totalVideosToProcess = videoIds.length;
var videosProcessed = 0;
var videosToProcessConcurrently = 5;
var videosProcessing = 0;
var self = this;
var youtubeQueryInterval = setInterval(function () {
if (videosProcessed == totalVideosToProcess) {
clearInterval(youtubeQueryInterval);
callback(bulkRelatedVideoInformation);
}
else {
// Don't flood the network -- process a few at a time.
if (videosProcessing <= videosToProcessConcurrently) {
videosProcessing++;
var currentVideoId = videoIds.pop();
// Use a closure to ensure that I iterate over the proper videoId for each async call.
var getRelatedVideoInfoClosure = function (closureCurrentVideoId) {
self.getRelatedVideoInformation(closureCurrentVideoId, function (relatedVideoInformation) {
// getRelatedVideoInformation might error out.
if (relatedVideoInformation) {
bulkRelatedVideoInformation.push({
videoId: closureCurrentVideoId,
relatedVideoInformation: relatedVideoInformation
});
}
videosProcessed++;
videosProcessing--;
});
};
getRelatedVideoInfoClosure(currentVideoId);
}
}
}, 200);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment