使用jquery / ajax获取给定用户的所有YoutubevideoID
我在获取所有YoutubevideoID方面遇到了问题。
我试图从Youtube获取所有videoID来获取给定的Youtube用户名,但问题是youtube一次只允许50个结果。 获取所有Youtube ID的最佳方式是什么? 这是我现在的代码:
function getYouTubeAllInfo() { $.getJSON('http://gdata.youtube.com/feeds/users/Revision3/uploads?alt=json-in-script&callback=?&start-index=1&max-results=49', function(data){ $.each(data.feed.entry, function(i, item){ var id = item['id']['$t']; id = id.replace("http://gdata.youtube.com/feeds/videos/",""); $("#video").append(' ['+id+'] ['+i+'] '); $("#video").append(''); }); }); } $(document).ready(function () { getYouTubeAllInfo(); });
假设您可以获得video数量,您可以执行以下操作:
int currentIndex = 1 int count = GET_COUNT_SOMEHOW; while (currentIndex < count) { $.getJSON('http://gdata.youtube.com/feeds/users/Revision3/uploads?alt=json-in-script&callback=?&start-index=' + currentIndex + '&max-results=49', function(data){ $.each(data.feed.entry, function(i, item){ var id = item['id']['$t']; id = id.replace("http://gdata.youtube.com/feeds/videos/",""); $("#video").append(' ['+id+'] ['+i+'] '); $("#video").append(''); }); }); currentIndex += 50; }
您可以递归调用它,直到没有更多结果:
function getYouTubeAllInfo(perpage, page, recursive) { // $("#video").append(' http://gdata.youtube.com/feeds/users/Revision3/uploads?alt=json-in-script&callback=?&start-index='+(((page-1)*perpage)+1)+'&max-results='+perpage+' '); //debug $.getJSON('http://gdata.youtube.com/feeds/users/Revision3/uploads?alt=json-in-script&callback=?&start-index='+(((page-1)*perpage)+1)+'&max-results='+perpage, function(data){ $.each(data.feed.entry, function(i, item){ var id = item['id']['$t']; id = id.replace("http://gdata.youtube.com/feeds/videos/",""); $("#video").append(' ['+id+'] ['+i+'] '); //alert(page+perpage-2); if(i == perpage-2 && recursive){ getYouTubeAllInfo(perpage, page+1, recursive); } }); }); } getYouTubeAllInfo(49, 1, true);
你可以在这里测试一下: http : //jsfiddle.net/Xemgz/8/
这是一个缓慢的过程,这个用户有很多video。