using Google.Apis.Services; using Google.Apis.YouTube.v3; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; using TomatenMusic.Music.Entitites; using System.Linq; using Google.Apis.YouTube.v3.Data; using Microsoft.Extensions.Logging; using static TomatenMusic.TomatenMusicBot; using Lavalink4NET.Player; using Microsoft.Extensions.DependencyInjection; using Lavalink4NET; namespace TomatenMusic.Services { public class YoutubeService { public YouTubeService Service { get; } public ILogger _logger { get; set; } public YoutubeService(ILogger logger, ConfigJson config) { Service = new YouTubeService(new BaseClientService.Initializer() { ApiKey = config.YoutubeAPIKey, ApplicationName = "TomatenMusic" }); _logger = logger; } public async Task PopulateTrackInfoAsync(LavalinkTrack track) { var video = await GetVideoAsync(track.TrackIdentifier); var channel = await GetChannelAsync(video.Snippet.ChannelId); FullTrackContext context = track.Context == null ? new FullTrackContext() : (FullTrackContext)track.Context; if (channel.Statistics.SubscriberCount != null) context.YoutubeAuthorSubs = (ulong) channel.Statistics.SubscriberCount; context.YoutubeAuthorThumbnail = new Uri(channel.Snippet.Thumbnails.High.Url); context.YoutubeAuthorUri = new Uri($"https://www.youtube.com/channel/{channel.Id}"); context.YoutubeDescription = video.Snippet.Description; if (video.Statistics.LikeCount != null) context.YoutubeLikes = (ulong) video.Statistics.LikeCount; context.YoutubeTags = video.Snippet.Tags; context.YoutubeThumbnail = new Uri(video.Snippet.Thumbnails.High.Url); context.YoutubeUploadDate = (DateTime)video.Snippet.PublishedAt; context.YoutubeViews = (ulong)video.Statistics.ViewCount; context.YoutubeCommentCount = video.Statistics.CommentCount; track.Context = context; return track; } public async Task> PopulateMultiTrackListAsync(IEnumerable tracks) { List newTracks = new List(); foreach (var track in tracks) newTracks.Add(await PopulateTrackInfoAsync(track)); return newTracks; } public async Task PopulatePlaylistAsync(YoutubePlaylist playlist) { var list = await GetPlaylistAsync(playlist.Identifier); var channel = await GetChannelAsync(list.Snippet.ChannelId); string desc = list.Snippet.Description; playlist.Description = desc.Substring(0, Math.Min(desc.Length, 4092)) + (desc.Length > 4092 ? "..." : " "); playlist.Thumbnail = new Uri(list.Snippet.Thumbnails.High.Url); playlist.CreationTime = (DateTime)list.Snippet.PublishedAt; playlist.YoutubeItem = list; playlist.AuthorThumbnail = new Uri(channel.Snippet.Thumbnails.High.Url); playlist.AuthorUri = new Uri($"https://www.youtube.com/playlist?list={playlist.Identifier}"); return playlist; } public async Task