From fe5c8622dd028764318e9ed786278913b33d3a5e Mon Sep 17 00:00:00 2001
From: Toastie <toastie@toastiet0ast.com>
Date: Thu, 1 Aug 2024 01:03:08 +1200
Subject: [PATCH] .wikia slightly changed and refactored

---
 src/EllieBot/Modules/Searches/Searches.cs     | 33 +++-------
 .../Modules/Searches/SearchesService.cs       | 61 ++++++++++++++-----
 .../Modules/Searches/_common/ErrorType.cs     |  9 +++
 .../Modules/Searches/_common/WikiaResponse.cs |  7 +++
 .../Searches/_common/WikipediaReply.cs        | 11 ++++
 5 files changed, 80 insertions(+), 41 deletions(-)
 create mode 100644 src/EllieBot/Modules/Searches/_common/ErrorType.cs
 create mode 100644 src/EllieBot/Modules/Searches/_common/WikiaResponse.cs
 create mode 100644 src/EllieBot/Modules/Searches/_common/WikipediaReply.cs

diff --git a/src/EllieBot/Modules/Searches/Searches.cs b/src/EllieBot/Modules/Searches/Searches.cs
index b6a7140..9159ac6 100644
--- a/src/EllieBot/Modules/Searches/Searches.cs
+++ b/src/EllieBot/Modules/Searches/Searches.cs
@@ -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]
diff --git a/src/EllieBot/Modules/Searches/SearchesService.cs b/src/EllieBot/Modules/Searches/SearchesService.cs
index 4ff9358..c336b72 100644
--- a/src/EllieBot/Modules/Searches/SearchesService.cs
+++ b/src/EllieBot/Modules/Searches/SearchesService.cs
@@ -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;
+        }
+    }
 }
\ No newline at end of file
diff --git a/src/EllieBot/Modules/Searches/_common/ErrorType.cs b/src/EllieBot/Modules/Searches/_common/ErrorType.cs
new file mode 100644
index 0000000..0daeea5
--- /dev/null
+++ b/src/EllieBot/Modules/Searches/_common/ErrorType.cs
@@ -0,0 +1,9 @@
+namespace EllieBot.Modules.Searches.Services;
+
+public enum ErrorType
+{
+    InvalidInput,
+    NotFound,
+    Unknown,
+    ApiKeyMissing
+}
\ No newline at end of file
diff --git a/src/EllieBot/Modules/Searches/_common/WikiaResponse.cs b/src/EllieBot/Modules/Searches/_common/WikiaResponse.cs
new file mode 100644
index 0000000..d0b3960
--- /dev/null
+++ b/src/EllieBot/Modules/Searches/_common/WikiaResponse.cs
@@ -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; }
+}
\ No newline at end of file
diff --git a/src/EllieBot/Modules/Searches/_common/WikipediaReply.cs b/src/EllieBot/Modules/Searches/_common/WikipediaReply.cs
new file mode 100644
index 0000000..3969090
--- /dev/null
+++ b/src/EllieBot/Modules/Searches/_common/WikipediaReply.cs
@@ -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; }
+}
\ No newline at end of file