Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ostorc committed Dec 19, 2022
1 parent b54ca63 commit ffe227e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 14 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5" />
<PackageVersion Include="Shouldly" Version="4.1.0" />
<PackageVersion Include="Html2Markdown" Version="5.1.0.703" />
<PackageVersion Include="HtmlAgilityPack" Version="1.11.46" />
</ItemGroup>
</Project>
53 changes: 53 additions & 0 deletions src/HonzaBotner.Discord.Services/Jobs/SuzJobProvider.cs
Original file line number Diff line number Diff line change
@@ -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<SuzJobProvider> _logger;

private readonly DiscordWrapper _discord;
private readonly ICanteenService _canteenService;
private readonly IGuildProvider _guildProvider;
private readonly CommonCommandOptions _commonOptions;

public SuzJobProvider(
ILogger<SuzJobProvider> logger,
DiscordWrapper discord,
ICanteenService canteenService,
IGuildProvider guildProvider,
IOptions<CommonCommandOptions> commonOptions)
{
_logger = logger;
_discord = discord;
_canteenService = canteenService;
_guildProvider = guildProvider;
_commonOptions = commonOptions.Value;
}

public string Name => "suz-agata";

public async Task ExecuteAsync(CancellationToken cancellationToken)
{
IList<CanteenDto> canteens = await _canteenService.ListCanteensAsync(true, cancellationToken);

DiscordGuild guild = await _guildProvider.GetCurrentGuildAsync();
guild.ListActiveThreadsAsync()
_discord.Client.SendMessageAsync()
}
}
42 changes: 28 additions & 14 deletions src/HonzaBotner.Services/SuzCanteenService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IList<CanteenDto>> 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<CanteenDto> 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();
Expand Down
3 changes: 3 additions & 0 deletions src/HonzaBotner/appsettings.CvutFit.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit ffe227e

Please sign in to comment.