diff --git a/src/NadekoBot/Modules/Searches/Searches.cs b/src/NadekoBot/Modules/Searches/Searches.cs index 90ba546c5..abde7d1e0 100644 --- a/src/NadekoBot/Modules/Searches/Searches.cs +++ b/src/NadekoBot/Modules/Searches/Searches.cs @@ -14,7 +14,6 @@ namespace NadekoBot.Modules.Searches; public partial class Searches : NadekoModule { - 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 : NadekoModule 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 : NadekoModule 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() @@ -221,6 +198,7 @@ public partial class Searches : NadekoModule .SendAsync(); } + [Cmd] public async Task MagicTheGathering([Leftover] string search) { @@ -325,15 +303,15 @@ public partial class Searches : NadekoModule if (!await ValidateQuery(word)) return; - + var maybeItems = await _service.GetDefinitionsAsync(word); - - if(!maybeItems.TryPickT0(out var defs, out var error)) + + if (!maybeItems.TryPickT0(out var defs, out var error)) { await HandleErrorAsync(error); return; } - + await Response() .Paginated() .Items(defs) @@ -487,10 +465,4 @@ public partial class Searches : NadekoModule 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/NadekoBot/Modules/Searches/SearchesService.cs b/src/NadekoBot/Modules/Searches/SearchesService.cs index 73231d64c..c0f34fa34 100644 --- a/src/NadekoBot/Modules/Searches/SearchesService.cs +++ b/src/NadekoBot/Modules/Searches/SearchesService.cs @@ -2,8 +2,8 @@ using NadekoBot.Modules.Searches.Common; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using System.Text.Json; using OneOf; +using System.Text.Json; namespace NadekoBot.Modules.Searches.Services; @@ -30,6 +30,7 @@ public class SearchesService : INService private readonly object _yomamaLock = new(); private int yomamaJokeIndex; + private readonly ConcurrentDictionary _cachedShortenedLinks = new(); public SearchesService( IGoogleApiService google, @@ -579,4 +580,42 @@ public class SearchesService : INService 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/NadekoBot/Modules/Searches/_common/ShortenData.cs b/src/NadekoBot/Modules/Searches/_common/ShortenData.cs new file mode 100644 index 000000000..7de6f52ad --- /dev/null +++ b/src/NadekoBot/Modules/Searches/_common/ShortenData.cs @@ -0,0 +1,10 @@ +#nullable disable +using Newtonsoft.Json; + +namespace NadekoBot.Modules.Searches.Services; + +public class ShortenData +{ + [JsonProperty("result_url")] + public string ResultUrl { get; set; } +} \ No newline at end of file