public async Task TestGradeParsing() { var client = ClientTests.Client(); await client.Authenticate("xxx", "xxx"); var parser = new GradingParser(client); var g = await parser.GetGradesForAllDegrees(); Assert.IsNotEmpty(g); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { Task <bool> loginTask; if (!string.IsNullOrEmpty(_config.LoginCookie)) { _logger.LogInformation("Logging in with login cookie"); loginTask = _httpClient.Authenticate(_config.LoginCookie); } else if (!string.IsNullOrEmpty(GetPassword()) && !string.IsNullOrEmpty(GetUserName())) { _logger.LogInformation("Logging in with username and password"); loginTask = _httpClient.Authenticate(GetUserName(), GetPassword()); } else { await HandleError("No authentication provided!"); return; } if (!await loginTask) { await HandleError("Authentication for fetching grades failed!"); return; } _logger.LogDebug("Execution started"); _logger.LogInformation($"Storing and saving results to {SaveFilePath}"); var parser = new GradingParser(_httpClient); var degrees = LoadFromFile().ToArray(); while (!stoppingToken.IsCancellationRequested) { try { var task = UpdateExamResults(parser, degrees); task.Wait(stoppingToken); degrees = task.Result.ToArray(); SaveToFile(degrees); } catch (Exception e) { await HandleError(e); } await Task.Delay(15 * 60 * 1000, stoppingToken); } }
private async Task <IEnumerable <ExamResultChangeTracking> > UpdateExamResults(GradingParser parser, IEnumerable <ExamResultChangeTracking> previousExamResults) { var currentDegrees = await parser.GetGradesForAllDegrees(); var previousExamResultsArr = previousExamResults as ExamResultChangeTracking[] ?? previousExamResults.ToArray(); var changes = GetChangedExams(previousExamResultsArr, currentDegrees.SelectMany(ToExamResultChangeTracking)) .ToArray(); if (changes.Length > 0) { _logger.LogInformation($"Found {changes.Length} changes in exam results."); await Notify(changes); } else { _logger.LogDebug("Found no new exams"); } return(changes.Union(previousExamResultsArr, new GenericEqualityComparer <ExamResultChangeTracking>((a, b) => a.ExamResult.ExamNumber == b.ExamResult.ExamNumber && a.ExamResult.Try == b.ExamResult.Try && a.ExamResult.ExamState == b.ExamResult.ExamState && (float.IsNaN(a.ExamResult.Grade) && float.IsNaN(b.ExamResult.Grade) || Math.Abs(a.ExamResult.Grade - b.ExamResult.Grade) < .001)))); }