omansak / libvideo

A lightweight .NET library to download YouTube videos.
BSD 2-Clause "Simplified" License
553 stars 163 forks source link

YouTube Playlists #156

Closed TieHaxJan closed 4 years ago

TieHaxJan commented 4 years ago

I wondered if you could add support for YouTube Playlists?

JOBBIN9422 commented 4 years ago

I added the ability to download playlists by using the YouTube Data API to extract a list of URLs when given a playlist URL - here is a code sample.

            List<string> videoUrls = new List<string>();
            string nextPageToken = "";

            //iterate over paginated playlist results from youtube and extract video URLs
            while (nextPageToken != null)
            {
                //prepare a paged playlist request for the given playlist URL
                var playlistRequest = _ytService.PlaylistItems.List("snippet,contentDetails");
                playlistRequest.PlaylistId = GetPlaylistIdFromUrl(playlistURL);
                playlistRequest.MaxResults = 50;
                playlistRequest.PageToken = nextPageToken;

                var searchListResponse = await playlistRequest.ExecuteAsync();

                //iterate over the results and build each video URL
                foreach (var item in searchListResponse.Items)
                {
                    string videoUrl = $"http://www.youtube.com/watch?v={item.Snippet.ResourceId.VideoId}";
                    videoUrls.Add(videoUrl);
                }

                //index to the next page of results
                nextPageToken = searchListResponse.NextPageToken;
            }
TieHaxJan commented 4 years ago

Thanks a lot :)

omansak commented 4 years ago

You can use or take a reference https://github.com/omansak/YoutubeSharpApi