diff --git a/TeachersTimetable/Models/Timetable.cs b/TeachersTimetable/Models/Timetable.cs index f8f1045..09f75d6 100644 --- a/TeachersTimetable/Models/Timetable.cs +++ b/TeachersTimetable/Models/Timetable.cs @@ -5,7 +5,6 @@ namespace TeachersTimetable.Models; public class Timetable { - public ObjectId Id { get; set; } public string Date { get; set; } = ""; [BsonIgnore] public List TeacherInfos { get; set; } = new(); } \ No newline at end of file diff --git a/TeachersTimetable/Services/ParseService.cs b/TeachersTimetable/Services/ParseService.cs index 4b440e3..b6a0340 100644 --- a/TeachersTimetable/Services/ParseService.cs +++ b/TeachersTimetable/Services/ParseService.cs @@ -1,4 +1,5 @@ using MongoDB.Driver; +using Newtonsoft.Json; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.Interactions; @@ -35,6 +36,7 @@ public class ParseService : IParseService private const string DayUrl = "https://mgkct.minskedu.gov.by/персоналии/преподавателям/расписание-занятий-на-день"; + private const string StatePath = "last.json"; private const int DriverTimeout = 2000; public string[] Teachers { get; init; } @@ -52,7 +54,7 @@ public ParseService(IMongoService mongoService, IBotService botService, IFirefox this.Teachers = teachers.Entries.Teachers; Console.WriteLine("Teachers: " + teachers.Entries.Teachers.Length); if (!Directory.Exists("./cachedImages")) Directory.CreateDirectory("./cachedImages"); - + LoadState(StatePath); var parseTimer = new Timer(1_000_000) { AutoReset = true, Enabled = true @@ -398,6 +400,7 @@ public async Task UpdateTimetableTick() await this._botService.SendAdminMessageAsync(new SendMessageArgs(0, "End parse day")); } + if (parseWeek || parseDay) await SaveState(StatePath); Console.WriteLine("End update tick"); } catch (Exception e) @@ -405,4 +408,33 @@ public async Task UpdateTimetableTick() Console.WriteLine(e); } } + + private Task SaveState(string filePath) + { + var stateToSave = new + { + WeekInterval = _weekInterval, + ThHeaders = _thHeaders, + LastDayHtmlContent, + LastWeekHtmlContent, + Timetable + }; + + string json = JsonConvert.SerializeObject(stateToSave); + File.WriteAllText(filePath, json); + return Task.CompletedTask; + } + + private void LoadState(string filePath) + { + if (File.Exists(filePath)) + { + var state = JsonConvert.DeserializeObject(File.ReadAllText(filePath)); + _weekInterval = state!.WeekInterval.ToObject(); + _thHeaders = state.ThHeaders.ToObject>(); + LastDayHtmlContent = state.LastDayHtmlContent; + LastWeekHtmlContent = state.LastWeekHtmlContent; + Timetable = state.Timetable.ToObject>(); + } + } } \ No newline at end of file