order - viewCount is showing wrong results in "YouTube API v3" - youtube

I am trying to get videos of a particular channel (like :- Luis Fonsi) in highest views order. And I am passing order parameter to sort the response by viewCount
like :-
order=viewCount
URL :-
https://www.googleapis.com/youtube/v3/search?key=[MY-API-KEY]&channelId=UCxoq-PAQeAdk_zyg8YS0JqA&part=snippet,id&order=viewCount&maxResults=20000
When I try to run it then It is not sorting as I expected. I was expecting first song would be Despacito But It got another one which definitely don't have the highest views.
As mentioned in Youtube-API Documentation in search section.
viewCount – Resources are sorted from highest to lowest number of views.
It is not working like this.
I have tried many times but it is still not working.
Any help would be much Appreciated.

YouTube Data API v3 is broken on some aspects, if I were you, I would just retrieve all videos from the given YouTube channel and then sort them.
To do so:
Get the channel's uploads playlist id by using Channels: list with part=contentDetails.
Get the channel's videos by using PlaylistItems:
list
with playlistId=UPLOADS_PLAYLIST_ID (replacing
UPLOADS_PLAYLIST_ID with the one you just obtained).
Get each
video's view count by using part=statistics with Videos:
list.

Related

Fetching videos list from a Youtube playlist through RSS doesn't return more than 15 results

I'm using the following API to fetch a list of the videos in a playlist:
https://www.youtube.com/feeds/videos.xml?playlist_id=
The problem is if the playlist has more than 15 videos, the response is only the first 15. I thought there is a paging algorithm, but there is no link rel="next" in the response and appending ?page to the end of the link returns 404.
Thanks
First, the YouTube Data API v2 call is already deprecated and encourage you to use the YouTube Data API (v3).
I found in this thread that https://www.youtube.com/feeds/videos.xml?playlist_id=xxxxx is only limited to 15 videos or results.
The old API use max-results and start-index to get more results but it is not working anymore when I try.
If you want more results than 15, I suggest you to use the PlaylistItems: list of the YouTube Data API (v3). It has a parameter maxResults that you can set to maximum of 50. To get the another or next 50 results, you can use the parameter pageToken for it.
Hope this information helps you.

How to get videos for a playlist in sorted by publish date using youtube api

I need to get daily uploaded videos in a playlist so thought to get videos for a playlist sorted by published date.
So is there any option to pass published date while fetching data for playlist?
I tried 2 options
option 1 )
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=15&playlistId=PLP-nGFpz3fa_boeLhG4m0Ie_8vmCvp5oH&key={YOUTUBE_API_KEY}
does result return from this URL is in sorted order? I found its not in sorted order.
Can I pass some date parameters related to date in this or some sort related parameters?
option2 )
I tried using "Youtube Search API"
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list
here we have date option but only can pass channelId not playlistId.
So can we pass playlistId in search API to get all videos for playlist?
Is there any other way which I can use?
Thanks
The PlaylistItems:list does not have any way to order results - the playlist order is returned in the order specified by the owner. Nor does Search:list allow you to specify a playlist.
Does the playlist owner add new videos to the beginning or end of the playlist? If the beginning, you could try fetching the first X videos and comparing the publish date to the current date.
Looking at the guide using Videos.list, nowhere does it mention that this feature is available, where the list is readily sorted upon return of the response body. Try putting the results in a container first where you will implement your own comparison of dates to sort the items. This SO thread seems to confirm this.

YouTube API V3 - How to get ALL game live streams

I'm trying to get "ALL" game live streams from youtube using API.
The way I first used is through "Search.list" with parameters: https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.search.list?part=snippet&eventType=live&maxResults=50&type=video&videoCategoryId=20. But in this way when I try to iterate results using PageToken, I can only get around 100 results while in the API response I can see "totalResults" is around 2000. Then I came into this topic and realized that "Search.list" is actually kind of "ranking" algorithm instead of "fetch all data from DB", which means "totalResults" is just an estimated number of results.
Then I came to use "liveBroadcasts.list" but it returns no results: https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.liveBroadcasts.list?part=id&broadcastStatus=active&maxResults=50&_h=8&
And for other APIs, they either need channelId or other ids.
Is there any way to get all game live streams regardless of any ids?
According to Youtube Category ID list, Gaming resides on number 20 category. In case you want to try a different category in the future, instead of videoCategoryId=20, you can try videoCategoryId=31 - that's for Anime/Animation.
Try to run this on your browser to get all live streams:
https://www.googleapis.com/youtube/v3/search?part=snippet&eventType=live&type=video&videoCategoryId=20&regionCode=US&maxResults=50&key=SERVER_API_KEY_HERE

YouTube Data Api v3 consistently returns invalid statistics

I'm using YouTube Data v3 in order to get statistics of a certain channel. To be precise I'm following this documentation.
I can get all data properly except for commentCount property. For example GET request:
https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername=Kurzgesagt&key={YOUR_API_KEY}
Which results in commentCount = 23
This is obviously not correct as this channel has much more than that.
Same thing happens to almost all other channels (not all of them). Is there any particular reason for this behaviour? Why does it return this number?
I've been monitoring this value for more than a week and it hasn't changed once in that time period.
Edit: I know that commentCount property should return number of comments on that channel (not sum of all videos), but for some reason this doesn't really match. You can compare the commentCount I mentioned with real comments for Kurzgesagt channel on YT directly.
CommentCount returns the number of comments in the channel, not the sum of all comments in the channel's videos, this has already been answered here Youtube Data Api has commentCount field and is this the number of comments on channel?
There seems to be more than one channels for that user, so is the link that you provided actually supposed to bring back the expected feedback ? There is no specified channel id on your URL.
Try the following:
GET https://www.googleapis.com/youtube/v3/commentThreads?part=snippet%2Creplies&allThreadsRelatedToChannelId=ChannelId&key={YOUR_API_KEY}

youtube api v3 - get videos from a playlist with view count data ordered by number of views

I want to get all the videos from a playlist with statistics data like viewCount and the videos should be ordered by number of video views. So far I have managed the following:
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=UUbW18JZRgko_mOGm5er8Yzg&key={YOUR_KEY_HERE}&maxResults=50
In the youtube api v2 we can pass part=statistics to get the view count but this is not working with v3 playlists. How can I get the above the video data in the above mentioned format.
From the results of playlistItems.list call, collect the video ids, make a comma-separated list and set it as id parameter of videos.list call. That call will give you the data you need.

Resources