Пример #1
0
        // Random user
        public async Task <string> GetRandomLastFMUserAsync()
        {
            await using var db = new FMBotDbContext(ConfigData.Data.Database.ConnectionString);
            var featuredUser = await db.Users
                               .AsQueryable()
                               .FirstOrDefaultAsync(f => f.Featured == true);

            if (featuredUser != null)
            {
                featuredUser.Featured = false;

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

            var users = db.Users
                        .AsQueryable()
                        .Where(w => w.Blacklisted != true).ToList();

            var rand = new Random();
            var user = users[rand.Next(users.Count)];

            user.Featured = true;

            db.Entry(user).State = EntityState.Modified;
            await db.SaveChangesAsync();

            return(user.UserNameLastFM);
        }
Пример #2
0
        public async Task SetGuildPrefixAsync(IGuild guild, string prefix)
        {
            await using var db = new FMBotDbContext(ConfigData.Data.Database.ConnectionString);
            var existingGuild = await db.Guilds
                                .AsQueryable()
                                .FirstOrDefaultAsync(f => f.DiscordGuildId == guild.Id);

            if (existingGuild == null)
            {
                var newGuild = new Guild
                {
                    DiscordGuildId  = guild.Id,
                    TitlesEnabled   = true,
                    ChartTimePeriod = ChartTimePeriod.Monthly,
                    FmEmbedType     = FmEmbedType.embedmini,
                    Name            = guild.Name,
                    Prefix          = prefix
                };

                await db.Guilds.AddAsync(newGuild);

                await db.SaveChangesAsync();
            }
            else
            {
                existingGuild.Prefix = prefix;
                existingGuild.Name   = guild.Name;

                db.Entry(existingGuild).State = EntityState.Modified;

                await db.SaveChangesAsync();
            }
        }
Пример #3
0
        public async Task <bool> SetUserTypeAsync(string discordUserID, UserType userType)
        {
            User user = await db.Users.FirstOrDefaultAsync(f => f.DiscordUserID == discordUserID);

            if (user == null)
            {
                return(false);
            }

            user.UserType = userType;

            db.Entry(user).State = EntityState.Modified;

            await db.SaveChangesAsync();

            return(true);
        }
Пример #4
0
        public async Task ResetChartTimerAsync(User user)
        {
            await using var db = new FMBotDbContext(ConfigData.Data.Database.ConnectionString);
            user.LastGeneratedChartDateTimeUtc = DateTime.Now;

            db.Entry(user).State = EntityState.Modified;

            await db.SaveChangesAsync();
        }
Пример #5
0
        // Random user
        public async Task <string> GetRandomLastFMUserAsync()
        {
            User featuredUser = await db.Users.FirstOrDefaultAsync(f => f.Featured == true).ConfigureAwait(false);

            if (featuredUser != null)
            {
                featuredUser.Featured = false;

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

            List <User> users = db.Users.Where(w => w.Blacklisted != true).ToList();

            Random rand = new Random();
            User   user = users[rand.Next(users.Count)];

            user.Featured = true;

            db.Entry(user).State = EntityState.Modified;
            db.SaveChanges();

            return(user.UserNameLastFM);
        }
Пример #6
0
        // Set LastFM Name
        public void SetLastFM(IUser discordUser, User newUserSettings)
        {
            using var db = new FMBotDbContext(ConfigData.Data.Database.ConnectionString);
            var user = db.Users.FirstOrDefault(f => f.DiscordUserId == discordUser.Id);

            this._cache.Remove($"user-settings-{discordUser.Id}");
            this._cache.Remove($"user-isRegistered-{discordUser.Id}");

            if (user == null)
            {
                var newUser = new User
                {
                    DiscordUserId    = discordUser.Id,
                    UserType         = UserType.User,
                    UserNameLastFM   = newUserSettings.UserNameLastFM,
                    TitlesEnabled    = true,
                    ChartTimePeriod  = ChartTimePeriod.Monthly,
                    FmEmbedType      = newUserSettings.FmEmbedType,
                    FmCountType      = newUserSettings.FmCountType,
                    SessionKeyLastFm = newUserSettings.SessionKeyLastFm
                };

                db.Users.Add(newUser);

                try
                {
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    Log.Error(e, "Error in SetLastFM");
                    throw;
                }
            }
            else
            {
                user.UserNameLastFM = newUserSettings.UserNameLastFM;
                user.FmEmbedType    = newUserSettings.FmEmbedType;
                user.FmCountType    = newUserSettings.FmCountType;

                if (!string.Equals(user.UserNameLastFM, newUserSettings.UserNameLastFM, StringComparison.CurrentCultureIgnoreCase))
                {
                    user.SessionKeyLastFm = newUserSettings.SessionKeyLastFm;
                }

                db.Entry(user).State = EntityState.Modified;

                db.SaveChanges();
            }
        }
Пример #7
0
        public async Task <bool> AddUserToBlacklistAsync(ulong discordUserID)
        {
            await using var db = new FMBotDbContext(ConfigData.Data.Database.ConnectionString);
            var user = await db.Users.FirstOrDefaultAsync(f => f.DiscordUserId == discordUserID);

            if (user == null)
            {
                return(false);
            }

            user.Blacklisted = true;

            db.Entry(user).State = EntityState.Modified;

            await db.SaveChangesAsync();

            return(true);
        }
Пример #8
0
        // Set LastFM Name
        public void SetLastFM(IUser discordUser, User userSettings)
        {
            using var db = new FMBotDbContext(ConfigData.Data.Database.ConnectionString);
            var user = db.Users.FirstOrDefault(f => f.DiscordUserId == discordUser.Id);

            if (user == null)
            {
                var newUser = new User
                {
                    DiscordUserId   = discordUser.Id,
                    UserType        = UserType.User,
                    UserNameLastFM  = userSettings.UserNameLastFM,
                    TitlesEnabled   = true,
                    ChartTimePeriod = ChartTimePeriod.Monthly,
                    FmEmbedType     = userSettings.FmEmbedType,
                    FmCountType     = userSettings.FmCountType
                };

                db.Users.Add(newUser);

                try
                {
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }
            else
            {
                user.UserNameLastFM = userSettings.UserNameLastFM;
                user.FmEmbedType    = userSettings.FmEmbedType;
                user.FmCountType    = userSettings.FmCountType;

                db.Entry(user).State = EntityState.Modified;

                db.SaveChanges();
            }
        }
Пример #9
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);
            }
        }
