TomatenMusic-V2/TomatenMusicCore/Services/SpotifyService.cs

189 lines
8.2 KiB
C#
Raw Normal View History

2022-03-19 22:28:54 +01:00
using SpotifyAPI.Web;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using DSharpPlus;
using System.Linq;
using Microsoft.Extensions.Logging;
using TomatenMusic.Music.Entitites;
using TomatenMusic.Services;
using TomatenMusic.Music;
using Lavalink4NET;
using Lavalink4NET.Player;
using System.Runtime.Caching;
2022-03-19 22:28:54 +01:00
namespace TomatenMusic.Services
{
public interface ISpotifyService
{
public Task<MusicActionResponse> ConvertURL(string url);
public Task<SpotifyPlaylist> PopulateSpotifyPlaylistAsync(SpotifyPlaylist playlist, FullPlaylist spotifyPlaylist = null);
public Task<SpotifyPlaylist> PopulateSpotifyAlbumAsync(SpotifyPlaylist playlist, FullAlbum spotifyAlbum = null);
public Task<LavalinkTrack> PopulateTrackAsync(LavalinkTrack track, FullTrack spotifyFullTrack = null);
2022-03-19 22:28:54 +01:00
}
public class SpotifyService : SpotifyClient, ISpotifyService
{
public ILogger _logger { get; set; }
public IAudioService _audioService { get; set; }
public ObjectCache Cache { get; set; }
2022-03-19 22:28:54 +01:00
public SpotifyService(SpotifyClientConfig config, ILogger<SpotifyService> logger, IAudioService audioService) : base(config)
{
_logger = logger;
_audioService = audioService;
Cache = MemoryCache.Default;
2022-03-19 22:28:54 +01:00
}
public async Task<MusicActionResponse> ConvertURL(string url)
{
string trackId = url
.Replace("https://open.spotify.com/track/", "")
.Replace("https://open.spotify.com/album/", "")
.Replace("https://open.spotify.com/playlist/", "")
.Substring(0, 22);
_logger.LogDebug($"Starting spotify conversion for: {url}");
2022-03-19 22:28:54 +01:00
if (url.StartsWith("https://open.spotify.com/track"))
{
FullTrack sTrack = Cache.Contains(trackId) ? Cache.Get(trackId) as FullTrack : await Tracks.Get(trackId);
2022-03-19 22:28:54 +01:00
_logger.LogInformation($"Searching youtube from spotify with query: {sTrack.Name} {String.Join(" ", sTrack.Artists)}");
var track = await _audioService.GetTrackAsync($"{sTrack.Name} {String.Join(" ", sTrack.Artists.ConvertAll(artist => artist.Name))}", Lavalink4NET.Rest.SearchMode.YouTube);
if (track == null) throw new ArgumentException("This Spotify Track was not found on Youtube");
Cache.Add(trackId, sTrack, DateTimeOffset.MaxValue);
return new MusicActionResponse(await FullTrackContext.PopulateAsync(track, sTrack));
2022-03-19 22:28:54 +01:00
}
else if (url.StartsWith("https://open.spotify.com/album"))
{
List<LavalinkTrack> tracks = new List<LavalinkTrack>();
FullAlbum album = Cache.Contains(trackId) ? Cache.Get(trackId) as FullAlbum : await Albums.Get(trackId);
2022-03-19 22:28:54 +01:00
foreach (var sTrack in await PaginateAll(album.Tracks))
{
_logger.LogInformation($"Searching youtube from spotify with query: {sTrack.Name} {String.Join(" ", sTrack.Artists.ConvertAll(artist => artist.Name))}");
var track = await _audioService.GetTrackAsync($"{sTrack.Name} {String.Join(" ", sTrack.Artists.ConvertAll(artist => artist.Name))}", Lavalink4NET.Rest.SearchMode.YouTube);
if (track == null) throw new ArgumentException("This Spotify Track was not found on Youtube");
tracks.Add(await FullTrackContext.PopulateAsync(track, spotifyId: sTrack.Uri.Replace("spotify:track:", "")));
2022-03-19 22:28:54 +01:00
}
Uri uri;
Uri.TryCreate(url, UriKind.Absolute, out uri);
SpotifyPlaylist playlist = new SpotifyPlaylist(album.Name, album.Id, tracks, uri);
await PopulateSpotifyAlbumAsync(playlist, album);
Cache.Add(trackId, album, DateTimeOffset.MaxValue);
2022-03-19 22:28:54 +01:00
return new MusicActionResponse(playlist: playlist);
}
else if (url.StartsWith("https://open.spotify.com/playlist"))
{
2022-03-19 22:28:54 +01:00
List<LavalinkTrack> tracks = new List<LavalinkTrack>();
FullPlaylist spotifyPlaylist = Cache.Contains(trackId) ? Cache.Get(trackId) as FullPlaylist : await Playlists.Get(trackId);
2022-03-19 22:28:54 +01:00
foreach (var sTrack in await PaginateAll(spotifyPlaylist.Tracks))
{
if (sTrack.Track is FullTrack)
{
FullTrack fullTrack = (FullTrack)sTrack.Track;
_logger.LogInformation($"Searching youtube from spotify with query: {fullTrack.Name} {String.Join(" ", fullTrack.Artists.ConvertAll(artist => artist.Name))}");
var track = await _audioService.GetTrackAsync($"{fullTrack.Name} {String.Join(" ", fullTrack.Artists.ConvertAll(artist => artist.Name))}", Lavalink4NET.Rest.SearchMode.YouTube);
if (track == null) throw new ArgumentException("This Spotify Track was not found on Youtube");
tracks.Add(await FullTrackContext.PopulateAsync(track, fullTrack));
2022-03-19 22:28:54 +01:00
}
}
Uri uri;
Uri.TryCreate(url, UriKind.Absolute, out uri);
SpotifyPlaylist playlist = new SpotifyPlaylist(spotifyPlaylist.Name, spotifyPlaylist.Id, tracks, uri);
await PopulateSpotifyPlaylistAsync(playlist, spotifyPlaylist);
Cache.Add(trackId, spotifyPlaylist, DateTimeOffset.MaxValue);
2022-03-19 22:28:54 +01:00
return new MusicActionResponse(playlist: playlist);
}
return null;
}
public async Task<SpotifyPlaylist> PopulateSpotifyPlaylistAsync(SpotifyPlaylist playlist, FullPlaylist spotifyPlaylist = null)
2022-03-19 22:28:54 +01:00
{
FullPlaylist list = spotifyPlaylist;
if (list == null)
list = await this.Playlists.Get(playlist.Identifier);
string desc = list.Description;
playlist.Description = desc.Substring(0, Math.Min(desc.Length, 1024)) + (desc.Length > 1020 ? "..." : " ");
if (playlist.Description.Length < 2)
playlist.Description = "None";
playlist.AuthorUri = new Uri($"https://open.spotify.com/user/{list.Owner.Id}");
2022-03-19 22:28:54 +01:00
playlist.AuthorName = list.Owner.DisplayName;
playlist.Followers = list.Followers.Total;
playlist.Url = new Uri($"https://open.spotify.com/playlist/{playlist.Identifier}");
try
{
playlist.AuthorThumbnail = new Uri(list.Owner.Images.First().Url);
}
catch (Exception ex) { }
2022-03-19 22:28:54 +01:00
return playlist;
}
public async Task<SpotifyPlaylist> PopulateSpotifyAlbumAsync(SpotifyPlaylist playlist, FullAlbum spotifyAlbum = null)
2022-03-19 22:28:54 +01:00
{
FullAlbum list = spotifyAlbum;
if (list == null)
list = await this.Albums.Get(playlist.Identifier);
string desc = list.Label;
playlist.Description = desc.Substring(0, Math.Min(desc.Length, 1024)) + (desc.Length > 1020 ? "..." : " ");
playlist.AuthorUri = new Uri($"https://open.spotify.com/user/{list.Artists.First().Uri}");
2022-03-19 22:28:54 +01:00
playlist.AuthorName = list.Artists.First().Name;
playlist.Followers = list.Popularity;
playlist.Url = new Uri($"https://open.spotify.com/album/{playlist.Identifier}");
2022-03-19 22:28:54 +01:00
return playlist;
}
public async Task<LavalinkTrack> PopulateTrackAsync(LavalinkTrack track, FullTrack spotifyFullTrack)
2022-03-19 22:28:54 +01:00
{
FullTrackContext context = (FullTrackContext)track.Context;
if (context.SpotifyIdentifier == null)
return track;
FullTrack spotifyTrack = spotifyFullTrack;
if (spotifyTrack == null)
spotifyTrack = await Tracks.Get(context.SpotifyIdentifier);
2022-03-19 22:28:54 +01:00
context.SpotifyAlbum = spotifyTrack.Album;
context.SpotifyArtists = spotifyTrack.Artists;
context.SpotifyPopularity = spotifyTrack.Popularity;
context.SpotifyUri = new Uri($"https://open.spotify.com/track/{context.SpotifyIdentifier}");
2022-03-19 22:28:54 +01:00
track.Context = context;
return track;
}
}
}