Sorting youtube API, newest first - youtube

How to reverse this videos list?
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=PLYQWoSxgWOXMjtLQJIikS56pu7eJusSKW&maxResults=25&pageToken=CB4QAA&key=AIzaSyDEm5wGLsWi2G3WG40re-DAJcWioQSpJ6o
I need newest first! (by publishedAt sorting newest first)

Unfortunately this is not supported for playlistItems.
You could try reading in the entire playlist and doing the sort yourself.

You need to use the order parameter in your REST API call to fetch the video is some specific order. As mentioned in your question you need to fetch the data in sorted order where the published recently should be at the top. Hence you need to set the value of order to date in your REST API call. Other values that are supported, rating, relevance (by default), title, videoCount and ViewCount.
For additional details read the official documentation and also try the web service.

https://developers.google.com/youtube/2.0/developers_guide_protocol
Add orderby=time to your URL
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=PLYQWoSxgWOXMjtLQJIikS56pu7eJusSKW&maxResults=25&pageToken=CB4QAA&key=AIzaSyDEm5wGLsWi2G3WG40re-DAJcWioQSpJ6o&orderby=time

Related

Is there a way to get a list of Youtube videos sorted by view count

I am trying to collect a large list of YouTube's most watched videos for a data science application. I tried to use the YouTube Data API with the following query:
https://www.googleapis.com/youtube/v3/search&order=viewCount&type=video&regionCode=US&key=API_KEY,
but it does not seem to give me the same video ideas as in this list: https://en.wikipedia.org/wiki/List_of_most-viewed_YouTube_videos
Could someone tell me how I should do it?
Using the Search.list API endpoint is one way to search for the most viewed videos pertaining to a given region.
There's another endpoint -- Videos.list -- that, when queried with chart set to mostPopular, gives back:
chart (string)
The chart parameter identifies the chart that you want to retrieve.
Acceptable values are:
mostPopular – Return the most popular videos for the specified content region and video category.
As per the specification of videoCategoryId:
By default, charts are not restricted to a particular category.
Therefore, you may safely invoke Videos.list with chart=mostPopular and regionCode=US, without passing to it any videoCategoryId.
These two are the only API means that accomplish the task described by your post.

Is the YouTube API v3 Activity.List() sorted by PublishedAt date?

I was wondering whether the Activity.list() YouTube API v3 function is sorted by publishedAt date.
That is: if I get the top element of the returned list whether I would get the last activity by the channel.
I want ultimately to get the last activity performed by the channel (the aim is to find whether a channel is active or not), without the need to load the whole collection of activities and sort it manually.
The YouTube API documentation is not explicit about it and I was wondering whether someone has experience with this.
Thanks a million!
Activity.list() returns in what ever order google sees fit I don't think there is going to be any way to guarantee that it will remain that way either as its not documented.
There is also no way to sort the list before it is returned. Your best bet is going to be to retrieve the whole list and sort it locally.

YouTube API "mostPopular" requests doesn't seem to give updated results

It seems that the YouTube API doesn't give updated results for mostPopular videos in my country since few days.
Example:
This request (https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails&chart=mostpopular&regionCode=FR) doesn't give me the same videos results than the ones displayed directly on YouTube for the French most popular channel (https://www.youtube.com/channel/UCmzy72gDEpfXoFV9Xdtd0DQ). It seems that the results of this request is not updated since the 1th of february. Results was real time updated before this.
Does someone know if something is wrong with my API request, or if there are some issues with the YouTube API at this moment?
There is nothing wrong with your request. This is a known issue with the YouTube API reported here for Saudi Arabia (but also applicable to multiple regions), and another related issue here with regard to content from France.
Your best bet would be to follow up with the YouTube team on one of those defects, or potentially (and dangerously) scrape the YouTube site for the correct results.
Problem seems to be solved since 13th of february (maybe someone from Google have seen my post..)
YouTube Channels and chart=mostPopular parameter data are separate data entities, aka you will get different results. They may be related but there is no guarantee you will get the same data. To get the data that you want you may need to query for channel itself and its videos.
I got this information from the thread #Jal linked, there was an update by matthewc...#google.com a few days ago:
The most popular channel for Saudia
Arabia and
the mostPopular chart parameter in the video.list
call
are separate and distinct entities. If you'd like to get the content
of the most popular channel for Saudia
Arabia
please use the Data API video.list call to list the videos with the
channel ID (in this case "UCWY-_j1MCth6yf24m58Bh_Q") by setting the
items/snippet/channelId parameter.
My current concern right now is that there is supposedly a way to get video information from the videos.list endpoint using a channelId, which does not seem the case in the API Explorer. I will update my answer once I figure out what this person meant exactly.

YouTube API v3 Order Parameter Possible Bug

Looking at the YouTube API v3 Search documentation, it states that the order parameter is used to control ordering of API response resource results. However, when attempting to order by date, I'm not receiving the results in reverse chronological order as specified. In fact, they are not in any order whatsoever.
API Request with order date using API Explorer UI:
Is there some other way I should be requesting for results to be ordered by the time they were published on YouTube? Is there a bug in the handling of the order API parameter?
There's no bug; the 'order' parameter will sort results based on when the resources were created, This value may be slightly different than when a video is published, as a resource is created when an upload begins but a video is published when the upload finishes and is processed.
In this case, it looks like the 10 or so most recent videos were all uploaded at the exact same time (YouTube.com has a batch uploader), so you're getting the order they were created in, even though the published dates reflect minor differences (maybe a couple took a bit longer to upload or something). IF you scan the full list of results, though, you'll see they play out just fine in terms of their reverse chronological order. And generally you probably won't have so many videos that were batch uploaded like this.

YouTube Data API V3: Fetch multiple videoCategoryId videos

I am using YouTube Data API Version 3.0 in one of my projects to fetch my channel video details from YouTube. I don't want the user to login to his/her Google account that's why I am directly using Search.list method instead of going through the OAuth 2.0 way.
Usually I fetch data using following URL.
https://www.googleapis.com/youtube/v3/search?key={API_KEY}&maxResults=5&part=snippet&type=video&channelId={CHANNEL_ID}
Now, I want to fetch data of 5 different categories at a time. What I can do is, hit the same URL 5 times with query string as
key={API_KEY}&maxResults=1&part=snippet&type=video&channelId={CHANNEL_ID}&videoCategoryId={CATEGORY_ID}
or,
Is there a way like
key={API_KEY}&maxResults=5&part=snippet&type=video&channelId={CHANNEL_ID}&videoCategoryId={CATEGORY_ID_1, CATEGORY_ID_2, CATEGORY_ID_3, CATEGORY_ID_4, CATEGORY_ID_5}
I want to fetch only 1 video data per category. That is why I have given 5 comma separated category IDs.
Also, Search.list method does not give videoCategoryId. To get it, I have to use
https://www.googleapis.com/youtube/v3/videos?part=snippet&id={VIDEO_ID}&key={API_KEY}
Is there any way to get videoCategoryId in Search.list method?
You need to have 5 queries for that. Even if you were able to give 5 category ids, it wouldn't understand to pick up one video data for each one.
Search doesn't return videoCategory right now, videos->list has it.
There is no problem going through OAuth2, you can just ask read-only permission, and should be fine. Users can pick their already stored accounts easily.

Resources