private async Task AddSongsToGenreDictionaryAsync( SavedTrackWithGenre genreTrack, ConcurrentDictionary <string, string> genreIdDict, ConcurrentDictionary <string, Collection <string> > newSongsDict, CancellationToken cancellationToken) { await RequestSemaphore.WaitAsync(); cancellationToken.ThrowIfCancellationRequested(); logger.LogInformation($"Adding {genreTrack.Track.Name} to genreList"); foreach (string genre in genreTrack.Genres) { foreach (KeyValuePair <string, string> genres in genreIdDict) { // If the genre exists in any playlist genre, AND isn't already in the newSongsDict. if (genres.Value.Contains(genre) && (!newSongsDict[genres.Key].Contains(genre))) { newSongsDict[genres.Key].Add(genreTrack.Track.Id); } } } RequestSemaphore.Release(); }
public async Task <SavedTrackWithGenre> GetGenreFromSongAsync( SavedTrackWithGenre genreTrack, CancellationToken cancellationToken) { await RequestSemaphore.WaitAsync(); cancellationToken.ThrowIfCancellationRequested(); Collection <string> genres = new(); foreach (var artist in genreTrack.Track.Artists) { // Could async this too if performance is really taking a hit, but it probably isn't necessary. logger.LogInformation($"Getting Genres for artist {artist} for song {genreTrack.Track.Name}"); // This call specifically seems to give me trouble when trying to leverage async. var fullArtist = await Api.GetArtistAsync(artist.Id); if (fullArtist.Genres != null) { // There was some weird behavior with using IEnumerable here where Linq's ConcatNIterator couldn't // be cast into a collection when assigning back to genreTracks. This will have to do for the meantime. genres = new Collection <string>(genres.Concat(fullArtist.Genres).ToList()); } } genreTrack.Genres = genres; cancellationToken.ThrowIfCancellationRequested(); RequestSemaphore.Release(); return(genreTrack); }