private async Task <TrendingOutput> GetTrendingItemsAsync(TrendingInput input, CancellationToken token, bool paging) { JObject listJson; TrendingOutput output = new TrendingOutput(); do { listJson = JObject.Parse(await GetJsonAsync(input, InputType.Trending, 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(SetTrendingContainsDetails(item)); output.TotalResults++; } } while (!listJson["nextPageToken"].IsNullOrEmpty() && !paging); return(output); }
private Uri CreateTredingClientUri(TrendingInput input) { if (string.IsNullOrEmpty(_apiKey)) { throw new ArgumentNullException(nameof(_apiKey), "Api Key can not be null"); } if (input.Category == default(int)) { throw new ArgumentNullException(nameof(input.Category), "Category can not be null"); } UriBuilder builder = new UriBuilder(_baseUri + EntryPoints.Videos) { Query = $"key={_apiKey}&" + "part=snippet&" + "chart=mostPopular&" + $"maxResults={input.MaxResultPerPage.SetMaxResultPerPage()}&" + $"pageToken={input.PageToken}&" + $"regionCode={new TrendingEnums().GetCodeOfRegion(input.Region)}&" + $"videoCategoryId={(int)input.Category}" }; return(builder.Uri); }
public async Task <TrendingOutput> GetTrendingWithPagingAsync(TrendingInput input, CancellationToken token = new CancellationToken()) { return(await this.GetTrendingItemsAsync(input, token, true)); }
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(); }