private async Task <OccurrenceSearcher> FindPlaylistOccurences(Entities.Application.Playlist compared, List <Place> places, string spotifyToken, HttpClient client) { var occurrences = new OccurrenceSearcher(); await places.ToAsyncEnumerable().ForEachAwaitAsync(async(Place place) => { var header = await _spotify.GetPlaylistHeader(client, spotifyToken, place.MainPlaylist.Id); if (header != null) { if (header.Snapshot_id == place.MainPlaylist.SnapshotVersion && place.MainPlaylist.Tags != null && place.MainPlaylist.Tags.Any() && place.MainPlaylist.Genres != null && place.MainPlaylist.Genres.Any()) { occurrences.UpdateTags(place.MainPlaylist.Tags, place.MainPlaylist.Id); occurrences.UpdateGenres(place.MainPlaylist.Genres, place.MainPlaylist.Id); } else { var playlistSongs = (await _spotify.GetSongsFromPlaylist(client, spotifyToken, place.MainPlaylist.Id)) .Items.Select(s => new Entities.Application.Song(s)).ToList(); await GetTagsAndGenresFromSongs(spotifyToken, place.MainPlaylist.Id, playlistSongs, occurrences); place.MainPlaylist.SnapshotVersion = header.Snapshot_id; place.MainPlaylist.Songs = playlistSongs; place.MainPlaylist.Tags = occurrences.GetTags(place.MainPlaylist.Id); place.MainPlaylist.Genres = occurrences.GetGenres(place.MainPlaylist.Id); } } }); await GetTagsAndGenresFromSongs(spotifyToken, compared.Id, compared.Songs, occurrences); compared.Tags = occurrences.GetTags(compared.Id); compared.Genres = occurrences.GetGenres(compared.Id); return(occurrences); }
private async Task GetTagsAndGenresFromSongs(string token, string playlistId, List <Entities.Application.Song> songs, OccurrenceSearcher occurrences) { var client = new HttpClient(); var lastFmTagOccurrenceConc = new ConcurrentDictionary <string, int>(); var spotifyGenreOccurrenceConc = new ConcurrentDictionary <string, int>(); await songs.ToAsyncEnumerable().ForEachAwaitAsync(async(song) => { var fmSuccess = false; while (!fmSuccess) { try { var lastFmTags = await lastFmService.GetTrackTags(client, song.Name, song.Artists[0]?.Name); if (lastFmTags != null && lastFmTags.Toptags != null && lastFmTags.Toptags.Tag != null) { lastFmTags.Toptags.Tag.Sort(new TagComparer()); lastFmTags.Toptags.Tag.Take(5).ToList().ForEach(t => { lastFmTagOccurrenceConc.AddOrUpdate(t.Name, 1, (k, old) => old + 1); }); } fmSuccess = true; } catch (TaskCanceledException) { } } var artists = song.Artists.Select(a => a.Id).ToList(); if (artists.Any()) { var newArtists = artists.Where(a => !occurrences.ArtistExists(a)).ToList(); if (newArtists.Any()) { newArtists.ForEach(a => occurrences.UpdateArtistGenres(new List <string>(), a)); var stringArtists = newArtists.Aggregate((a, b) => a + "," + b); var spotifySuccess = false; while (!spotifySuccess) { try { var spotifyTags = await _spotify.GetArtists(client, token, stringArtists); if (spotifyTags != null) { spotifyTags.ForEach(a => occurrences.UpdateArtistGenres(a.Genres.ToList(), a.Id)); spotifySuccess = true; } } catch (TaskCanceledException) { } } } artists.ForEach(s => { occurrences.GetArtistGenres(s).ForEach(g => { // Add Genre occurrence spotifyGenreOccurrenceConc.AddOrUpdate(g, 1, (k, l) => l + 1); }); }); } }); occurrences.UpdateTags(new Dictionary <string, int>(lastFmTagOccurrenceConc), playlistId); occurrences.UpdateGenres(new Dictionary <string, int>(spotifyGenreOccurrenceConc), playlistId); }