NadekoBot Patronage system, Search commands improvements + fixes

This commit is contained in:
Kwoth
2022-06-14 07:24:33 +00:00
parent 18b10b8c6f
commit 7b5145f116
165 changed files with 14920 additions and 1457 deletions

View File

@@ -0,0 +1,22 @@
using NadekoBot.Modules.Searches;
using System.Text.Json.Serialization;
namespace NadekoBot.Services;
public sealed class GoogleCustomSearchResult : ISearchResult
{
ISearchResultInformation ISearchResult.Info
=> Info;
public string? Answer
=> null;
IReadOnlyCollection<ISearchResultEntry> ISearchResult.Entries
=> Entries ?? Array.Empty<OfficialGoogleSearchResultEntry>();
[JsonPropertyName("searchInformation")]
public GoogleSearchResultInformation Info { get; init; } = null!;
[JsonPropertyName("items")]
public IReadOnlyCollection<OfficialGoogleSearchResultEntry>? Entries { get; init; }
}

View File

@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;
namespace NadekoBot.Services;
public sealed class GoogleImageData
{
[JsonPropertyName("contextLink")]
public string ContextLink { get; init; } = null!;
[JsonPropertyName("thumbnailLink")]
public string ThumbnailLink { get; init; } = null!;
}

View File

@@ -0,0 +1,19 @@
using NadekoBot.Modules.Searches;
using System.Text.Json.Serialization;
namespace NadekoBot.Services;
public sealed class GoogleImageResult : IImageSearchResult
{
ISearchResultInformation IImageSearchResult.Info
=> Info;
IReadOnlyCollection<IImageSearchResultEntry> IImageSearchResult.Entries
=> Entries ?? Array.Empty<GoogleImageResultEntry>();
[JsonPropertyName("searchInformation")]
public GoogleSearchResultInformation Info { get; init; } = null!;
[JsonPropertyName("items")]
public IReadOnlyCollection<GoogleImageResultEntry>? Entries { get; init; }
}

View File

@@ -0,0 +1,13 @@
using NadekoBot.Modules.Searches;
using System.Text.Json.Serialization;
namespace NadekoBot.Services;
public sealed class GoogleImageResultEntry : IImageSearchResultEntry
{
[JsonPropertyName("link")]
public string Link { get; init; } = null!;
[JsonPropertyName("image")]
public GoogleImageData Image { get; init; } = null!;
}

View File

@@ -0,0 +1,13 @@
using NadekoBot.Modules.Searches;
using System.Text.Json.Serialization;
namespace NadekoBot.Services;
public sealed class GoogleSearchResultInformation : ISearchResultInformation
{
[JsonPropertyName("formattedTotalResults")]
public string TotalResults { get; init; } = null!;
[JsonPropertyName("formattedSearchTime")]
public string SearchTime { get; init; } = null!;
}

View File

@@ -0,0 +1,66 @@
using MorseCode.ITask;
namespace NadekoBot.Modules.Searches;
public sealed class GoogleSearchService : SearchServiceBase, INService
{
private readonly IBotCredsProvider _creds;
private readonly IHttpClientFactory _httpFactory;
public GoogleSearchService(IBotCredsProvider creds, IHttpClientFactory httpFactory)
{
_creds = creds;
_httpFactory = httpFactory;
}
public override async ITask<GoogleImageResult?> SearchImagesAsync(string query)
{
ArgumentNullException.ThrowIfNull(query);
var creds = _creds.GetCreds();
var key = creds.Google.ImageSearchId;
var cx = string.IsNullOrWhiteSpace(key)
? "c3f56de3be2034c07"
: key;
using var http = _httpFactory.CreateClient("google:search");
http.DefaultRequestHeaders.Add("Accept-Encoding", "gzip");
await using var stream = await http.GetStreamAsync(
$"https://customsearch.googleapis.com/customsearch/v1"
+ $"?cx={cx}"
+ $"&q={Uri.EscapeDataString(query)}"
+ $"&fields=items(image(contextLink%2CthumbnailLink)%2Clink)%2CsearchInformation"
+ $"&key={creds.GoogleApiKey}"
+ $"&searchType=image"
+ $"&safe=active");
var result = await System.Text.Json.JsonSerializer.DeserializeAsync<GoogleImageResult>(stream);
return result;
}
public override async ITask<GoogleCustomSearchResult?> SearchAsync(string query)
{
ArgumentNullException.ThrowIfNull(query);
var creds = _creds.GetCreds();
var key = creds.Google.SearchId;
var cx = string.IsNullOrWhiteSpace(key)
? "c7f1dac95987d4571"
: key;
using var http = _httpFactory.CreateClient("google:search");
http.DefaultRequestHeaders.Add("Accept-Encoding", "gzip");
await using var stream = await http.GetStreamAsync(
$"https://customsearch.googleapis.com/customsearch/v1"
+ $"?cx={cx}"
+ $"&q={Uri.EscapeDataString(query)}"
+ $"&fields=items(title%2Clink%2CdisplayLink%2Csnippet)%2CsearchInformation"
+ $"&key={creds.GoogleApiKey}"
+ $"&safe=active");
var result = await System.Text.Json.JsonSerializer.DeserializeAsync<GoogleCustomSearchResult>(stream);
return result;
}
}

View File

@@ -0,0 +1,19 @@
using NadekoBot.Modules.Searches;
using System.Text.Json.Serialization;
namespace NadekoBot.Services;
public sealed class OfficialGoogleSearchResultEntry : ISearchResultEntry
{
[JsonPropertyName("title")]
public string Title { get; init; } = null!;
[JsonPropertyName("link")]
public string Url { get; init; } = null!;
[JsonPropertyName("displayLink")]
public string DisplayUrl { get; init; } = null!;
[JsonPropertyName("snippet")]
public string Description { get; init; } = null!;
}