From 0a73f85956e16c0d429c566e711a6828b884b3e9 Mon Sep 17 00:00:00 2001 From: Ronny Gunawan <3048897+ronnygunawan@users.noreply.github.com> Date: Wed, 27 Dec 2023 22:30:22 +0700 Subject: [PATCH] Fix primbon --- BotNet.Services/BotCommands/Primbon.cs | 11 +++- BotNet.Services/Primbon/PrimbonScraper.cs | 73 +++++++++++++++++++---- 2 files changed, 71 insertions(+), 13 deletions(-) diff --git a/BotNet.Services/BotCommands/Primbon.cs b/BotNet.Services/BotCommands/Primbon.cs index 8938a1f..d9e2db2 100644 --- a/BotNet.Services/BotCommands/Primbon.cs +++ b/BotNet.Services/BotCommands/Primbon.cs @@ -41,6 +41,10 @@ await botClient.SendTextMessageAsync( try { RATE_LIMITER.ValidateActionRate(message.Chat.Id, message.From!.Id); + (string javaneseDate, string sangar, string restriction) = await serviceProvider.GetRequiredService().GetTaliwangkeAsync( + date: date, + cancellationToken: cancellationToken + ); (string title, string[] traits) = await serviceProvider.GetRequiredService().GetKamarokamAsync( date: date, cancellationToken: cancellationToken @@ -61,11 +65,14 @@ string[] inauspiciousActivities await botClient.SendTextMessageAsync( chatId: message.Chat.Id, text: $$""" - {{date:dd MMMM yyyy}} + {{javaneseDate}} - Pentung Kamarokam + Petung Hari Baik {{title}}: {{string.Join(", ", traits)}} + Hari Larangan + {{sangar}}{{restriction}} + Chinese Calendar Clash: {{clash}} Evil: {{evil}} God of Joy: {{godOfJoy}} diff --git a/BotNet.Services/Primbon/PrimbonScraper.cs b/BotNet.Services/Primbon/PrimbonScraper.cs index 6849f67..040814a 100644 --- a/BotNet.Services/Primbon/PrimbonScraper.cs +++ b/BotNet.Services/Primbon/PrimbonScraper.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Net.Http; using System.Threading; using System.Threading.Tasks; @@ -7,22 +8,24 @@ using AngleSharp.Html.Dom; namespace BotNet.Services.Primbon { - public class PrimbonScraper(HttpClient httpClient) { + public class PrimbonScraper( + HttpClient httpClient + ) { private const string KAMAROKAM_URL = "https://www.primbon.com/petung_hari_baik.php"; + private const string TALIWANGKE_URL = "https://primbon.com/hari_sangar_taliwangke.php"; private readonly HttpClient _httpClient = httpClient; public async Task<(string Title, string[] Traits)> GetKamarokamAsync(DateOnly date, CancellationToken cancellationToken) { using HttpRequestMessage httpRequest = new(HttpMethod.Post, KAMAROKAM_URL); - using StringContent tgl = new(date.ToString("d")); - using StringContent bln = new(date.ToString("M")); - using StringContent thn = new(date.ToString("yyyy")); - using StringContent submit = new("Submit!"); - httpRequest.Content = new MultipartFormDataContent { - { tgl, "tgl" }, - { bln, "bln" }, - { thn, "thn" }, - { submit, "submit" } - }; + using FormUrlEncodedContent content = new( + nameValueCollection: new List>() { + new("tgl", date.Day.ToString()), + new("bln", date.Month.ToString()), + new("thn", date.Year.ToString()), + new("submit", " Submit! ") + } + ); + httpRequest.Content = content; using HttpResponseMessage httpResponse = await _httpClient.SendAsync(httpRequest, cancellationToken); httpResponse.EnsureSuccessStatusCode(); @@ -49,5 +52,53 @@ public class PrimbonScraper(HttpClient httpClient) { Traits: traits.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) ); } + + public async Task<(string JavaneseDate, string Title, string Description)> GetTaliwangkeAsync(DateOnly date, CancellationToken cancellationToken) { + using HttpRequestMessage httpRequest = new(HttpMethod.Post, TALIWANGKE_URL); + using FormUrlEncodedContent content = new( + nameValueCollection: new List>() { + new("tgl", date.Day.ToString()), + new("bln", date.Month.ToString()), + new("thn", date.Year.ToString()), + new("kirim", " Submit! ") + } + ); + httpRequest.Content = content; + using HttpResponseMessage httpResponse = await _httpClient.SendAsync(httpRequest, cancellationToken); + httpResponse.EnsureSuccessStatusCode(); + + string html = await httpResponse.Content.ReadAsStringAsync(cancellationToken); + + IBrowsingContext browsingContext = BrowsingContext.New(Configuration.Default); + IDocument document = await browsingContext.OpenAsync(req => req.Content(html), cancellationToken); + IHtmlElement? bodyDiv = document.QuerySelector("div#body"); + IHtmlElement? iElement = document.QuerySelector("div#body > i"); + IHtmlElement? bElement = document.QuerySelector("div#body > i > b"); + + string? body = bodyDiv?.InnerHtml; + string? title = bElement?.InnerHtml; + string? desc = iElement?.InnerHtml; + + if (body is null || title is null || desc is null) { + throw new InvalidOperationException("Primbon.com returned an unexpected response."); + } + + if (body.IndexOf("
") is int index1 and not -1) { + body = body[(index1 + 4)..]; + if (body.IndexOf("
") is int index2 and not -1) { + body = body[..index2]; + } + } + + if (desc.IndexOf("") is int index3 and not -1) { + desc = desc[(index3 + 4)..]; + } + + return ( + JavaneseDate: body.Trim(), + Title: title.Trim(), + Description: desc.Trim() + ); + } } }