From 66560bb769439aeabe2958eda5f8c1ed40d4d9b2 Mon Sep 17 00:00:00 2001 From: Toastie Date: Tue, 6 Aug 2024 00:29:40 +1200 Subject: [PATCH] lmgtfy should now be properly shortened, small refactor of 'shorten command --- src/EllieBot/Modules/Searches/Searches.cs | 43 +++---------------- .../Modules/Searches/SearchesService.cs | 41 +++++++++++++++++- .../Modules/Searches/_common/ShortenData.cs | 10 +++++ 3 files changed, 57 insertions(+), 37 deletions(-) create mode 100644 src/EllieBot/Modules/Searches/_common/ShortenData.cs diff --git a/src/EllieBot/Modules/Searches/Searches.cs b/src/EllieBot/Modules/Searches/Searches.cs index 845e2ad..14b7e79 100644 --- a/src/EllieBot/Modules/Searches/Searches.cs +++ b/src/EllieBot/Modules/Searches/Searches.cs @@ -14,7 +14,6 @@ namespace EllieBot.Modules.Searches; public partial class Searches : EllieModule { - private static readonly ConcurrentDictionary _cachedShortenedLinks = new(); private readonly IBotCredentials _creds; private readonly IGoogleApiService _google; private readonly IHttpClientFactory _httpFactory; @@ -172,7 +171,8 @@ public partial class Searches : EllieModule if (!await ValidateQuery(smh)) return; - var shortenedUrl = await _google.ShortenUrl($"https://letmegooglethat.com/?q={Uri.EscapeDataString(smh)}"); + var link = $"https://letmegooglethat.com/?q={Uri.EscapeDataString(smh)}"; + var shortenedUrl = await _service.ShortenLink(link) ?? link; await Response().Confirm($"<{shortenedUrl}>").SendAsync(); } @@ -182,35 +182,12 @@ public partial class Searches : EllieModule if (!await ValidateQuery(query)) return; - query = query.Trim(); - if (!_cachedShortenedLinks.TryGetValue(query, out var shortLink)) + var shortLink = await _service.ShortenLink(query); + + if (shortLink is null) { - try - { - using var http = _httpFactory.CreateClient(); - using var req = new HttpRequestMessage(HttpMethod.Post, "https://goolnk.com/api/v1/shorten"); - var formData = new MultipartFormDataContent - { - { new StringContent(query), "url" } - }; - req.Content = formData; - - using var res = await http.SendAsync(req); - var content = await res.Content.ReadAsStringAsync(); - var data = JsonConvert.DeserializeObject(content); - - if (!string.IsNullOrWhiteSpace(data?.ResultUrl)) - _cachedShortenedLinks.TryAdd(query, data.ResultUrl); - else - return; - - shortLink = data.ResultUrl; - } - catch (Exception ex) - { - Log.Error(ex, "Error shortening a link: {Message}", ex.Message); - return; - } + await Response().Error(strs.error_occured).SendAsync(); + return; } await Response() @@ -487,10 +464,4 @@ public partial class Searches : EllieModule await Response().Error(strs.specify_search_params).SendAsync(); return false; } - - public class ShortenData - { - [JsonProperty("result_url")] - public string ResultUrl { get; set; } - } } \ No newline at end of file diff --git a/src/EllieBot/Modules/Searches/SearchesService.cs b/src/EllieBot/Modules/Searches/SearchesService.cs index 3f01fc1..b8dd2e5 100644 --- a/src/EllieBot/Modules/Searches/SearchesService.cs +++ b/src/EllieBot/Modules/Searches/SearchesService.cs @@ -2,8 +2,8 @@ using EllieBot.Modules.Searches.Common; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using System.Text.Json; using OneOf; +using System.Text.Json; namespace EllieBot.Modules.Searches.Services; @@ -30,6 +30,7 @@ public class SearchesService : IEService private readonly object _yomamaLock = new(); private int yomamaJokeIndex; + private readonly ConcurrentDictionary _cachedShortenedLinks = new(); public SearchesService( IGoogleApiService google, @@ -578,4 +579,42 @@ public class SearchesService : IEService return ErrorType.Unknown; } } + + public async Task ShortenLink(string query) + { + query = query.Trim(); + + if (_cachedShortenedLinks.TryGetValue(query, out var shortLink)) + return shortLink; + + try + { + using var http = _httpFactory.CreateClient(); + using var req = new HttpRequestMessage(HttpMethod.Post, "https://goolnk.com/api/v1/shorten"); + var formData = new MultipartFormDataContent + { + { new StringContent(query), "url" } + }; + req.Content = formData; + + using var res = await http.SendAsync(req); + var content = await res.Content.ReadAsStringAsync(); + var data = JsonConvert.DeserializeObject(content); + + if (!string.IsNullOrWhiteSpace(data?.ResultUrl)) + _cachedShortenedLinks.TryAdd(query, data.ResultUrl); + else + return query; + + shortLink = data.ResultUrl; + } + catch (Exception ex) + { + Log.Error(ex, "Error shortening a link: {Message}", ex.Message); + return null; + } + + return shortLink; + throw new NotImplementedException(); + } } \ No newline at end of file diff --git a/src/EllieBot/Modules/Searches/_common/ShortenData.cs b/src/EllieBot/Modules/Searches/_common/ShortenData.cs new file mode 100644 index 0000000..b08435c --- /dev/null +++ b/src/EllieBot/Modules/Searches/_common/ShortenData.cs @@ -0,0 +1,10 @@ +#nullable disable +using Newtonsoft.Json; + +namespace EllieBot.Modules.Searches.Services; + +public class ShortenData +{ + [JsonProperty("result_url")] + public string ResultUrl { get; set; } +} \ No newline at end of file