Ejemplo n.º 1
0
 private static void AddAliasToExistingArtist(string artistNameBeforeCorrect, Artist dbArtist)
 {
     if (dbArtist.Aliases != null && dbArtist.Aliases.Length > 0 && !dbArtist.Aliases.Contains(artistNameBeforeCorrect))
     {
         var aliases = dbArtist.Aliases;
         Array.Resize(ref aliases, aliases.Length + 1);
         aliases[^ 1]     = artistNameBeforeCorrect;
Ejemplo n.º 2
0
        public async Task <string> GetOrStoreArtistImageAsync(ArtistResponse lastFmArtist, string artistNameBeforeCorrect)
        {
            await using var db = new FMBotDbContext(ConfigData.Data.Database.ConnectionString);
            var artist = await db.Artists
                         .AsQueryable()
                         .FirstOrDefaultAsync(f => f.Name.ToLower() == lastFmArtist.Artist.Name.ToLower());

            var imageUrlToReturn = "";

            if (artist == null)
            {
                var spotifyArtist = await GetArtistFromSpotify(lastFmArtist.Artist.Name);

                var artistToAdd = new Artist
                {
                    Name      = lastFmArtist.Artist.Name,
                    LastFmUrl = lastFmArtist.Artist.Url,
                    Mbid      = lastFmArtist.Artist.Mbid
                };

                if (spotifyArtist != null)
                {
                    artistToAdd.SpotifyId = spotifyArtist.Id;

                    if (spotifyArtist.Images.Any())
                    {
                        artistToAdd.SpotifyImageUrl  = spotifyArtist.Images.OrderByDescending(o => o.Height).First().Url;
                        artistToAdd.SpotifyImageDate = DateTime.UtcNow;
                        imageUrlToReturn             = artistToAdd.SpotifyImageUrl;
                    }
                }

                if (!string.Equals(artistNameBeforeCorrect, lastFmArtist.Artist.Name, StringComparison.CurrentCultureIgnoreCase))
                {
                    artistToAdd.Aliases = new[] { artistNameBeforeCorrect };
                }

                await db.Artists.AddAsync(artistToAdd);

                await db.SaveChangesAsync();
            }
            else
            {
                if (!string.Equals(artistNameBeforeCorrect, lastFmArtist.Artist.Name, StringComparison.CurrentCultureIgnoreCase))
                {
                    if (artist.Aliases != null && artist.Aliases.Length > 0)
                    {
                        var aliases = artist.Aliases;
                        Array.Resize(ref aliases, aliases.Length + 1);
                        aliases[^ 1]   = artistNameBeforeCorrect;
Ejemplo n.º 3
0
        public async Task <string> GetOrStoreArtistImageAsync(ArtistResponse lastFmArtist, string artistNameBeforeCorrect)
        {
            await using var db = new FMBotDbContext(ConfigData.Data.Database.ConnectionString);
            var dbArtist = await db.Artists
                           .AsQueryable()
                           .FirstOrDefaultAsync(f => f.Name.ToLower() == lastFmArtist.Artist.Name.ToLower());

            var imageUrlToReturn = "";

            try
            {
                if (dbArtist == null)
                {
                    var spotifyArtist = await GetArtistFromSpotify(lastFmArtist.Artist.Name);

                    var artistToAdd = new Artist
                    {
                        Name      = lastFmArtist.Artist.Name,
                        LastFmUrl = lastFmArtist.Artist.Url,
                        Mbid      = lastFmArtist.Artist.Mbid
                    };

                    if (spotifyArtist != null)
                    {
                        artistToAdd.SpotifyId = spotifyArtist.Id;

                        if (spotifyArtist.Images.Any())
                        {
                            artistToAdd.SpotifyImageUrl  = spotifyArtist.Images.OrderByDescending(o => o.Height).First().Url;
                            artistToAdd.SpotifyImageDate = DateTime.UtcNow;
                            imageUrlToReturn             = artistToAdd.SpotifyImageUrl;
                        }
                    }

                    if (!string.Equals(artistNameBeforeCorrect, lastFmArtist.Artist.Name, StringComparison.CurrentCultureIgnoreCase))
                    {
                        artistToAdd.Aliases = new[] { artistNameBeforeCorrect };
                    }

                    await db.Artists.AddAsync(artistToAdd);

                    await db.SaveChangesAsync();
                }
                else
                {
                    if (!string.Equals(artistNameBeforeCorrect, lastFmArtist.Artist.Name, StringComparison.CurrentCultureIgnoreCase))
                    {
                        AddAliasToExistingArtist(artistNameBeforeCorrect, dbArtist);

                        db.Entry(dbArtist).State = EntityState.Modified;
                    }

                    if (dbArtist.SpotifyImageUrl == null || dbArtist.SpotifyImageDate < DateTime.UtcNow.AddMonths(-2))
                    {
                        var spotifyArtist = await GetArtistFromSpotify(lastFmArtist.Artist.Name);

                        if (spotifyArtist != null && spotifyArtist.Images.Any())
                        {
                            dbArtist.SpotifyImageUrl = spotifyArtist.Images.OrderByDescending(o => o.Height).First().Url;
                            imageUrlToReturn         = dbArtist.SpotifyImageUrl;
                        }

                        dbArtist.SpotifyImageDate = DateTime.UtcNow;
                        db.Entry(dbArtist).State  = EntityState.Modified;
                    }
                    else
                    {
                        imageUrlToReturn = dbArtist.SpotifyImageUrl;
                    }

                    await db.SaveChangesAsync();
                }

                return(!string.IsNullOrEmpty(imageUrlToReturn) ? imageUrlToReturn : null);
            }
            catch (Exception e)
            {
                Log.Error(e, "Something went wrong while retrieving artist image");
                return(null);
            }
        }