From 8f181eed85a5edb4e7a2dccfe5d6e5214b589e61 Mon Sep 17 00:00:00 2001 From: Kwoth Date: Wed, 31 Jul 2024 10:55:34 +0000 Subject: [PATCH] change: .wikia slightly changed and refactored --- src/NadekoBot/Modules/Searches/Searches.cs | 35 +++-------- .../Modules/Searches/SearchesService.cs | 60 ++++++++++++++----- .../Modules/Searches/_common/ErrorType.cs | 9 +++ .../Modules/Searches/_common/WikiaResponse.cs | 7 +++ .../Searches/_common/WikipediaReply.cs | 11 ++++ 5 files changed, 80 insertions(+), 42 deletions(-) create mode 100644 src/NadekoBot/Modules/Searches/_common/ErrorType.cs create mode 100644 src/NadekoBot/Modules/Searches/_common/WikiaResponse.cs create mode 100644 src/NadekoBot/Modules/Searches/_common/WikipediaReply.cs diff --git a/src/NadekoBot/Modules/Searches/Searches.cs b/src/NadekoBot/Modules/Searches/Searches.cs index bedcb05ad..ec719ab9b 100644 --- a/src/NadekoBot/Modules/Searches/Searches.cs +++ b/src/NadekoBot/Modules/Searches/Searches.cs @@ -404,7 +404,7 @@ public partial class Searches : NadekoModule await HandleErrorAsync(error); return; } - + await Response().Confirm("🐈" + GetText(strs.catfact), fact).SendAsync(); } @@ -487,35 +487,16 @@ public partial class Searches : NadekoModule 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] diff --git a/src/NadekoBot/Modules/Searches/SearchesService.cs b/src/NadekoBot/Modules/Searches/SearchesService.cs index 7dd6c1ca5..cae6f6089 100644 --- a/src/NadekoBot/Modules/Searches/SearchesService.cs +++ b/src/NadekoBot/Modules/Searches/SearchesService.cs @@ -468,22 +468,52 @@ public class SearchesService : INService return factElement.ToString(); } -} -public enum ErrorType -{ - InvalidInput, - NotFound, - Unknown, - ApiKeyMissing -} - -public class WikipediaReply -{ - public class Info + public async Task> 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; + } + } } \ No newline at end of file diff --git a/src/NadekoBot/Modules/Searches/_common/ErrorType.cs b/src/NadekoBot/Modules/Searches/_common/ErrorType.cs new file mode 100644 index 000000000..deeb50cbe --- /dev/null +++ b/src/NadekoBot/Modules/Searches/_common/ErrorType.cs @@ -0,0 +1,9 @@ +namespace NadekoBot.Modules.Searches.Services; + +public enum ErrorType +{ + InvalidInput, + NotFound, + Unknown, + ApiKeyMissing +} \ No newline at end of file diff --git a/src/NadekoBot/Modules/Searches/_common/WikiaResponse.cs b/src/NadekoBot/Modules/Searches/_common/WikiaResponse.cs new file mode 100644 index 000000000..4c80be7fd --- /dev/null +++ b/src/NadekoBot/Modules/Searches/_common/WikiaResponse.cs @@ -0,0 +1,7 @@ +namespace NadekoBot.Modules.Searches.Services; + +public sealed class WikiaResponse +{ + public required string Url { get; init; } + public required string Title { get; init; } +} \ No newline at end of file diff --git a/src/NadekoBot/Modules/Searches/_common/WikipediaReply.cs b/src/NadekoBot/Modules/Searches/_common/WikipediaReply.cs new file mode 100644 index 000000000..a398b4706 --- /dev/null +++ b/src/NadekoBot/Modules/Searches/_common/WikipediaReply.cs @@ -0,0 +1,11 @@ +namespace NadekoBot.Modules.Searches.Services; + +public class WikipediaReply +{ + public class Info + { + public required string Url { get; init; } + } + + public required Info Data { get; init; } +} \ No newline at end of file