Sorting Youtube API result from playlist
Asked Answered
L

4

17

I have been struggling with Youtube's API v3 now and I can't get it work in the way I want. My goal is to load the latest videos from a selected playlist into my website. The problem is that I only the the oldest videos in my response.

To load the playlist I request following API URI: https://www.googleapis.com/youtube/v3/playlistItems

Here is an example of a request (just click execute): http://developers.google.com/apis-explorer/#p/youtube/v3/youtube.playlistItems.list?part=id%252Csnippet%252CcontentDetails%252Cstatus&playlistId=PLOU2XLYxmsIKGo-dgliIJQNZ6L3G8UV4b&_h=1&

How can a sort the result and get the latest videos first? I can't find anything in the docs and I have been googling without any luck. There is no point with this API if you can't sort the result. The only way is to load all videos by keep loading the next page by token but that is just insane.

Any idea? Sounds like a basic problem but I can't find a solution...

Leprosarium answered 6/4, 2014 at 11:22 Comment(0)
R
15

The current API doesn't have a sorting option for listing playlist items, but if you are inserting the items programmatically, you can specify the position value when inserting to make new videos appear at the top of the list.

If it's someone else that is adding the videos, than you'll have to get all pages. Since the playlists are limited to 200 items, this requires 4 requests at most. Not ideal, but it's the only option now.

Rollway answered 6/4, 2014 at 18:24 Comment(5)
Ok, this is what I was afraid of. Quite useless API if you're asking me.Leprosarium
Just noticed that the results are ordered based on the playlist setting? (which you have to own to change of course).Hyperbola
Yes you got it, We need to change the settings in the playlist. There is an option "Ordering", in the playlist advanced settings.Deland
Playlist advanced settings are now disabledTedra
@Tedra just tried it and it's enabled. You can only change the settings of your own playlist ofcAussie
C
10

This does seem to be a limitation currently of the YouTube API and seems quite short-sighted by its creators.

This doesn't solve your exact problem, but for others having a similar problem, I found that using the 'search' query you can order to a certain extent.

This helped me order a playlist (not playlistItems) query by searching for playlists rather than requesting a list of playlists. Thus my query:

https://www.googleapis.com/youtube/v3/playlists?part=snippet&maxResults=20&channelId={channelID}&key={API_KEY}

became:

https://www.googleapis.com/youtube/v3/search?part=snippet&type=video&order=date&safeSearch=none&maxResults=20&channelId={channelID}&key={API_KEY}

You can also search for videos and query terms and several other parameters, so maybe you can get the results you want this way.

Complacence answered 21/1, 2015 at 11:10 Comment(2)
Thanks @paul-thomas-gc. I changed the type to video and that gives me the sorted resultsNordrheinwestfalen
I observed rare cases in which the search does not return all videos. My guess is that YouTube completely removes certain inappropriate videos from search even if you use safeSearch=none.Finitude
O
0

This is the only option right now available. Instead of trying to sort video list from the Http request Url, you can sort the order in the youtube account's playlist setting.

In the Playlist setting you can either manually drag and change the list or there is a option to change the order. Youtube Data API will output the data as the playlist shows the data.

Summary : change the playlist order in youtube account instead trying to filter data using http requests.

FYI: playlist settings can be only changed by the uploader's account Hope this helps.

Organdy answered 27/5, 2019 at 16:21 Comment(0)
A
0

I had the same issue .. instead of using the API to get the newest first (there doesn't appear to be a way to do this), why don't you just sort the videos inside your playlist first, then use the API which will return them in the order they appear in the playlist.

Something like this: https://community.tubepress.com/topic/5624-how-to-customize-sort-order-of-youtube-playlists/

Adallard answered 27/5, 2019 at 23:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.