asok Buzz
asok Buzz

Reputation: 1894

Getting the sorted playlistItems from google youtube api v3

I am using google youtube data api v3 to get videos in a playlist. Everything is fine but I want the final result to be sorted by date. How can I accomplish this?

Following is my query

https://www.googleapis.com/youtube/v3/playlistItems?key={my_api_key}&playlistId=PLxtF39cCOsxehnvwzlYT0R5h8dh4y3Lp2&part=snippet,contentDetails&maxResults=50&fields=items(snippet/title,snippet/publishedAt,snippet/resourceId/videoId)

Upvotes: 4

Views: 1845

Answers (3)

Digitech
Digitech

Reputation: 292

You can set sort order in the playlist settings API will return results in that order.

Upvotes: 0

WJLong
WJLong

Reputation: 21

Since this option to "pre-sort" the results from API is not yet available, you would have to manually do it.

  • Use https://www.googleapis.com/youtube/v3/playlistItems to grab all the videoID's of the current playlist.
  • If the playlist is longer than 50 videos, then create a loop that first stores the totalResults into a variable.
  • Loop through each set of 50, until all videoID's have been fetched using nextPageToken.
  • Now you should have an array with all videoID's stored inside it.
  • Sort it how you want (newest first would be a simple reverse),
  • Use https://www.googleapis.com/youtube/v3/videos to fetch all the info from each video, in sets of 50. Do same loop as above.

In theory this should work. I will be testing this out later on.

Upvotes: 2

Ibrahim Ulukaya
Ibrahim Ulukaya

Reputation: 12877

This feature request is reported internally. You can create a ticket to track.

Upvotes: 0

Related Questions