Ejemplo n.º 1
0
        private static async Task <SpotifyAlbumDownloadModel> FetchAlbumForIndexAsync(
            string albumIdentifier,
            SpotifyAPICredentials spotifyAPICredentials)
        {
            var spotifyClient = SearchHelper.CreateSpotifyClient(spotifyAPICredentials);

            var album = await spotifyClient.Albums.Get(albumIdentifier);

            var artists = await ArtistModel.FetchArtistsAsync(album.Artists.Select(a => a.Id).ToList(), spotifyAPICredentials);

            var spotifyAlbumDownloadModel = new SpotifyAlbumDownloadModel
            {
                Artists = artists,
                Album   = album
            };

            return(spotifyAlbumDownloadModel);
        }
Ejemplo n.º 2
0
        private static async Task <List <TrackArtist> > MapTrackArtistsAsync(
            SimpleTrack track,
            HashSet <string> albumArtistsSpotifyIds,
            IEnumerable <Artist> albumArtists,
            IRepositoryManager repositoryManager,
            IMapper mapper,
            SpotifyAPICredentials spotifyAPICredentials)
        {
            List <TrackArtist> trackArtists            = new List <TrackArtist>();
            List <string>      trackArtistsToBeCreated = new List <string>();
            var trackArtistSpotifyIds = track.Artists.Select(a => a.Id).ToHashSet();

            foreach (var trackArtist in track.Artists)
            {
                if (albumArtistsSpotifyIds.Contains(trackArtist.Id))
                {
                    var albumArtist = albumArtists.Where(a => a.SpotifyId == trackArtist.Id).FirstOrDefault();

                    trackArtists.Add(new TrackArtist
                    {
                        ArtistId      = albumArtist.Id,
                        CreatedOn     = DateTime.Now,
                        LastUpdatedOn = DateTime.Now
                    });
                }
                else
                {
                    trackArtistsToBeCreated.Add(trackArtist.Id);
                }
            }

            if (trackArtistsToBeCreated.Count > 0)
            {
                var newArtists = await ArtistModel.FetchArtistsAsync(trackArtistsToBeCreated, spotifyAPICredentials);

                var artists = await ArtistModel.CreateOrFetchArtistAsync(newArtists, repositoryManager, mapper);

                trackArtists.AddRange(MapArtistsToTrackArtists(artists.Select(a => a.Id).ToList()));
            }

            return(trackArtists);
        }