Exemplo n.º 1
0
        private async Task <PlaylistOutput> GetPlayListItemsAsync(PlaylistInput input, CancellationToken token, bool paging)
        {
            JObject        listJson;
            PlaylistOutput output = new PlaylistOutput();

            do
            {
                listJson            = JObject.Parse(await GetJsonAsync(input, InputType.Playlist, token));
                output.TotalResults = int.Parse(listJson["pageInfo"]["totalResults"].ToString());
                input.PageToken     = listJson["nextPageToken"].IsNullOrEmpty() != true ? listJson["nextPageToken"].ToString() : null;
                output.NextPage     = input.PageToken;
                foreach (var item in listJson["items"])
                {
                    output.VideosData.Add(SetPlaylistContainsDetails(item["snippet"]));
                    output.TotalResults++;
                }
            } while (!listJson["nextPageToken"].IsNullOrEmpty() && !paging);
            return(output);
        }
Exemplo n.º 2
0
        private Uri CreatePlaylistClientUri(PlaylistInput input)
        {
            if (string.IsNullOrEmpty(_apiKey))
            {
                throw new ArgumentNullException(nameof(_apiKey), "Api Key can not be null");
            }
            if (string.IsNullOrEmpty(input.PlaylistId))
            {
                throw new ArgumentNullException(nameof(input.PlaylistId), "PlaylistId can not be null");
            }
            UriBuilder builder = new UriBuilder(_baseUri + EntryPoints.Playlist)
            {
                Query = $"key={_apiKey}&" +
                        "part=snippet&" +
                        $"playlistId={input.PlaylistId}&" +
                        $"maxResults={input.MaxResultPerPage.SetMaxResultPerPage()}&" +
                        $"pageToken={input.PageToken}"
            };

            return(builder.Uri);
        }
Exemplo n.º 3
0
 public async Task <PlaylistOutput> GetPlaylistWithPagingAsync(PlaylistInput input, CancellationToken token = new CancellationToken())
 {
     return(await this.GetPlayListItemsAsync(input, token, true));
 }
Exemplo n.º 4
0
        private static string _playlistId = "PLzByySESNL7GKiOXOs7ew5vEFBxuJvf0D"; // https://www.youtube.com/playlist?list=PLzByySESNL7GKiOXOs7ew5vEFBxuJvf0D
        static void Main(string[] args)
        {
            Console.WriteLine($"--- --- --- --- Playlist --- --- --- ---");
            var youtubeClient = new YoutubeClient(_apiKey);
            var response      = Task.Run(() => youtubeClient.GetPlayListAsync(new PlaylistInput
            {
                PlaylistId = _playlistId
            }, CancellationToken.None)).Result;

            foreach (var item in response.VideosData)
            {
                Console.WriteLine($"{item.Position} - {item.Title} (Video Id : {item.VideoId})");
            }



            Console.WriteLine($"--- --- --- --- Trendings --- --- --- ---");
            var responseTrending = Task.Run(() => youtubeClient.GetTrendingAsync(new TrendingInput
            {
                Category = TrendingEnums.Categories.Music,
                Region   = TrendingEnums.Regions.Turkey
            }), CancellationToken.None).Result;

            int i = 0;

            foreach (var item in responseTrending.VideosData)
            {
                Console.WriteLine($"{++i} - {item.Title} (Video Id : {item.VideoId}) ({item.CategoryId} | {item.Category.ToString()})");
            }



            Console.WriteLine($"--- --- --- --- Playlist With Pagging --- --- --- ---");
            var playlistInput = new PlaylistInput
            {
                MaxResultPerPage = 20,
                PlaylistId       = _playlistId
            };

            for (int j = 0; j < 3; j++)
            {
                Console.WriteLine("Page : " + j);
                var responsePlaylistPaging = Task.Run(() => youtubeClient.GetPlaylistWithPagingAsync(playlistInput, token: new CancellationToken())).Result;
                playlistInput.PageToken = responsePlaylistPaging.NextPage;
                foreach (var item in responsePlaylistPaging.VideosData)
                {
                    Console.WriteLine($"{item.Position} - {item.Title} (Video Id : {item.VideoId})");
                }
            }



            Console.WriteLine($"--- --- --- --- Trending With Pagging --- --- --- ---");
            var trendingInput = new TrendingInput()
            {
                MaxResultPerPage = 20,
                Category         = TrendingEnums.Categories.Music,
                Region           = TrendingEnums.Regions.Turkey
            };
            int k = 0;

            for (int j = 0; j < 3; j++)
            {
                Console.WriteLine("Page : " + j);
                var responsePlaylistPaging = Task.Run(() => youtubeClient.GetTrendingWithPagingAsync(trendingInput, token: new CancellationToken())).Result;
                playlistInput.PageToken = responsePlaylistPaging.NextPage;
                foreach (var item in responsePlaylistPaging.VideosData)
                {
                    Console.WriteLine($"{++k} - {item.Title} (Video Id : {item.VideoId})");
                }
            }
            Console.ReadKey();
        }