add refresh token to spotify
This commit is contained in:
parent
7a38d774c3
commit
9ef8af8e23
@ -5,36 +5,35 @@ namespace Kasbot.App.Services.Internal
|
|||||||
{
|
{
|
||||||
public class SpotifyService
|
public class SpotifyService
|
||||||
{
|
{
|
||||||
private readonly SpotifyClient? spotifyClient = null;
|
private readonly string spotifyClientId = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID") ?? string.Empty;
|
||||||
|
private readonly string spotifyClientSecret = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_SECRET") ?? string.Empty;
|
||||||
|
|
||||||
|
private SpotifyClient? spotifyClient = null;
|
||||||
private ILogger Logger { get; set; }
|
private ILogger Logger { get; set; }
|
||||||
|
|
||||||
public SpotifyService(ILogger logger)
|
public SpotifyService(ILogger logger)
|
||||||
{
|
{
|
||||||
this.Logger = logger;
|
this.Logger = logger;
|
||||||
|
|
||||||
this.spotifyClient = SetupSpotifyClient();
|
SetupSpotifyClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
private SpotifyClient SetupSpotifyClient()
|
private void SetupSpotifyClient()
|
||||||
{
|
{
|
||||||
var spotifyClientId = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_ID");
|
if (string.IsNullOrWhiteSpace(spotifyClientId) ||
|
||||||
var spotifyClientSecret = Environment.GetEnvironmentVariable("SPOTIFY_CLIENT_SECRET");
|
string.IsNullOrWhiteSpace(spotifyClientSecret))
|
||||||
|
|
||||||
if (spotifyClientId == null || spotifyClientSecret == null)
|
|
||||||
{
|
{
|
||||||
Logger.Warning("Spotify Token was not found. Will disable Spotify integration.");
|
Logger.Warning("Spotify Token was not found. Will disable Spotify integration.");
|
||||||
return null;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var config = SpotifyClientConfig.CreateDefault();
|
if (RefreshToken().IsFaulted)
|
||||||
|
{
|
||||||
var request = new ClientCredentialsRequest(spotifyClientId, spotifyClientSecret);
|
throw new Exception("Failed to create Spotify client.");
|
||||||
var response = new OAuthClient(config).RequestToken(request).Result;
|
}
|
||||||
|
|
||||||
return new SpotifyClient(config.WithToken(response.AccessToken));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Media> FetchSingleMedia(Media media)
|
private async Task CheckTokenValid()
|
||||||
{
|
{
|
||||||
if (spotifyClient == null)
|
if (spotifyClient == null)
|
||||||
{
|
{
|
||||||
@ -42,6 +41,35 @@ namespace Kasbot.App.Services.Internal
|
|||||||
throw new Exception("Spotify integration is disabled.");
|
throw new Exception("Spotify integration is disabled.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await spotifyClient.Browse.GetCategories();
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
await RefreshToken();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task RefreshToken()
|
||||||
|
{
|
||||||
|
if (spotifyClient == null)
|
||||||
|
{
|
||||||
|
spotifyClient = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var config = SpotifyClientConfig.CreateDefault();
|
||||||
|
|
||||||
|
var request = new ClientCredentialsRequest(spotifyClientId, spotifyClientSecret);
|
||||||
|
var response = await (new OAuthClient(config)).RequestToken(request);
|
||||||
|
|
||||||
|
spotifyClient = new SpotifyClient(config.WithToken(response.AccessToken));
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Media> FetchSingleMedia(Media media)
|
||||||
|
{
|
||||||
|
await CheckTokenValid();
|
||||||
|
|
||||||
var trackId = UrlResolver.GetSpotifyResourceId(media.Search);
|
var trackId = UrlResolver.GetSpotifyResourceId(media.Search);
|
||||||
var spotifyTrack = await spotifyClient.Tracks.Get(trackId);
|
var spotifyTrack = await spotifyClient.Tracks.Get(trackId);
|
||||||
|
|
||||||
@ -58,11 +86,7 @@ namespace Kasbot.App.Services.Internal
|
|||||||
|
|
||||||
public async Task<MediaCollection> FetchPlaylist(Media rawMedia)
|
public async Task<MediaCollection> FetchPlaylist(Media rawMedia)
|
||||||
{
|
{
|
||||||
if (spotifyClient == null)
|
await CheckTokenValid();
|
||||||
{
|
|
||||||
Logger.Warning("Spotify integration is disabled.");
|
|
||||||
throw new Exception("Spotify integration is disabled.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var playlistId = UrlResolver.GetSpotifyResourceId(rawMedia.Search);
|
var playlistId = UrlResolver.GetSpotifyResourceId(rawMedia.Search);
|
||||||
var spotifyPlaylist = await spotifyClient.Playlists.Get(playlistId);
|
var spotifyPlaylist = await spotifyClient.Playlists.Get(playlistId);
|
||||||
@ -104,11 +128,7 @@ namespace Kasbot.App.Services.Internal
|
|||||||
|
|
||||||
public async Task<MediaCollection> FetchAlbum(Media rawMedia)
|
public async Task<MediaCollection> FetchAlbum(Media rawMedia)
|
||||||
{
|
{
|
||||||
if (spotifyClient == null)
|
await CheckTokenValid();
|
||||||
{
|
|
||||||
Logger.Warning("Spotify integration is disabled.");
|
|
||||||
throw new Exception("Spotify integration is disabled.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var albumId = UrlResolver.GetSpotifyResourceId(rawMedia.Search);
|
var albumId = UrlResolver.GetSpotifyResourceId(rawMedia.Search);
|
||||||
var spotifyAlbum = await spotifyClient.Albums.Get(albumId);
|
var spotifyAlbum = await spotifyClient.Albums.Get(albumId);
|
||||||
|
@ -81,6 +81,7 @@ namespace Kasbot.Services
|
|||||||
{
|
{
|
||||||
case SearchType.StringSearch:
|
case SearchType.StringSearch:
|
||||||
case SearchType.VideoURL:
|
case SearchType.VideoURL:
|
||||||
|
case SearchType.VideoPlaylistURL:
|
||||||
case SearchType.SpotifyTrack:
|
case SearchType.SpotifyTrack:
|
||||||
Logger.Debug($"Fetching {media.Search} as {mediaType}");
|
Logger.Debug($"Fetching {media.Search} as {mediaType}");
|
||||||
|
|
||||||
@ -97,7 +98,6 @@ namespace Kasbot.Services
|
|||||||
conn.Queue.Enqueue(media);
|
conn.Queue.Enqueue(media);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case SearchType.VideoPlaylistURL:
|
|
||||||
case SearchType.YoutubePlaylist:
|
case SearchType.YoutubePlaylist:
|
||||||
case SearchType.SpotifyPlaylist:
|
case SearchType.SpotifyPlaylist:
|
||||||
case SearchType.SpotifyAlbum:
|
case SearchType.SpotifyAlbum:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user