jquery - Youtube API - Function to retrieve video image thumbnails fails when finding deleted video -
i'm retrieving youtube video thumbnails , such function deals comma-delimited string of video ids :
function videoids(){ var videoarray = $("#comma-delimited-ids").val().split(","); var defarray = $.map(videoarray, function (videoids) { var requesturls = '//gdata.youtube.com/feeds/api/videos/'+ videoids +'?v=2& format=5alt=json-in-script&callback=?'; return $.ajax({ type: "get", datatype: "jsonp", url: requesturls, success: function(data) {} }); }); $.when.apply(null,defarray).done(function(){ $.each(arguments,function(i,arg){ var data = arg[0]; ... the map , done methods retrieve videos first, in chronological order appear based on comma-delimited string, before displaying them on page. if method wasn't used, youtube return faster random order of video thumbnails. function above works 2 video ids or more, not 1 single video id. current prob when video doesn't exist anymore on youtube servers, comma-delimited id string containing missing id provokes failure, making function unable retrieve existing videos reflecting ids in string. work-araound appreciated.
note: prefer being able retrieve thumbnails of deleted videos (youtube's default deleted video thumbnail) including available in order keep total original video thumbnail count intact (based on id string). thx sample/suggestion.
update:
it appear cross-domain issues hamper error handling of native ajax/jsonp processes, luckily, jquery-jsonp alleviates 1kb plugin (see: https://github.com/jaubourg/jquery-jsonp ). following amended code looks this:
function videoids(){ var videoarray = $("#comma-delimited-ids").val().split(","); var defarray = $.map(videoarray, function (videoids) { var requesturls = '//gdata.youtube.com/feeds/api/videos/'+ videoids +'?v=2& format=5alt=json-in-script&callback=?'; return $.jsonp({ // replace $.ajax({ $.jsonp({ type: "get", datatype: "jsonp", url: requesturls, success: function(data) {}, error: function(d,msg) { alert("error. video id non existant."); // add function here skip missing id , proceed next id in csv string } }); }); $.when.apply(null,defarray).done(function(){ $.each(arguments,function(i,arg){ var data = arg[0]; ... // code displays youtube thumbnails etc of available ids. so alert pops when video id non existant (deleted or private), when happens function @ current state of code halts , not retrieve thumbnail image of videos existing ids found in csv string. there way iterate through ids found in csv string in following order? :
- request data of first id in csv string;
- if alert (or not found), ignore , go 2nd id in csv string, else, return data (thumbnail, etc) of first id , when completed, go 2nd id in csv string;
- repeat above procedure in chronological manner throughout csv string of ids until done.
i'm kinda new ajax-jsonp methods , jquery level still beginners. hope can suggest workable solution, thx helping out.
finally! google searching gave me kool sample of success/error handling supported jquery-jsonp plugin (see above):
success: function(json, value) { console.log(your-string-of-ids); }, error: function(xoptions, textstatus){ console.log(your-string-of-ids); } now it's question of replacing console.logs function(s) needed filter, in case, missing thumbnail images. error function handles malformed video ids , deleted/private video ids. using new regexp and/or replace methods within simple $.each loop trick, heck, replace missing thumbnail custom image supplied one-frame video you've uploaded youtube channel. hope answer others. - koolness
Comments
Post a Comment