Skip to content

Commit

Permalink
Fix primbon
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Dec 27, 2023
1 parent 3b6ebdd commit 0a73f85
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 13 deletions.
11 changes: 9 additions & 2 deletions BotNet.Services/BotCommands/Primbon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PrimbonScraper>().GetTaliwangkeAsync(
date: date,
cancellationToken: cancellationToken
);
(string title, string[] traits) = await serviceProvider.GetRequiredService<PrimbonScraper>().GetKamarokamAsync(
date: date,
cancellationToken: cancellationToken
Expand All @@ -61,11 +65,14 @@ string[] inauspiciousActivities
await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: $$"""
<b>{{date:dd MMMM yyyy}}</b>
<b>{{javaneseDate}}</b>

<b>Pentung Kamarokam</b>
<b>Petung Hari Baik</b>
{{title}}: {{string.Join(", ", traits)}}

<b>Hari Larangan</b>
{{sangar}}{{restriction}}

<b>Chinese Calendar</b>
Clash: {{clash}} Evil: {{evil}}
God of Joy: {{godOfJoy}}
Expand Down
73 changes: 62 additions & 11 deletions BotNet.Services/Primbon/PrimbonScraper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -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<KeyValuePair<string, string>>() {
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();

Expand All @@ -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<KeyValuePair<string, string>>() {
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<IHtmlElement>("div#body");
IHtmlElement? iElement = document.QuerySelector<IHtmlElement>("div#body > i");
IHtmlElement? bElement = document.QuerySelector<IHtmlElement>("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("<br>") is int index1 and not -1) {
body = body[(index1 + 4)..];
if (body.IndexOf("<br>") is int index2 and not -1) {
body = body[..index2];
}
}

if (desc.IndexOf("</b>") is int index3 and not -1) {
desc = desc[(index3 + 4)..];
}

return (
JavaneseDate: body.Trim(),
Title: title.Trim(),
Description: desc.Trim()
);
}
}
}

0 comments on commit 0a73f85

Please sign in to comment.