Skip to content

Commit

Permalink
Merge pull request #43 from litolax/saturday-fix
Browse files Browse the repository at this point in the history
Saturday fix
  • Loading branch information
litolax authored Oct 9, 2023
2 parents c86fcc9 + 2840478 commit cc7779f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
44 changes: 40 additions & 4 deletions StudentsTimetable/Services/ParseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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/персоналии/учащимся/расписание-занятий-на-неделю";
private const string DayUrl = "https://mgkct.minskedu.gov.by/персоналии/учащимся/расписание-занятий-на-день";

Expand All @@ -49,7 +50,6 @@ public ParseService(IMongoService mongoService, IBotService botService, IFirefox
this._distributionService = distributionService;
this.Groups = groups.Entries.Groups;
if (!Directory.Exists("./cachedImages")) Directory.CreateDirectory("./cachedImages");

var parseTimer = new Timer(1_000_000)
{
AutoReset = true, Enabled = true
Expand Down Expand Up @@ -86,7 +86,24 @@ private async Task ParseDay()
if (content is null) return;

var groupsAndLessons = content.FindElements(By.XPath(".//div")).ToList();
if (groupsAndLessons.Count > 0) day = groupsAndLessons[0].Text.Split('-')[1].Trim();
if (groupsAndLessons.Count > 0)
{
day = groupsAndLessons[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;
}

try
{
for (var i = 1; i < groupsAndLessons.Count; i += 2)
Expand Down Expand Up @@ -228,7 +245,7 @@ private async Task ParseDay()
}
this._botService.SendAdminMessageAsync(new SendMessageArgs(0,
$"{notificationUserList.Count} notifications sent"));
$"{day}:{notificationUserList.Count} notifications sent"));
});
}

Expand All @@ -255,6 +272,25 @@ private 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);
this._botService.SendAdminMessage(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.HideGroupElements(driver, h3);
Utils.HideGroupElements(driver, h2);
Expand Down
41 changes: 35 additions & 6 deletions StudentsTimetable/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 StudentsTimetable.Models;
Expand All @@ -23,17 +24,17 @@ public static void ModifyUnnecessaryElementsOnWebsite(FirefoxDriver driver)

var footer = driver.FindElement(By.Id("footer"));
driver.ExecuteScript("arguments[0].style='display: none'", footer);

var breadcrumbs = driver.FindElement(By.ClassName("breadcrumbs"));
driver.ExecuteScript("arguments[0].style='display: none'", breadcrumbs);

var pageShareButtons = driver.FindElement(By.ClassName("page_share_buttons"));
driver.ExecuteScript("arguments[0].style='display: none'", pageShareButtons);

var all = driver.FindElement(By.CssSelector("*"));
driver.ExecuteScript("arguments[0].style='overflow-y: hidden; overflow-x: hidden'", all);


driver.ExecuteScript("arguments[0].style='display : none'", driver.FindElement(By.TagName("h1")));
driver.Manage().Window.Size = new Size(1920, container.Size.Height - 30);
}
Expand Down Expand Up @@ -83,12 +84,40 @@ public static void HideGroupElements(FirefoxDriver driver, IEnumerable<IWebEleme
}
}


public static void ShowGroupElements(FirefoxDriver driver, IEnumerable<IWebElement> elements)
{
foreach (var element in elements)
{
driver.ExecuteScript("arguments[0].style='display: block;'", element);
}
}

/// <summary>
/// Week interval parse method
/// </summary>
/// <param name="interval">String in format "day.month.year - day.month.year"</param>
/// <returns>week interval array, where 0 - start, 1 - end. Or null, if one of two dates is incorrect</returns>
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 cc7779f

Please sign in to comment.