Skip to content

Commit

Permalink
Merge pull request #55 from litolax/feature/last-state
Browse files Browse the repository at this point in the history
Add last state feature
  • Loading branch information
litolax committed Nov 3, 2023
2 parents 7800757 + 14edd2c commit 14e8a61
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
1 change: 0 additions & 1 deletion StudentsTimetable/Models/Day.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace StudentsTimetable.Models;

public class Day
{
public ObjectId Id { get; set; }
public string Date { get; set; }
[BsonIgnore] public List<GroupInfo> GroupInfos { get; set; } = new();
}
35 changes: 34 additions & 1 deletion StudentsTimetable/Services/ParseService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.RegularExpressions;
using MongoDB.Driver;
using Newtonsoft.Json;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;
Expand Down Expand Up @@ -31,7 +32,7 @@ public class ParseService : IParseService
private List<string> _thHeaders;
private const string WeekUrl = "https://mgkct.minskedu.gov.by/персоналии/учащимся/расписание-занятий-на-неделю";
private const string DayUrl = "https://mgkct.minskedu.gov.by/персоналии/учащимся/расписание-занятий-на-день";

private const string StatePath = "last.json";
private static string LastDayHtmlContent { get; set; }
private static string LastWeekHtmlContent { get; set; }

Expand All @@ -49,6 +50,7 @@ public ParseService(IMongoService mongoService, IBotService botService, IFirefox
this._firefoxService = firefoxService;
this._distributionService = distributionService;
this.Groups = groups.Entries.Groups;
LoadState(StatePath);
if (!Directory.Exists("./cachedImages")) Directory.CreateDirectory("./cachedImages");
var parseTimer = new Timer(1_000_000)
{
Expand Down Expand Up @@ -394,6 +396,8 @@ public async Task UpdateTimetableTick()
await this.ParseDay();
await this._botService.SendAdminMessageAsync(new SendMessageArgs(0, "End parse day"));
}

if (parseWeek || parseDay) await SaveState(StatePath);
}
catch (Exception e)
{
Expand All @@ -402,4 +406,33 @@ public async Task UpdateTimetableTick()

Console.WriteLine("End update tick");
}

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<Day>>();
}
}
}

0 comments on commit 14e8a61

Please sign in to comment.