ZeroOne
ZeroOne

Reputation: 3171

YouTube API 'orderby=duration' not handling the entire playlist, just the newest videos

I'm trying to build a little JavaScript program to query the YouTube API for a given playlist, ordered by duration. Everything works otherwise perfectly, but the ordering does not account for the whole playlist, just the 25 newest videos on it! Here's a minimum complete working example as a JSFiddle and here's the JavaScript part of it:

var playlistId = "UUAuUUnT6oDeKwE6v1NGQxug";    
jQuery.getJSON(
    "https://gdata.youtube.com/feeds/api/playlists/"+playlistId+"?v=2&orderby=duration&alt=json", 
    function(data) {
        $.each(data.feed.entry, function(key, val) {
            var title = val.title.$t;
            var url = val.content.src;
            var duration = val.media$group.yt$duration.seconds;
            var minutes = Math.floor(duration / 60);
            var seconds = (duration % 60);

            if( seconds < 10 ) seconds = "0"+seconds;

            var newRow = $("<tr></tr>");
            newRow.append("<td><a href='"+url+"'>"+title+"</a></td>");
            newRow.append("<td class='dur'>"+minutes+":"+seconds+"</td>");
            $("#videos").append(newRow);
        });
    }
);

I have tried this in both XML and JSON and I have also tried other kinds of searches besides the playlist search. Having the API sort just the newest videos of the result seems quite pointless. How exactly do I retrieve the longest or shortest videos of a playlist, or those uploaded by a given user for that matter?

Upvotes: 10

Views: 2789

Answers (1)

JSuar
JSuar

Reputation: 21091

EDIT

I don't think the functionality you want is available. Ordering by duration is not even available on YouTube itself. I think you will need to use max-results and start-index to fetch blocks of videos. You can then sort the compiled list by duration.

Here's a working example of one possible solution: http://jsfiddle.net/PKcnb/8


I found where Google says this explicity not possible.

YouTube API v2.0 – Video Feed Types: Videos uploaded by a specific user

This section explains how to retrieve a feed containing all of the videos uploaded by a specific user. You should not include search parameters in requests to retrieve an uploaded videos feed. The parameters will generate a feed from YouTube's search index, effectively restricting the result set to indexed, public videos, rather than returning a complete list of the user's uploaded videos.

It goes on to say:

To request a feed of all videos uploaded by the currently logged-in user, send a GET request to the following URL. This request requires an authentication token, which enables YouTube to identify the user.

So, it may be possible but you will have to test it using an authentication token.


Original Post

  1. The API docs for orderby make it sound like only the response is sorted.

    The orderby parameter, which is supported for video feeds and playlist feeds, specifies the method that will be used to order entries in the API response.

  2. Also, the max-results tag will let you specify how many items to return.

  3. Here's an example of how you could pull more (50 in this example) results already sorted. Looks like you might have to request as many as you can and then sort. Definitely not ideal.

  4. Retrieving a user's playlists

Upvotes: 6

Related Questions