Jquery and Youtube API 2.0 - Getting Video Links from Playlists -


i prefer using program smplayer watch youtube videos. in order use smplayer playlist, however, i'd need right-click , copy each , every individual link , paste each 1 smplayer...

i wanted simple application took youtube playlist link , obtained list of urls respective videos within playlist. copy-paste list of urls program , done.

this apparently extremely difficult pull off...

i came close finding solution existed here. playlists big, seems fail.

so, after searching around on google, came across this post. it, able create solution... these requests seem finish whenever want, , enter output in whatever order choose.

while(keepgoing&&index<absolutemax){         $.getjson(playlisturl, {}).done(function(data) {             var list_data="";             if (data.feed.entry.length<resultsperpage){                 keepgoing=false;             }             $.each(data.feed.entry, function(i, item) {                 var feedurl = item.link[1].href;                 var fragments = feedurl.split("/");                 var videoid = fragments[fragments.length - 2];                 var url = videourl + videoid;                 list_data += url + '\n';             });             var results = $('#results').val();             $('#results').val(results+=list_data);         });         index+=resultsperpage;         playlisturl = 'http://gdata.youtube.com/feeds/api/playlists/plpclvninngnu9a9qqphjpsk95kkzsns_z?alt=json&v=2&max-results='+resultsperpage+'&start-index='+index+'&callback=?';     } 

link jsfiddle

you'll note had loop in order pull in video links, because api not allow pull in more 50 videos @ time. problem here each set of 50 comes in different order when called. click start button in fiddle, , you'll notice urls come in different orders each time...

what i'd know why these results coming in random orders? have missed? i've tried adding wait timer, doesn't work. i've tried appending each result array , displaying array @ end, doesn't work either. , reason in firebug debug, cannot seem step inside "getjson" function... in fact never goes inside. time function works when don't debug @ all...

i'm on cusp of getting right, , i've spent far many hours hacking away @ it... if who's more familiar has better idea, please let me know :)

the problem you're using while loop. because calls yt api asynchronous, script reaches end of while loop , triggers next iteration before results have come first call. there lots of ways avoid this; simple 1 might wrap in function instead of loop:

$('#startbutton').on("click",function(){   $('#results').val('');   var youtubeurl = $('#youtubeurl').val();   var fromlistonwards = youtubeurl.substring(youtubeurl.indexof('list=')).split('&');   var playlistid = fromlistonwards[0].substring(5);   var resultsperpage=50;   var index=1;   var absolutemax=999;   var ytkey="{your api key}";   var videourl= 'http://www.youtube.com/watch?v=';   function getytpage(pagetoken) {    playlisturl= 'https://www.googleapis.com/youtube/v3/playlistitemspart=snippet&maxresults='+resultsperpage+'&playlistid='+playlistid+'&key='+ytkey;    if (typeof pagetoken !== 'undefined') {          playlisturl +="&pagetoken="+pagetoken;    }    $.get(playlisturl, {}).done(function(data) {       var list_data="";       $.each(data.items, function(i, item) {          var url = videourl + item.snippet.resourceid.videoid;          list_data += url + '\n';       });       var results = $('#results').val();       $('#results').val(results+=list_data);       if (data.items.length===resultsperpage&&index<absolutemax) {          getytpage(data.nextpagetoken);       }    });   }   getytpage(); }); 

(note used v3 of api working code example, makes getting videoid , pagination easier, same paradigm applied v2).

this way, call returned , processed before next function execution.

also note that, again, because it's ajax, if click start button once again while page in process of waiting results (i.e. before has completed), call won't cancelled ... might duplication of later results pages. ordering consistent. , if let 1 full cycle finish before clicking button again, results identical.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -