Question

Is there any way to pull only the latest video from a Youtube channel using Javascript?

I've got an idea of using Youtube feed but I don't really know how to implement it using javascript.

Any practical example would be very much appreciated.

Était-ce utile?

La solution

After a hard searching this morning, I now get it work perfectly:

$(function() {  
   var API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
   var PLAYLIST_ID = 'PLDvUaV28ulciewonYGknOdIw8lAJfXIzn';

   var GOOGLE_API_URL = 'https://www.googleapis.com/youtube/v3/playlistItems?part=id%2C+snippet%2C+contentDetails&playlistId='+PLAYLIST_ID+ '&key=' + API_KEY + '&callback=showVideos';

   $.ajax({
    url: GOOGLE_API_URL,
    dataType: 'jsonp',
    crossDomain: true
   });

   window.showVideos = function(data) {
    if (data.items && data.items.length > 0) {
       $("#video").html('<iframe width="504" height="283" src="http://www.youtube.com/embed/'+data.items[0].contentDetails.videoId+'" frameborder="0" allowfullscreen></iframe>');
    }
   }
});

Autres conseils

Using jQuery:

$.getJSON('https://gdata.youtube.com/feeds/api/videos?author=yourchannel&orderby=published', function(data) {

   //data represents JSON Object of videos

}

If I set the parameters right, data should represents all videos sort by published time, so the last one is the latest one

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top