Skip to content

Commit

Permalink
Merge pull request #27 from litolax/saturday-fix
Browse files Browse the repository at this point in the history
Saturday fix
  • Loading branch information
litolax authored Oct 10, 2023
2 parents 1b5162b + dd8cc18 commit f65605c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
43 changes: 41 additions & 2 deletions TeachersTimetable/Services/ParseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class ParseService : IParseService
private readonly IBotService _botService;
private readonly IFirefoxService _firefoxService;
private readonly IDistributionService _distributionService;
private DateTime?[]? _weekInterval;
private List<string> _thHeaders;

private const string WeekUrl =
"https://mgkct.minskedu.gov.by/персоналии/преподавателям/расписание-занятий-на-неделю";
Expand Down Expand Up @@ -92,7 +94,25 @@ private async Task ParseDay()
var teacher = string.Empty;
try
{
if (teachersAndLessons.Count > 0) day = teachersAndLessons[0].Text.Split('-')[1].Trim();
if (teachersAndLessons.Count > 0)
{
day = teachersAndLessons[0].Text.Split('-')[1].Trim();
var tempDay =
_thHeaders.FirstOrDefault(th =>
th.Contains(day, StringComparison.InvariantCultureIgnoreCase)) ??
day;
var daytime = Utils.ParseDateTime(tempDay.Split(", ")[1].Trim());
if (daytime?.DayOfWeek is DayOfWeek.Saturday && !Utils.IsDateBelongsToInterval(daytime, _weekInterval))
{
Console.WriteLine("End parse day(next saturday)");
await this._botService.SendAdminMessageAsync(new SendMessageArgs(0,
"Detected next Saturday!" + tempDay));
return;
}

day = tempDay;
}

for (var i = 1; i < teachersAndLessons.Count; i += 2)
{
var parsedTeacherName = teachersAndLessons[i - 1].Text.Split('-')[0].Trim();
Expand Down Expand Up @@ -218,7 +238,7 @@ private async Task ParseDay()
}
this._botService.SendAdminMessageAsync(new SendMessageArgs(0,
$"{notificationUsersList.Count} notifications sent"));
$"{day}:{notificationUsersList.Count} notifications sent"));
}
catch (Exception e)
{
Expand Down Expand Up @@ -251,6 +271,25 @@ private async Task ParseWeek()
var h3 =
driver.FindElements(
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])
{
_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)
{
_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 Down
27 changes: 26 additions & 1 deletion TeachersTimetable/Services/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using System.Globalization;
using System.Text.RegularExpressions;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using TeachersTimetable.Models;
Expand Down Expand Up @@ -75,4 +76,28 @@ public static void ShowTeacherElements(FirefoxDriver driver, IEnumerable<IWebEle
driver.ExecuteScript("arguments[0].style='display: block;'", element);
}
}

public static DateTime?[]? ParseDateTimeWeekInterval(string interval)
{
var weekInterval = new DateTime?[2];
var days = interval.Split('-');
if (days.Length != 2) return null;
for (var i = 0; i < days.Length; i++)
{
weekInterval[i] = ParseDateTime(days[i]);
if (weekInterval[i] is null) return null;
}

return weekInterval;
}

public static bool IsDateBelongsToInterval(DateTime? date, DateTime?[]? interval) => date is not null &&
interval is not null && date.Value.Date >= interval?[0]?.Date && date.Value.Date <= interval[1]?.Date;

public static DateTime? ParseDateTime(string? date, string? format = "dd.MM.yyyy")
{
if (date is not null && DateTime.TryParseExact(date.Trim(), format, CultureInfo.InvariantCulture,
DateTimeStyles.None, out var dayTime)) return dayTime;
return null;
}
}

0 comments on commit f65605c

Please sign in to comment.