public async Task <YoutubeChannelResult> GetYouTuberByNameAsync(string name) { var searchList = _youtubeService.Search.List("snippet"); searchList.Q = name; searchList.MaxResults = 50; searchList.Type = "channel"; var result = await searchList.ExecuteAsync(); var channel = result.Items.FirstOrDefault(x => x.Id.Kind == "youtube#channel"); if (channel == null) { return(null); } var resultObject = new YoutubeChannelResult { Url = $"https://youtube.com/channel/{channel.Id.ChannelId}", Name = channel.Snippet.ChannelTitle, Description = channel.Snippet.Description, IconUrl = channel.Snippet.Thumbnails.Default__.Url }; var channelSearch = _youtubeService.Channels.List("snippet,statistics"); channelSearch.Id = channel.Id.ChannelId; var channelResult = await channelSearch.ExecuteAsync(); if (channelResult?.Items == null || !channelResult.Items.Any()) { return(null); } var firstItem = channelResult.Items.FirstOrDefault(); if (firstItem != null && firstItem.Statistics.SubscriberCount.HasValue) { resultObject.SubscriberCount = firstItem.Statistics.SubscriberCount.Value; } var videoRequest = _youtubeService.Search.List("snippet"); videoRequest.ChannelId = channel.Id.ChannelId; videoRequest.Type = "video"; videoRequest.Order = SearchResource.ListRequest.OrderEnum.ViewCount; videoRequest.MaxResults = 5; var videosResult = await videoRequest.ExecuteAsync(); resultObject.Videos = videosResult.Items .Select(x => new VideoResult($"https://youtube.com/v?id={x.Id.VideoId}", x.Snippet.Title)).ToList(); return(resultObject); }
public IYoutubeEmbedGenerator CreateChannelEmbed(YoutubeChannelResult channel) { ResetBuilder(); _builder.WithUrl(channel.Url); _builder.AddField("Channel", $"{channel.Name} with {channel.SubscriberCount} subscribers.\n{channel.Url}"); var stringBuilder = new StringBuilder(); channel.Videos.ForEach(x => stringBuilder.AppendLine($"{x.Name}: {x.Url}")); _builder.AddField("Most Popular Videos", stringBuilder.ToString()); _builder.WithThumbnailUrl(channel.IconUrl); return(this); }