mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 09:48:26 -04:00
More restructuring
This commit is contained in:
@@ -1,46 +0,0 @@
|
||||
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)];
|
||||
|
||||
using var http = _http.CreateClient();
|
||||
var res = await http.GetFromJsonAsync<List<InvidiousSearchResponse>>(
|
||||
$"{instance}/api/v1/search"
|
||||
+ $"?q={query}"
|
||||
+ $"&type=video");
|
||||
|
||||
if (res is null or {Count: 0})
|
||||
return null;
|
||||
|
||||
return new VideoInfo(res[0].VideoId);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user