Search code examples
javascriptjqueryyoutubefeed

Pull the latest video from a Youtube Channel


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.


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>');
        }
       }
    });