How do I always get the latest video from a YouTube channel?

5

I have a% div of% and within it, I want the last video posted to a channel to always appear.

I have this structure in jQuery, however I can not get it to pull only 1. Here is the structure:

$(function() {
    // Baixar URLs do feed do canal
    $.get('http://gdata.youtube.com/feeds/api/users/Rodrigoaraujooficial/uploads?max-results=10&alt=json', function(result) {

        var entries = result.feed.entry;

        for(var i=0; i<entries.length; i++) {
            var url = entries[i].link[0].href;   

            // Transformar URL de vídeo em URL de embed
            // ANTES: http://www.youtube.com/watch?v=aDqExWjK49Y&algumacoisaaqui
            // DEPOIS: http://www.youtube.com/embed/aDqExWjK49Y
            url = url.replace('/watch?v=', '/embed/');
            url = url.substring(0, url.indexOf('&'));

            // Fazer append dos vídeos
            $('.player').append('<iframe width="480" height="270" src="' + url + '" frameborder="0" allowfullscreen></iframe>');
        }
    });
});

How can I solve this problem?

    
asked by anonymous 13.11.2014 / 14:24

1 answer

5

As the data is sorted by date, the easiest way to get the latest video without changing the code structure is to manipulate the max-results URL parameter:
From:
http://gdata.youtube.com/feeds/api/users/Rodrigoaraujooficial/uploads?max-results=10&alt=json
To:
http://gdata.youtube.com/feeds/api/users/Rodrigoaraujooficial/uploads?max-results=1&alt=json

    
13.11.2014 / 14:49