使用jquery/ajax获取所有Youtube视频ID';对于给定的用户

使用jquery/ajax获取所有Youtube视频ID';对于给定的用户,jquery,Jquery,我在获取所有Youtube视频id时遇到问题 我正在尝试从Youtube获取给定Youtube用户名的所有视频id,但问题是Youtube一次只允许50个结果。获取所有Youtube id的最佳方式是什么。 这是我现在拥有的代码: <div id="video"></div> <script> function getYouTubeAllInfo() { $.getJSON('http://gdata.youtube.com/feeds/u

我在获取所有Youtube视频id时遇到问题

我正在尝试从Youtube获取给定Youtube用户名的所有视频id,但问题是Youtube一次只允许50个结果。获取所有Youtube id的最佳方式是什么。 这是我现在拥有的代码:

<div id="video"></div>
<script>
   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('<div id="'+id+'"> ['+id+'] ['+i+'] ');
               $("#video").append('</div>');
           });
       }); 
   }

 $(document).ready(function () {
     getYouTubeAllInfo();
 });    


</script>

函数getYouTubealInfo(){
$.getJSON('http://gdata.youtube.com/feeds/users/Revision3/uploads?alt=json-在脚本&callback=?&start index=1&max results=49'中,
功能(数据){
$。每个(data.feed.entry,函数(i,项){
变量id=项目['id']['$t'];
id=id.replace(“http://gdata.youtube.com/feeds/videos/",""); 
$(“#视频”).append('['+id+']['+i+']');
$(“#视频”)。附加(“”);
});
}); 
}
$(文档).ready(函数(){
GetYouTubealInfo();
});    

假设您可以获得视频数量,您可以执行以下操作:

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('<div id="'+id+'"> ['+id+'] ['+i+'] ');
                   $("#video").append('</div>');
       });
   }); 
    currentIndex += 50;
}
int currentIndex=1
int count=GET\u count\u;
while(当前索引<计数){
$.getJSON('http://gdata.youtube.com/feeds/users/Revision3/uploads?alt=json-在脚本&回调=?&开始索引='+currentIndex+'&最大结果=49'中,
功能(数据){
$。每个(data.feed.entry,函数(i,项){
变量id=项目['id']['$t'];
id=id.replace(“http://gdata.youtube.com/feeds/videos/",""); 
$(“#视频”).append('['+id+']['+i+']');
$(“#视频”)。附加(“”);
});
}); 
电流指数+=50;
}

假设您可以获得视频数量,您可以执行以下操作:

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('<div id="'+id+'"> ['+id+'] ['+i+'] ');
                   $("#video").append('</div>');
       });
   }); 
    currentIndex += 50;
}
int currentIndex=1
int count=GET\u count\u;
while(当前索引<计数){
$.getJSON('http://gdata.youtube.com/feeds/users/Revision3/uploads?alt=json-在脚本&回调=?&开始索引='+currentIndex+'&最大结果=49'中,
功能(数据){
$。每个(data.feed.entry,函数(i,项){
变量id=项目['id']['$t'];
id=id.replace(“http://gdata.youtube.com/feeds/videos/",""); 
$(“#视频”).append('['+id+']['+i+']');
$(“#视频”)。附加(“”);
});
}); 
电流指数+=50;
}

您可以递归调用它,直到不再有结果:

function getYouTubeAllInfo(perpage, page, recursive) {
   // $("#video").append('<div> http://gdata.youtube.com/feeds/users/Revision3/uploads?alt=json-in-script&callback=?&start-index='+(((page-1)*perpage)+1)+'&max-results='+perpage+' </div>');  //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('<div id="'+id+'"> ['+id+'] ['+i+'] </div>');
           //alert(page+perpage-2);
           if(i == perpage-2 && recursive){
               getYouTubeAllInfo(perpage, page+1, recursive);
           }
       });
   }); 
}

       getYouTubeAllInfo(49, 1, true);
