Files
nadekobot/src/NadekoBot/Modules/Searches/Search/Youtube/InvidiousYtSearchService.cs
Kwoth 3c108e531e dev: Added initial version of the grpc api. Added relevant dummy settings to creds (they have no effect rn)
dev: Yt searches now INTERNALLY return multiple results but there is no way right now to paginate plain text results
dev: moved some stuff around
2024-09-26 07:26:18 +00:00

48 lines
1.4 KiB
C#

using NadekoBot.Modules.Searches.Youtube;
using System.Net.Http.Json;
namespace NadekoBot.Modules.Searches;
public sealed class InvidiousYtSearchService : IYoutubeSearchService, INService
{
private readonly IHttpClientFactory _http;
private readonly SearchesConfigService _scs;
private readonly NadekoRandom _rng;
public InvidiousYtSearchService(
IHttpClientFactory http,
SearchesConfigService scs)
{
_http = http;
_scs = scs;
_rng = new();
}
public async Task<VideoInfo[]?> SearchAsync(string query)
{
ArgumentNullException.ThrowIfNull(query);
var instances = _scs.Data.InvidiousInstances;
if (instances is null or { Count: 0 })
{
Log.Warning("Attempted to use Invidious as the .youtube provider but there are no 'invidiousInstances' "
+ "specified in `data/searches.yml`");
return null;
}
var instance = instances[_rng.Next(0, instances.Count)];
var url = $"{instance}/api/v1/search"
+ $"?q={query}"
+ $"&type=video";
using var http = _http.CreateClient();
var res = await http.GetFromJsonAsync<List<InvidiousSearchResponse>>(
url);
if (res is null or { Count: 0 })
return null;
return res.Map(r => new VideoInfo(r.VideoId));
}
}