Skip to content

Commit

Permalink
Maybe fix week notification
Browse files Browse the repository at this point in the history
  • Loading branch information
wh0o7 committed Nov 8, 2023
1 parent de694af commit ceff32d
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions TeachersTimetable/Services/ParseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ParseService : IParseService

private const string StatePath = "last.json";
private const int DriverTimeout = 2000;

private bool IsNewInterval;
public string[] Teachers { get; init; }
public static List<Timetable> Timetable { get; set; } = new();
private static string LastDayHtmlContent { get; set; }
Expand Down Expand Up @@ -256,12 +256,10 @@ await this._botService.SendAdminMessageAsync(new SendMessageArgs(0,
private async Task ParseWeek()
{
Console.WriteLine("Start week parse");

var (service, options, delay) = this._firefoxService.Create();
using FirefoxDriver driver = new FirefoxDriver(service, options, delay);
driver.Manage().Timeouts().PageLoad.Add(TimeSpan.FromMinutes(2));
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
bool isNewInterval = false;
driver.Navigate().GoToUrl(WeekUrl);
var element = driver.FindElement(By.XPath("/html/body/div[1]/div[2]/div/div[2]/div[1]/div"));
wait.Until(d => element.Displayed);
Expand All @@ -275,26 +273,28 @@ private async Task ParseWeek()
By.XPath("/html/body/div[1]/div[2]/div/div[2]/div[1]/div/h3"));
var weekIntervalStr = h3[0].Text;
var weekInterval = Utils.ParseDateTimeWeekInterval(weekIntervalStr);
if (_weekInterval is null || !string.IsNullOrEmpty(weekIntervalStr) && _weekInterval != weekInterval &&
_weekInterval[1] is not null && DateTime.Today == _weekInterval[1])
{
isNewInterval = _weekInterval is not null;
_weekInterval = weekInterval;
Console.WriteLine("New interval is " + weekIntervalStr);
await this._botService.SendAdminMessageAsync(new SendMessageArgs(0,
"New interval is " + weekIntervalStr));
}

var tempThHeaders =
driver.FindElement(
By.XPath("/html/body/div[1]/div[2]/div/div[2]/div[1]/div/div[1]/table/tbody/tr[1]"))
.FindElements(By.TagName("th"));
_thHeaders = new List<string>();
foreach (var thHeader in tempThHeaders)
var isIsNewInterval = IsNewInterval;
if (_weekInterval is null || !string.IsNullOrEmpty(weekIntervalStr) && _weekInterval != weekInterval)
{
_thHeaders.Add(new string(thHeader.Text));
IsNewInterval = _weekInterval is not null;
if (_weekInterval is null || _weekInterval[1] is not null && DateTime.Today == _weekInterval[1])
{
IsNewInterval = false;
_weekInterval = weekInterval;
Console.WriteLine("New interval is " + weekIntervalStr);
await this._botService.SendAdminMessageAsync(new SendMessageArgs(0,
"New interval is " + weekIntervalStr));
}
else IsNewInterval = true;
isIsNewInterval = !isIsNewInterval && IsNewInterval;
var tempThHeaders =
driver.FindElement(
By.XPath("/html/body/div[1]/div[2]/div/div[2]/div[1]/div/div[1]/table/tbody/tr[1]"))
.FindElements(By.TagName("th"));
_thHeaders = new List<string>();
foreach (var thHeader in tempThHeaders) _thHeaders.Add(new string(thHeader.Text));
}

var table = driver.FindElements(By.XPath("/html/body/div[1]/div[2]/div/div[2]/div[1]/div/div"));
Utils.HideTeacherElements(driver, h3);
Utils.HideTeacherElements(driver, h2);
Expand All @@ -319,7 +319,7 @@ await this._botService.SendAdminMessageAsync(new SendMessageArgs(0,
var image = Image.Load(screenshot.AsByteArray);
image.Mutate(x => x.Resize((int)(image.Width / 1.5), (int)(image.Height / 1.5)));
await image.SaveAsync($"./cachedImages/{teacher}.png");
if (isNewInterval)
if (IsNewInterval)
foreach (var notificationUser in (await this._mongoService.Database.GetCollection<User>("Users")
.FindAsync(u => u.Teachers != null && u.Notifications)).ToList())
notificationUserHashSet.Add(notificationUser);
Expand All @@ -336,7 +336,7 @@ await this._botService.SendAdminMessageAsync(new SendMessageArgs(0,
}
}

if (isNewInterval)
if (isIsNewInterval)
_ = Task.Run(() =>
{
foreach (var user in notificationUserHashSet)
Expand Down Expand Up @@ -408,7 +408,7 @@ public async Task UpdateTimetableTick()
Console.WriteLine(e);
}
}

private void SaveState(string filePath)
{
var stateToSave = new
Expand Down

0 comments on commit ceff32d

Please sign in to comment.