函数GetYouTubealInfo(每页,每页,递归){ //$(“#视频”).append('http://gdata.youtube.com/feeds/users/Revision3/uploads?alt=json-在script&callback=?&start index='+((第1页)*每页)+1)+'&max results='+每页+'');//调试 $.getJSON('http://gdata.youtube.com/feeds/users/Revision3/uploads?alt=json-在脚本&回调=?&开始索引='+((第1页)*每页)+1)+'&最大结果='+每页中, 功能(数据){ $。每个(data.feed.entry,函数(i,项){ 变量id=项目['id']['$t']; id=id.replace(“http://gdata.youtube.com/feeds/videos/",""); $(“#视频”).append('['+id+']['+i+']'); //警报(第页+第2页); if(i==第2页和递归){ GetYouTubealInfo(每页,第+1页,递归); } }); }); } GetYouTubealInfo(49,1,真); 您可以在此处进行测试:


这是一个缓慢的过程,该用户有许多视频。

您可以递归调用它,直到没有更多结果:

function getYouTubeAllInfo(perpage, page, recursive) {
   // $("#video").append('<div> http://gdata.youtube.com/feeds/users/Revision3/uploads?alt=json-in-script&callback=?&start-index='+(((page-1)*perpage)+1)+'&max-results='+perpage+' </div>');  //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('<div id="'+id+'"> ['+id+'] ['+i+'] </div>');
           //alert(page+perpage-2);
           if(i == perpage-2 && recursive){
               getYouTubeAllInfo(perpage, page+1, recursive);
           }
       });
   }); 
}

       getYouTubeAllInfo(49, 1, true);
函数GetYouTubealInfo(每页,每页,递归){ //$(“#视频”).append('http://gdata.youtube.com/feeds/users/Revision3/uploads?alt=json-在script&callback=?&start index='+((第1页)*每页)+1)+'&max results='+每页+'');//调试 $.getJSON('http://gdata.youtube.com/feeds/users/Revision3/uploads?alt=json-在脚本&回调=?&开始索引='+((第1页)*每页)+1)+'&最大结果='+每页中, 功能(数据){ $。每个(data.feed.entry,函数(i,项){ 变量id=项目['id']['$t']; id=id.replace(“http://gdata.youtube.com/feeds/videos/",""); $(“#视频”).append('['+id+']['+i+']'); //警报(第页+第2页); if(i==第2页和递归){ GetYouTubealInfo(每页,第+1页,递归); } }); }); } GetYouTubealInfo(49,1,真); 您可以在此处进行测试:


这是一个缓慢的过程,该用户有许多视频。

请小心。这种方法可以同时触发数百个ajax请求;关于ajax请求,您是正确的。有没有一种很好的方法可以通过排队请求来获取这些信息?Sebastián,我运行了代码,但它进入了一个无限循环,再次并再次显示相同的50个结果。任何建议。谢谢,小心点。这种方法可以同时触发数百个ajax请求;关于ajax请求,您是正确的。有没有一种很好的方法可以通过排队请求来获取这些信息?Sebastián,我运行了代码,但它进入了一个无限循环,再次并再次显示相同的50个结果。任何建议。感谢塞巴斯蒂安;关于ajax请求,您是正确的。有没有一种很好的方法来消除请求?Sebastián,我运行了代码,但它进入了一个无限循环,再次和再次显示相同的50个结果。任何建议。感谢塞巴斯蒂安;我运行了代码!谢谢,太完美了。我怎么能:1。获取所有id并将其传递给php变量?2.如何从youtube和Update mysql表中获取id和其他信息?谢谢如果你需要你的PHP服务器知道see ID,我想你应该直接从PHP而不是使用Javascript获取它们;php的问题是,当我使用$json=json\u解码(file\u get\u contents(“),true)时,它跳过了一些条目;或者当我使用Curl时。嗨,Sebastián;你对ajax请求的看法是正确的。有没有一种好的方法来消除这些请求?Sebastián,我运行了代码,但它进入了一个无限循环,再次并再次显示相同的50个结果。有什么建议吗?谢谢Sebastián;我运行了t