Home > Blockchain >  How to get a list of every video from a channel using the YouTube API?
How to get a list of every video from a channel using the YouTube API?

Time:02-12

I have created a program where the user enters a YouTube channel name and it does a search and returns and displays YouTube channels with similar names based on their relevancy. Then the user picks what channel that they want and the program saves the ChannelID. Then I want to get a list of all of the channels videos so i can display a random video to the user from that channel. The issue is that the old method of using a channels "Uploads" playlist no longer works as I believe that channels don't have this playlist anymore by default. The code I am currently using to get the channels videos has its own issues.

   SearchListResponse response;
                var search = service.Search.List(repeatable);
                search.Type = "video";
                search.ChannelId = Session["ChannelID"].ToString();
                search.MaxResults = 500;

                do
                {
                    response = search.ExecuteAsync().Result;

                    foreach (SearchResult sr in response.Items)
                    {
                        videoIds.Add(sr.Id.VideoId);
                    }

                    search.PageToken = response.NextPageToken;

                } while (response.NextPageToken != "");

The issue with this method is that it uses 100 quota per search but only returns 50 videos at a time. And if a user has 1000 videos it would take 20 searches aka 2000 quota (20% of daily quota) just to get the channels VideoID's.

I believe there are two ways to fix this:

From a single request, get a random video from the channel

OR

Get all VideoIDs from the channel and then pick a random one.

CodePudding user response:

The issue is that the old method of using a channels "Uploads" playlist no longer works as I believe that channels don't have this playlist anymore by default.

Not sure where you get that info, but, you can retrieve the videos from the "Uploads" playlist - see this answer for more info.

Basically, its changing one letter from the channelId and then you save another request for bring the "Uploads" playlist.

See this working sample I made for an guidance to get the uploaded videos from a channel - check that this channel UCyztLS0dr85vRqpKZHFfbmQ has 91 uploaded videos up to this date (11/02/2022), then, the nextPageToken is needed for get the next results.

The maximum value for maxresults is 50 - this applies for all requests as far as I read in the documentation.

Since you mention the random video picker, you could use the results from the playlistItems endpoint and pick one random video result (after making one single request) and - since it's random, you can't control which video is picked randomly.

If you follow this line of thinking and want to save more quota, then use this endpoint for get the latest uploaded videos from a given channel:

https://www.youtube.com/feeds/videos.xml?channel_id=<CHANNEL_ID>

Example:

https://www.youtube.com/feeds/videos.xml?channel_id=UCypzG0nv9B65DwOLJ578rZQ

N.B: if you want to get more videos, you have to make more requests - as far as I know, there is no request that brings you all info from a given channel - that's why you use the NextPageToken for results pagination.

CodePudding user response:

Channel has a property ChannelContentDetails which you can access RelatedPlaylists from. Inside related playlists, you can pull the Playlist "ID" for the Channels Uploads. then you can grab the PlaylistItems thus videoID's and you could randomly go through that with a much lower quota cost. looking through the documentation it doesn't appear to be depreciated so this could be a workaround.

I apologize for not being able to format a script for you, but I have never used Youtube's API and it probably wouldn't compile anyway.

  • Related