lmgtfy should now be properly shortened, small refactor of 'shorten command

This commit is contained in:
Toastie (DCS Team) 2024-08-06 00:29:40 +12:00
parent 7d0ef44e8e
commit 66560bb769
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
3 changed files with 57 additions and 37 deletions

View file

@ -14,7 +14,6 @@ namespace EllieBot.Modules.Searches;
public partial class Searches : EllieModule<SearchesService> public partial class Searches : EllieModule<SearchesService>
{ {
private static readonly ConcurrentDictionary<string, string> _cachedShortenedLinks = new();
private readonly IBotCredentials _creds; private readonly IBotCredentials _creds;
private readonly IGoogleApiService _google; private readonly IGoogleApiService _google;
private readonly IHttpClientFactory _httpFactory; private readonly IHttpClientFactory _httpFactory;
@ -172,7 +171,8 @@ public partial class Searches : EllieModule<SearchesService>
if (!await ValidateQuery(smh)) if (!await ValidateQuery(smh))
return; 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(); await Response().Confirm($"<{shortenedUrl}>").SendAsync();
} }
@ -182,35 +182,12 @@ public partial class Searches : EllieModule<SearchesService>
if (!await ValidateQuery(query)) if (!await ValidateQuery(query))
return; return;
query = query.Trim(); var shortLink = await _service.ShortenLink(query);
if (!_cachedShortenedLinks.TryGetValue(query, out var 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); if (shortLink is null)
var content = await res.Content.ReadAsStringAsync(); {
var data = JsonConvert.DeserializeObject<ShortenData>(content); await Response().Error(strs.error_occured).SendAsync();
if (!string.IsNullOrWhiteSpace(data?.ResultUrl))
_cachedShortenedLinks.TryAdd(query, data.ResultUrl);
else
return; return;
shortLink = data.ResultUrl;
}
catch (Exception ex)
{
Log.Error(ex, "Error shortening a link: {Message}", ex.Message);
return;
}
} }
await Response() await Response()
@ -487,10 +464,4 @@ public partial class Searches : EllieModule<SearchesService>
await Response().Error(strs.specify_search_params).SendAsync(); await Response().Error(strs.specify_search_params).SendAsync();
return false; return false;
} }
public class ShortenData
{
[JsonProperty("result_url")]
public string ResultUrl { get; set; }
}
} }

View file

@ -2,8 +2,8 @@
using EllieBot.Modules.Searches.Common; using EllieBot.Modules.Searches.Common;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System.Text.Json;
using OneOf; using OneOf;
using System.Text.Json;
namespace EllieBot.Modules.Searches.Services; namespace EllieBot.Modules.Searches.Services;
@ -30,6 +30,7 @@ public class SearchesService : IEService
private readonly object _yomamaLock = new(); private readonly object _yomamaLock = new();
private int yomamaJokeIndex; private int yomamaJokeIndex;
private readonly ConcurrentDictionary<string, string> _cachedShortenedLinks = new();
public SearchesService( public SearchesService(
IGoogleApiService google, IGoogleApiService google,
@ -578,4 +579,42 @@ public class SearchesService : IEService
return ErrorType.Unknown; return ErrorType.Unknown;
} }
} }
public async Task<string> 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<ShortenData>(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();
}
} }

View file

@ -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; }
}