public IEnumerable <string> Get() { using var db = _database.Open(); var processed = new HashSet <string>( db.GetCollection <SongData>().Query() .Select(s => s.Hash.ToUpper()).ToEnumerable()); var result = db.GetCollection <Song>().Query() .Select(s => s.Hash).ToEnumerable().Distinct() .Where(s => !processed.Contains(s.ToUpper())) .ToList(); return(result); }
public IEnumerable <SongResponse> Get([FromQuery] double avgScore, [FromQuery] double minScore) { using var db = _database.Open(); _logger.LogTrace($"Request [{avgScore} | {minScore}]"); var songCol = db.GetCollection <Song>(); var dataCol = db.GetCollection <SongData>(); var goodSongs = songCol.Query() .Where(s => s.ScoreToPass <avgScore && s.WorstScore> minScore) .ToEnumerable(); var songs = from song in goodSongs let data = dataCol.Query() .Where(s => s.Hash == song.Hash) .FirstOrDefault() where data != null select new SongResponse { Key = data.Key, Hash = song.Hash, SongName = data.SongName, Uploader = data.Uploader, Difficulty = song.Difficulty, BestScore = song.BestScore, WorstScore = song.WorstScore }; var songList = songs.ToList(); return(songList); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { foreach (var page in _targetPages) { // Wait await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken); // Get user id & score string id; try { var users = await GetUsersOnPage(page, stoppingToken); id = users[_random.Next(users.Count)].id; } catch (Exception e) { _logger.LogError(e, $"Failed to get page #{page}!"); continue; } // Get user songs List<UserSong> songs; double playerScore; try { songs = await GetUserSongs(id, stoppingToken); playerScore = songs.Select(s => s.Score).OrderByDescending(s => s) .Take(8).Average(); } catch (Exception e) { _logger.LogError(e, $"Failed to get user #{id}!"); continue; } // Put to base try { using var db = _database.Open(); var songCol = db.GetCollection<Song>(); foreach (var song in songs) { var dbSong = songCol.Query() .Where(s => s.Hash == song.Hash && s.Difficulty == song.Difficulty) .FirstOrDefault(); if (dbSong == null) { songCol.Insert(new Song() { Hash = song.Hash, Difficulty = song.Difficulty, BestScore = song.Score, WorstScore = song.Score, ScoreToPass = playerScore, LastFounded = DateTime.Now }); } else { dbSong.BestScore = MoveValue(dbSong.BestScore, song.Score, true); dbSong.WorstScore = MoveValue(dbSong.WorstScore, song.Score, false); dbSong.ScoreToPass = MoveValue(dbSong.ScoreToPass, playerScore, false); dbSong.LastFounded = DateTime.Now; songCol.Update(dbSong); } } } catch (Exception e) { _logger.LogError(e, $"Failed to update database!"); } } // Wait a lot await Task.Delay(TimeSpan.FromMinutes(10), stoppingToken); } }