Skip to content

Commit

Permalink
Merge pull request #36 from litolax/feature/last-state
Browse files Browse the repository at this point in the history
Add last state feature
  • Loading branch information
litolax authored Nov 3, 2023
2 parents 544f449 + 9abec95 commit 32a4c9b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
1 change: 0 additions & 1 deletion TeachersTimetable/Models/Timetable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace TeachersTimetable.Models;

public class Timetable
{
public ObjectId Id { get; set; }
public string Date { get; set; } = "";
[BsonIgnore] public List<TeacherInfo> TeacherInfos { get; set; } = new();
}
34 changes: 33 additions & 1 deletion TeachersTimetable/Services/ParseService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MongoDB.Driver;
using Newtonsoft.Json;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;
Expand Down Expand Up @@ -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; }
Expand All @@ -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
Expand Down Expand Up @@ -398,11 +400,41 @@ 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)
{
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<dynamic>(File.ReadAllText(filePath));
_weekInterval = state!.WeekInterval.ToObject<DateTime?[]>();
_thHeaders = state.ThHeaders.ToObject<List<string>>();
LastDayHtmlContent = state.LastDayHtmlContent;
LastWeekHtmlContent = state.LastWeekHtmlContent;
Timetable = state.Timetable.ToObject<List<Timetable>>();
}
}
}

0 comments on commit 32a4c9b

Please sign in to comment.