How do you always get the last video from a Youtube channel?

Asked

Viewed 2,499 times

6

I have a div <div class="player"></div> and inside it, I want the last video posted on a channel to always appear.

I have this structure in jQuery, but I can’t get it to pull only 1. Follow 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 to solve this problem?

1 answer

6


Because the data is ordered by decreasing date, the easiest way to get the last video without having to change the code structure is to manipulate the parameter max-results URL:
Of:
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

  • Oh man, thank you so much! It worked out here!

  • You’re welcome. Good luck with the code!

  • In case to be another channel I have to change only after users/ID_DO_CANAL ? And in mine here gave an error 410 (Gone).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.