mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 09:48:26 -04:00
add: Added .q support for invidious. If you have ytProvider set to invidious in data/searches.yml, invidious will be used to queue up songs and play them."work
This commit is contained in:
@@ -60,21 +60,21 @@ public partial class Searches
|
||||
descStr = descStr.TrimTo(4096);
|
||||
|
||||
var embed = _sender.CreateEmbed()
|
||||
.WithOkColor()
|
||||
.WithAuthor(ctx.User)
|
||||
.WithTitle(query.TrimTo(64)!)
|
||||
.WithDescription(descStr)
|
||||
.WithFooter(
|
||||
GetText(strs.results_in(data.Info.TotalResults, data.Info.SearchTime)),
|
||||
"https://i.imgur.com/G46fm8J.png");
|
||||
.WithOkColor()
|
||||
.WithAuthor(ctx.User)
|
||||
.WithTitle(query.TrimTo(64)!)
|
||||
.WithDescription(descStr)
|
||||
.WithFooter(
|
||||
GetText(strs.results_in(data.Info.TotalResults, data.Info.SearchTime)),
|
||||
"https://i.imgur.com/G46fm8J.png");
|
||||
|
||||
await Response().Embed(embed).SendAsync();
|
||||
}
|
||||
|
||||
[Cmd]
|
||||
public async Task Image([Leftover] string? query)
|
||||
public async Task Image([Leftover] string query)
|
||||
{
|
||||
query = query?.Trim();
|
||||
query = query.Trim();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(query))
|
||||
{
|
||||
@@ -99,34 +99,37 @@ public partial class Searches
|
||||
EmbedBuilder CreateEmbed(IImageSearchResultEntry entry)
|
||||
{
|
||||
return _sender.CreateEmbed()
|
||||
.WithOkColor()
|
||||
.WithAuthor(ctx.User)
|
||||
.WithTitle(query)
|
||||
.WithUrl("https://google.com")
|
||||
.WithImageUrl(entry.Link);
|
||||
.WithOkColor()
|
||||
.WithAuthor(ctx.User)
|
||||
.WithTitle(query)
|
||||
.WithUrl("https://google.com")
|
||||
.WithImageUrl(entry.Link);
|
||||
}
|
||||
|
||||
embeds.Add(CreateEmbed(data.Entries.First())
|
||||
.WithFooter(
|
||||
GetText(strs.results_in(data.Info.TotalResults, data.Info.SearchTime)),
|
||||
"https://i.imgur.com/G46fm8J.png"));
|
||||
await Response()
|
||||
.Paginated()
|
||||
.Items(data.Entries)
|
||||
.PageSize(1)
|
||||
.AddFooter(false)
|
||||
.Page((items, _) =>
|
||||
{
|
||||
var item = items.FirstOrDefault();
|
||||
|
||||
var random = data.Entries.Skip(1)
|
||||
.Shuffle()
|
||||
.Take(3)
|
||||
.ToArray();
|
||||
if (item is null)
|
||||
return _sender.CreateEmbed()
|
||||
.WithDescription(GetText(strs.no_search_results));
|
||||
|
||||
foreach (var entry in random)
|
||||
{
|
||||
embeds.Add(CreateEmbed(entry));
|
||||
}
|
||||
var embed = CreateEmbed(item);
|
||||
embeds.Add(embed);
|
||||
|
||||
await Response().Embeds(embeds).SendAsync();
|
||||
return embed;
|
||||
})
|
||||
.SendAsync();
|
||||
}
|
||||
|
||||
private TypedKey<string> GetYtCacheKey(string query)
|
||||
=> new($"search:youtube:{query}");
|
||||
|
||||
|
||||
private async Task AddYoutubeUrlToCacheAsync(string query, string url)
|
||||
=> await _cache.AddAsync(GetYtCacheKey(query), url, expiry: 1.Hours());
|
||||
|
||||
@@ -158,7 +161,7 @@ public partial class Searches
|
||||
|
||||
var maybeResult = await GetYoutubeUrlFromCacheAsync(query)
|
||||
?? await _searchFactory.GetYoutubeSearchService().SearchAsync(query);
|
||||
if (maybeResult is not {} result || result is {Url: null})
|
||||
if (maybeResult is not { } result || result is { Url: null })
|
||||
{
|
||||
await Response().Error(strs.no_results).SendAsync();
|
||||
return;
|
||||
|
@@ -5,5 +5,59 @@ namespace NadekoBot.Modules.Searches;
|
||||
public sealed class InvidiousSearchResponse
|
||||
{
|
||||
[JsonPropertyName("videoId")]
|
||||
public string VideoId { get; set; } = null!;
|
||||
public required string VideoId { get; init; }
|
||||
|
||||
[JsonPropertyName("title")]
|
||||
public required string Title { get; init; }
|
||||
|
||||
[JsonPropertyName("videoThumbnails")]
|
||||
public required List<InvidiousThumbnail> Thumbnails { get; init; }
|
||||
|
||||
[JsonPropertyName("lengthSeconds")]
|
||||
public required int LengthSeconds { get; init; }
|
||||
|
||||
[JsonPropertyName("description")]
|
||||
public required string Description { get; init; }
|
||||
}
|
||||
|
||||
public sealed class InvidiousVideoResponse
|
||||
{
|
||||
[JsonPropertyName("title")]
|
||||
public required string Title { get; init; }
|
||||
|
||||
[JsonPropertyName("videoId")]
|
||||
public required string VideoId { get; init; }
|
||||
|
||||
[JsonPropertyName("lengthSeconds")]
|
||||
public required int LengthSeconds { get; init; }
|
||||
|
||||
[JsonPropertyName("videoThumbnails")]
|
||||
public required List<InvidiousThumbnail> Thumbnails { get; init; }
|
||||
|
||||
[JsonPropertyName("adaptiveFormats")]
|
||||
public required List<InvidiousAdaptiveFormat> AdaptiveFormats { get; init; }
|
||||
}
|
||||
|
||||
public sealed class InvidiousAdaptiveFormat
|
||||
{
|
||||
[JsonPropertyName("url")]
|
||||
public required string Url { get; init; }
|
||||
|
||||
[JsonPropertyName("audioQuality")]
|
||||
public string? AudioQuality { get; init; }
|
||||
}
|
||||
|
||||
public sealed class InvidiousPlaylistResponse
|
||||
{
|
||||
[JsonPropertyName("title")]
|
||||
public required string Title { get; init; }
|
||||
|
||||
[JsonPropertyName("videos")]
|
||||
public required List<InvidiousVideoResponse> Videos { get; init; }
|
||||
}
|
||||
|
||||
public sealed class InvidiousThumbnail
|
||||
{
|
||||
[JsonPropertyName("url")]
|
||||
public required string Url { get; init; }
|
||||
}
|
@@ -32,13 +32,14 @@ public sealed class InvidiousYtSearchService : IYoutubeSearchService, INService
|
||||
|
||||
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>>(
|
||||
$"{instance}/api/v1/search"
|
||||
+ $"?q={query}"
|
||||
+ $"&type=video");
|
||||
url);
|
||||
|
||||
if (res is null or {Count: 0})
|
||||
if (res is null or { Count: 0 })
|
||||
return null;
|
||||
|
||||
return new VideoInfo(res[0].VideoId);
|
||||
|
Reference in New Issue
Block a user