Пример #10
0
        public async Task <Track> GetOrStoreTrackAsync(ResponseTrack track)
        {
            await using var db = new FMBotDbContext(ConfigData.Data.Database.ConnectionString);
            var dbTrack = await db.Tracks
                          .AsQueryable()
                          .FirstOrDefaultAsync(f => f.Name.ToLower() == track.Name.ToLower() && f.ArtistName.ToLower() == track.Artist.Name.ToLower());

            if (dbTrack == null)
            {
                var trackToAdd = new FMBot.Persistence.Domain.Models.Track
                {
                    Name       = track.Name,
                    AlbumName  = track.Album?.Title,
                    ArtistName = track.Artist?.Name
                };

                var artist = await db.Artists
                             .AsQueryable()
                             .FirstOrDefaultAsync(f => f.Name.ToLower() == track.Artist.Name.ToLower());

                if (artist != null)
                {
                    trackToAdd.Artist = artist;
                }

                var spotifyTrack = await GetTrackFromSpotify(track.Name, track.Artist.Name.ToLower());

                if (spotifyTrack != null)
                {
                    trackToAdd.SpotifyId  = spotifyTrack.Id;
                    trackToAdd.DurationMs = spotifyTrack.DurationMs;

                    var audioFeatures = await GetAudioFeaturesFromSpotify(spotifyTrack.Id);

                    if (audioFeatures != null)
                    {
                        trackToAdd.Key   = audioFeatures.Key;
                        trackToAdd.Tempo = audioFeatures.Tempo;
                    }
                }

                trackToAdd.SpotifyLastUpdated = DateTime.UtcNow;

                await db.Tracks.AddAsync(trackToAdd);

                await db.SaveChangesAsync();

                return(trackToAdd);
            }
            else
            {
                if (dbTrack.Artist == null)
                {
                    var artist = await db.Artists
                                 .AsQueryable()
                                 .FirstOrDefaultAsync(f => f.Name.ToLower() == track.Artist.Name.ToLower());

                    if (artist != null)
                    {
                        dbTrack.Artist          = artist;
                        db.Entry(dbTrack).State = EntityState.Modified;
                    }
                }
                if (string.IsNullOrEmpty(dbTrack.SpotifyId) && dbTrack.SpotifyLastUpdated < DateTime.UtcNow.AddMonths(-2))
                {
                    var spotifyTrack = await GetTrackFromSpotify(track.Name, track.Artist.Name.ToLower());

                    if (spotifyTrack != null)
                    {
                        dbTrack.SpotifyId  = spotifyTrack.Id;
                        dbTrack.DurationMs = spotifyTrack.DurationMs;

                        var audioFeatures = await GetAudioFeaturesFromSpotify(spotifyTrack.Id);

                        if (audioFeatures != null)
                        {
                            dbTrack.Key   = audioFeatures.Key;
                            dbTrack.Tempo = audioFeatures.Tempo;
                        }
                    }

                    dbTrack.SpotifyLastUpdated = DateTime.UtcNow;
                    db.Entry(dbTrack).State    = EntityState.Modified;
                }

                await db.SaveChangesAsync();

                return(dbTrack);
            }
        }