public static async Task <SpotifyClient> GetAsync() { if (_spotifyClient == null || _tokenResponse == null || _tokenResponse.IsExpired) { if (_CLIENT_ID == "CHANGEME" || _CLIENT_SECRET == "CHANGEME") { throw new Exception("You need to override _CLIENT_ID and _CLIENT_SECRET with the values from your Spotify dev account"); } var request = new ClientCredentialsRequest(_CLIENT_ID, _CLIENT_SECRET); _tokenResponse = await new OAuthClient(_spotifyClientConfig).RequestToken(request); _spotifyClient = new SpotifyClient(_spotifyClientConfig.WithToken(_tokenResponse.AccessToken)); } return(_spotifyClient); }
/// <summary> /// Can't use this because Spotify enforces a limit of 10,000 items, even though there may be 10 times that number of /// tag:new album results. Can't filter futher by AlbumType or release date either. https://github.com/spotify/web-api/issues/862 /// </summary> public async Task <GetNewAlbumsOutput> GetNewAlbums(GetNewAlbumsInput input) { var albums = new List <AlbumDto>(); int max = SpotifyConsts.MaxLimitTotalSearchItems / SpotifyConsts.MaxLimitGetSearchItems; int i = 0; while (i < max) { //Allows us to only request a new API access token for the first time, or when the existing one expires if (_apiWithClientCredentials == null || _apiAccessToken.IsExpired) { var apiOutput = await GetApiWithClientCredentials(); _apiWithClientCredentials = apiOutput.Api; _apiAccessToken = apiOutput.TokenResponse; } int offset = i * SpotifyConsts.MaxLimitGetSearchItems; //tag:new retrieves only albums released in the last two weeks var search = _apiWithClientCredentials.Search; var searchRequest = new SearchRequest(SearchRequest.Types.Album, "tag:new"); searchRequest.Limit = SpotifyConsts.MaxLimitGetSearchItems; searchRequest.Offset = offset; searchRequest.Market = SpotifyConsts.MarketToUse; try { var searchResponse = await search.Item(searchRequest); if (searchResponse.Albums.Items == null || searchResponse.Albums.Items.Count == 0) { break; } albums.AddRange( searchResponse.Albums.Items.Select(albumItem => new AlbumDto { SpotifyId = albumItem.Id, Name = albumItem.Name, Image = GetImage(albumItem.Images, 200), ReleaseDate = albumItem.ReleaseDate, //Artists = albumItem.Artists.Select(artistItem => new ArtistDto //{ // SpotifyId = artistItem.Id, // Name = artistItem.Name //}).ToList() }) ); if (String.IsNullOrEmpty(searchResponse.Albums.Next)) { break; } i++; } catch (APITooManyRequestsException ex) { await HandleRateLimitingError(ex); } catch (Exception ex) { Logger.LogError(ex, ""); return(new GetNewAlbumsOutput { ErrorMessage = ex.Message }); } } return(new GetNewAlbumsOutput { Albums = albums }); }
public async Task <GetAlbumsForArtistOutput> GetAlbumsForArtist(GetAlbumsForArtistInput input) { if (String.IsNullOrWhiteSpace(input.SpotifyArtistId)) { throw new ArgumentException("SpotifyArtistId must be a valid Spotify Id", "SpotifyArtistId"); } Logger.LogInformation("Getting albums for SpotifyArtistId: {0}", input.SpotifyArtistId); try { var albums = new List <AlbumDto>(); int max = SpotifyConsts.MaxLimitTotalArtistAlbums / SpotifyConsts.MaxLimitGetArtistAlbums; int i = 0; while (i < max) { //Allows us to only request a new API access token for the first time, or when the existing one expires if (_apiWithClientCredentials == null || _apiAccessToken.IsExpired) { var apiOutput = await GetApiWithClientCredentials(); _apiWithClientCredentials = apiOutput.Api; _apiAccessToken = apiOutput.TokenResponse; } int offset = i * SpotifyConsts.MaxLimitGetArtistAlbums; try { var searchResponse = await _apiWithClientCredentials.Artists.GetAlbums(input.SpotifyArtistId, new ArtistsAlbumsRequest { Offset = offset, Market = SpotifyConsts.MarketToUse, IncludeGroupsParam = ArtistsAlbumsRequest.IncludeGroups.Album }); //Assume there are no more if this is true if (searchResponse.Items == null || searchResponse.Items.Count == 0) { break; } albums.AddRange( searchResponse.Items.Select(albumItem => new AlbumDto { SpotifyId = albumItem.Id, Name = albumItem.Name, Image = GetImage(albumItem.Images, 200), ReleaseDate = albumItem.ReleaseDate }) ); if (String.IsNullOrEmpty(searchResponse.Next)) { break; } i++; } catch (APITooManyRequestsException ex) { await HandleRateLimitingError(ex); } catch (Exception ex) { Logger.LogError(ex, ""); return(new GetAlbumsForArtistOutput { ErrorMessage = ex.Message }); } } Logger.LogInformation("Returning {0} albums for SpotifyArtistId: {1}", albums.Count, input.SpotifyArtistId); return(new GetAlbumsForArtistOutput { Albums = albums }); } catch (Exception ex) { Logger.LogError(ex, ""); return(new GetAlbumsForArtistOutput { ErrorMessage = ex.Message }); } }
private async Task RenewAccessTokenAsync() { tokenResponse = await oauthClient.RequestToken(credentialsRequest); spotifyClient = spotifyClientProvider(ClientConfig.WithToken(tokenResponse.AccessToken)); }