From ffe227e98919f475bfed87f9a605f18e4c8273a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20=C5=A0torc?= Date: Tue, 25 Oct 2022 14:39:14 +0200 Subject: [PATCH] wip --- Directory.Packages.props | 1 + .../Jobs/SuzJobProvider.cs | 53 +++++++++++++++++++ src/HonzaBotner.Services/SuzCanteenService.cs | 42 ++++++++++----- src/HonzaBotner/appsettings.CvutFit.json | 3 ++ 4 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 src/HonzaBotner.Discord.Services/Jobs/SuzJobProvider.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index 9cec753a..57fee1a6 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -27,5 +27,6 @@ + diff --git a/src/HonzaBotner.Discord.Services/Jobs/SuzJobProvider.cs b/src/HonzaBotner.Discord.Services/Jobs/SuzJobProvider.cs new file mode 100644 index 00000000..13a4b6d2 --- /dev/null +++ b/src/HonzaBotner.Discord.Services/Jobs/SuzJobProvider.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text.RegularExpressions; +using System.Threading; +using System.Threading.Tasks; +using DSharpPlus; +using DSharpPlus.Entities; +using HonzaBotner.Discord.Services.Helpers; +using HonzaBotner.Discord.Services.Options; +using HonzaBotner.Scheduler.Contract; +using HonzaBotner.Services.Contract; +using HonzaBotner.Services.Contract.Dto; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace HonzaBotner.Discord.Services.Jobs; + +[Cron("0 30 10 * * 1-5")] +public class SuzJobProvider : IJob +{ + private readonly ILogger _logger; + + private readonly DiscordWrapper _discord; + private readonly ICanteenService _canteenService; + private readonly IGuildProvider _guildProvider; + private readonly CommonCommandOptions _commonOptions; + + public SuzJobProvider( + ILogger logger, + DiscordWrapper discord, + ICanteenService canteenService, + IGuildProvider guildProvider, + IOptions commonOptions) + { + _logger = logger; + _discord = discord; + _canteenService = canteenService; + _guildProvider = guildProvider; + _commonOptions = commonOptions.Value; + } + + public string Name => "suz-agata"; + + public async Task ExecuteAsync(CancellationToken cancellationToken) + { + IList canteens = await _canteenService.ListCanteensAsync(true, cancellationToken); + + DiscordGuild guild = await _guildProvider.GetCurrentGuildAsync(); + guild.ListActiveThreadsAsync() + _discord.Client.SendMessageAsync() + } +} diff --git a/src/HonzaBotner.Services/SuzCanteenService.cs b/src/HonzaBotner.Services/SuzCanteenService.cs index 1e6e3baa..9d1ba0e9 100644 --- a/src/HonzaBotner.Services/SuzCanteenService.cs +++ b/src/HonzaBotner.Services/SuzCanteenService.cs @@ -7,35 +7,49 @@ using HonzaBotner.Services.Contract; using HonzaBotner.Services.Contract.Dto; using HtmlAgilityPack; +using Microsoft.Extensions.Caching.Memory; namespace HonzaBotner.Services; public class SuzCanteenService: ICanteenService { private readonly HttpClient _httpClient; + private readonly IMemoryCache _memoryCache; - public SuzCanteenService(HttpClient httpClient) + public SuzCanteenService(HttpClient httpClient, IMemoryCache memoryCache) { _httpClient = httpClient; + _memoryCache = memoryCache; } public async Task> ListCanteensAsync(bool onlyOpen = false, CancellationToken cancellationToken = default) { - const string url = "https://agata.suz.cvut.cz/jidelnicky/index.php"; - string pageContent = await _httpClient.GetStringAsync(url, cancellationToken); + const string cacheKey = $"{nameof(SuzCanteenService)}_{nameof(ListCanteensAsync)}"; - var htmlDoc = new HtmlDocument(); - htmlDoc.LoadHtml(pageContent); + if (!_memoryCache.TryGetValue(cacheKey, out List canteens)) + { - var canteens = htmlDoc.DocumentNode.SelectNodes("//ul[@id='menzy']/li/a") - .Select(node => - { - int.TryParse(node.Id.Replace("podSh", ""), out int id); - bool open = node.SelectSingleNode($"{node.XPath}/img") - .GetAttributeValue("src", "closed").Contains("Otevreno"); - string name = node.InnerText.Trim(); - return new CanteenDto(id, name, open); - }); + const string url = "https://agata.suz.cvut.cz/jidelnicky/index.php"; + string pageContent = await _httpClient.GetStringAsync(url, cancellationToken); + + var htmlDoc = new HtmlDocument(); + htmlDoc.LoadHtml(pageContent); + + canteens = htmlDoc.DocumentNode.SelectNodes("//ul[@id='menzy']/li/a") + .Select(node => + { + int.TryParse(node.Id.Replace("podSh", ""), out int id); + bool open = node.SelectSingleNode($"{node.XPath}/img") + .GetAttributeValue("src", "closed").Contains("Otevreno"); + string name = node.InnerText.Trim(); + return new CanteenDto(id, name, open); + }).ToList(); + + var cacheEntryOptions = new MemoryCacheEntryOptions() + .SetAbsoluteExpiration(TimeSpan.FromHours(1)); + + _memoryCache.Set(cacheKey, canteens, cacheEntryOptions); + } if (onlyOpen) return canteens.Where(c => c.Open).ToList(); diff --git a/src/HonzaBotner/appsettings.CvutFit.json b/src/HonzaBotner/appsettings.CvutFit.json index d3378490..f2fe52e4 100644 --- a/src/HonzaBotner/appsettings.CvutFit.json +++ b/src/HonzaBotner/appsettings.CvutFit.json @@ -56,6 +56,9 @@ "issueTrackerUrl": "https://github.com/fit-ctu-discord/honza-botner/issues", "changelogUrl": "https://github.com/fit-ctu-discord/honza-botner/releases" }, + "SuzCanteenOptions": { + "PublishThre" + }, "DiscordRoles": { "AuthenticatedRoleIds": [681559148546359432, 686867633085480970], "AuthRoleMapping": {