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?
Standard feeds are updated periodically, with the update frequency varying from feed to feed. Many feeds are updated every 30 to 40 minutes but other feeds such as those that capture daily, weekly or monthly results – may only be updated hourly or even daily. Check the link above for more information.
It's easy and you don't need a special website. Open the playlist and watch videos. Drag the address to your favorites or bookmarks bar before closing out when you're done. When you want to go back, open the link.
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
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.
Also, the max-results
tag will let you specify how many items to return.
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.
Retrieving a user's playlists
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With