.wikia slightly changed and refactored

This commit is contained in:
Toastie (DCS Team) 2024-08-01 01:03:08 +12:00
parent 219c122f1c
commit fe5c8622dd
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
5 changed files with 80 additions and 41 deletions

View file

@ -487,35 +487,16 @@ public partial class Searches : EllieModule<SearchesService>
return;
}
await ctx.Channel.TriggerTypingAsync();
using var http = _httpFactory.CreateClient();
http.DefaultRequestHeaders.Clear();
try
{
var res = await http.GetStringAsync($"https://{Uri.EscapeDataString(target)}.fandom.com/api.php"
+ "?action=query"
+ "&format=json"
+ "&list=search"
+ $"&srsearch={Uri.EscapeDataString(query)}"
+ "&srlimit=1");
var items = JObject.Parse(res);
var title = items["query"]?["search"]?.FirstOrDefault()?["title"]?.ToString();
var maybeRes = await _service.GetWikiaPageAsync(target, query);
if (string.IsNullOrWhiteSpace(title))
{
await Response().Error(strs.wikia_error).SendAsync();
return;
}
var url = Uri.EscapeDataString($"https://{target}.fandom.com/wiki/{title}");
var response = $@"`{GetText(strs.title)}` {title.SanitizeMentions()}
`{GetText(strs.url)}:` {url}";
await Response().Text(response).SendAsync();
}
catch
if (!maybeRes.TryPickT0(out var res, out var error))
{
await Response().Error(strs.wikia_error).SendAsync();
await HandleErrorAsync(error);
return;
}
var response = $"### {res.Title}\n{res.Url}";
await Response().Text(response).Sanitize().SendAsync();
}
[Cmd]

View file

@ -460,6 +460,7 @@ public class SearchesService : IEService
var doc = JsonDocument.Parse(response);
if (!doc.RootElement.TryGetProperty("fact", out var factElement))
{
return ErrorType.Unknown;
@ -467,22 +468,52 @@ public class SearchesService : IEService
return factElement.ToString();
}
}
public enum ErrorType
{
InvalidInput,
NotFound,
Unknown,
ApiKeyMissing
}
public class WikipediaReply
{
public class Info
public async Task<OneOf<WikiaResponse, ErrorType>> GetWikiaPageAsync(string target, string query)
{
public required string Url { get; init; }
}
if (string.IsNullOrWhiteSpace(target) || string.IsNullOrWhiteSpace(query))
{
return ErrorType.InvalidInput;
}
public required Info Data { get; init; }
query = Uri.EscapeDataString(query.Trim());
target = Uri.EscapeDataString(target.Trim());
if (string.IsNullOrEmpty(query))
{
return ErrorType.InvalidInput;
}
using var http = _httpFactory.CreateClient();
http.DefaultRequestHeaders.Clear();
try
{
var res = await http.GetStringAsync($"https://{Uri.EscapeDataString(target)}.fandom.com/api.php"
+ "?action=query"
+ "&format=json"
+ "&list=search"
+ $"&srsearch={Uri.EscapeDataString(query)}"
+ "&srlimit=1");
var items = JObject.Parse(res);
var title = items["query"]?["search"]?.FirstOrDefault()?["title"]?.ToString();
if (string.IsNullOrWhiteSpace(title))
{
return ErrorType.NotFound;
}
var url = $"https://{target}.fandom.com/wiki/{title}";
return new WikiaResponse()
{
Url = url,
Title = title,
};
}
catch (Exception ex)
{
Log.Warning(ex, "Error getting wikia page: {Message}", ex.Message);
return ErrorType.Unknown;
}
}
}

View file

@ -0,0 +1,9 @@
namespace EllieBot.Modules.Searches.Services;
public enum ErrorType
{
InvalidInput,
NotFound,
Unknown,
ApiKeyMissing
}

View file

@ -0,0 +1,7 @@
namespace EllieBot.Modules.Searches.Services;
public sealed class WikiaResponse
{
public required string Url { get; init; }
public required string Title { get; init; }
}

View file

@ -0,0 +1,11 @@
namespace EllieBot.Modules.Searches.Services;
public class WikipediaReply
{
public class Info
{
public required string Url { get; init; }
}
public required Info Data { get; init; }
}