mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-19 00:03:36 +00:00
231 lines
7.5 KiB
C#
231 lines
7.5 KiB
C#
using System.Net.Http.Headers;
|
|
using System.Net.Http.Json;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using Jellyfin.Plugin.SceneSearch.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Jellyfin.Plugin.SceneSearch;
|
|
|
|
/// <summary>
|
|
/// HTTP client for the local scene-search service.
|
|
/// </summary>
|
|
public sealed class SceneSearchClient : IDisposable
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
private readonly ILogger<SceneSearchClient> _logger;
|
|
|
|
public SceneSearchClient(ILogger<SceneSearchClient> logger)
|
|
{
|
|
_logger = logger;
|
|
_httpClient = new HttpClient();
|
|
}
|
|
|
|
public bool IsConfigured
|
|
{
|
|
get
|
|
{
|
|
var config = Configuration;
|
|
return config.Enabled
|
|
&& Uri.TryCreate(config.ApiUrl, UriKind.Absolute, out _);
|
|
}
|
|
}
|
|
|
|
private static PluginConfiguration Configuration
|
|
=> Plugin.Instance?.Configuration ?? new PluginConfiguration();
|
|
|
|
internal async Task<IReadOnlyList<SceneSearchItem>> SearchAsync(
|
|
string searchTerm,
|
|
int limit,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var config = Configuration;
|
|
if (!config.Enabled || string.IsNullOrWhiteSpace(config.ApiUrl))
|
|
{
|
|
return [];
|
|
}
|
|
|
|
var safeLimit = Math.Clamp(limit, 1, Math.Clamp(config.MaxResults, 1, 50));
|
|
var path = string.Concat(
|
|
"/api/search?q=",
|
|
Uri.EscapeDataString(searchTerm),
|
|
"&scene_limit=",
|
|
Math.Min(safeLimit * 4, 100).ToString(System.Globalization.CultureInfo.InvariantCulture),
|
|
"&item_limit=",
|
|
safeLimit.ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
|
|
var payload = await SendAsync<SceneSearchResponse>(
|
|
HttpMethod.Get,
|
|
path,
|
|
content: null,
|
|
cancellationToken).ConfigureAwait(false);
|
|
|
|
return payload?.Items ?? [];
|
|
}
|
|
|
|
public Task<SceneSearchLibraryListResponse?> GetLibrariesAsync(
|
|
CancellationToken cancellationToken)
|
|
=> SendAsync<SceneSearchLibraryListResponse>(
|
|
HttpMethod.Get,
|
|
"/api/jellyfin/libraries",
|
|
content: null,
|
|
cancellationToken);
|
|
|
|
public Task<Dictionary<string, object>?> ApplyLibrarySelectionAsync(
|
|
SceneSearchIndexRequest request,
|
|
CancellationToken cancellationToken)
|
|
=> SendAsync<Dictionary<string, object>>(
|
|
HttpMethod.Post,
|
|
"/api/jellyfin/libraries/selection",
|
|
JsonContent.Create(request),
|
|
cancellationToken);
|
|
|
|
public Task<SceneSearchIndexStatus?> StartIndexAsync(
|
|
SceneSearchIndexRequest request,
|
|
CancellationToken cancellationToken)
|
|
=> SendAsync<SceneSearchIndexStatus>(
|
|
HttpMethod.Post,
|
|
"/api/jellyfin/index",
|
|
JsonContent.Create(request),
|
|
cancellationToken);
|
|
|
|
public Task<SceneSearchIndexStatus?> GetIndexStatusAsync(
|
|
CancellationToken cancellationToken)
|
|
=> SendAsync<SceneSearchIndexStatus>(
|
|
HttpMethod.Get,
|
|
"/api/jellyfin/index/status",
|
|
content: null,
|
|
cancellationToken);
|
|
|
|
public Task<SceneSearchIndexReportResponse?> GetIndexReportAsync(
|
|
string? category,
|
|
int offset,
|
|
int limit,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var path = string.Concat(
|
|
"/api/jellyfin/index/report?offset=",
|
|
Math.Max(offset, 0).ToString(System.Globalization.CultureInfo.InvariantCulture),
|
|
"&limit=",
|
|
Math.Clamp(limit, 1, 1000).ToString(System.Globalization.CultureInfo.InvariantCulture));
|
|
|
|
if (!string.IsNullOrWhiteSpace(category))
|
|
{
|
|
path += "&category=" + Uri.EscapeDataString(category);
|
|
}
|
|
|
|
return SendAsync<SceneSearchIndexReportResponse>(
|
|
HttpMethod.Get,
|
|
path,
|
|
content: null,
|
|
cancellationToken);
|
|
}
|
|
|
|
public Task<SceneSearchCaptionStatus?> StartCaptionAsync(
|
|
SceneSearchCaptionRequest request,
|
|
CancellationToken cancellationToken)
|
|
=> SendAsync<SceneSearchCaptionStatus>(
|
|
HttpMethod.Post,
|
|
"/api/caption",
|
|
JsonContent.Create(request),
|
|
cancellationToken);
|
|
|
|
public Task<SceneSearchCaptionStatus?> GetCaptionStatusAsync(
|
|
CancellationToken cancellationToken)
|
|
=> SendAsync<SceneSearchCaptionStatus>(
|
|
HttpMethod.Get,
|
|
"/api/caption/status",
|
|
content: null,
|
|
cancellationToken);
|
|
|
|
public Task<SceneSearchCaptionStatus?> PauseCaptionAsync(
|
|
CancellationToken cancellationToken)
|
|
=> SendAsync<SceneSearchCaptionStatus>(
|
|
HttpMethod.Post,
|
|
"/api/caption/pause",
|
|
content: null,
|
|
cancellationToken);
|
|
|
|
public Task<SceneSearchCaptionStatus?> ResumeCaptionAsync(
|
|
CancellationToken cancellationToken)
|
|
=> SendAsync<SceneSearchCaptionStatus>(
|
|
HttpMethod.Post,
|
|
"/api/caption/resume",
|
|
content: null,
|
|
cancellationToken);
|
|
|
|
public Task<SceneSearchCaptionStatus?> StopCaptionAsync(
|
|
CancellationToken cancellationToken)
|
|
=> SendAsync<SceneSearchCaptionStatus>(
|
|
HttpMethod.Post,
|
|
"/api/caption/stop",
|
|
content: null,
|
|
cancellationToken);
|
|
|
|
private async Task<T?> SendAsync<T>(
|
|
HttpMethod method,
|
|
string path,
|
|
HttpContent? content,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var config = Configuration;
|
|
if (string.IsNullOrWhiteSpace(config.ApiUrl))
|
|
{
|
|
return default;
|
|
}
|
|
|
|
using var request = new HttpRequestMessage(
|
|
method,
|
|
config.ApiUrl.TrimEnd('/') + path)
|
|
{
|
|
Content = content
|
|
};
|
|
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
|
|
|
|
if (!string.IsNullOrWhiteSpace(config.ApiKey))
|
|
{
|
|
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", config.ApiKey);
|
|
}
|
|
|
|
using var timeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
|
timeout.CancelAfter(TimeSpan.FromSeconds(Math.Clamp(config.TimeoutSeconds, 5, 300)));
|
|
|
|
try
|
|
{
|
|
using var response = await _httpClient.SendAsync(
|
|
request,
|
|
HttpCompletionOption.ResponseHeadersRead,
|
|
timeout.Token).ConfigureAwait(false);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
var body = await response.Content.ReadAsStringAsync(timeout.Token).ConfigureAwait(false);
|
|
throw new HttpRequestException(
|
|
$"Scene Search API returned HTTP {(int)response.StatusCode}: {body}");
|
|
}
|
|
|
|
return await response.Content.ReadFromJsonAsync<T>(
|
|
cancellationToken: timeout.Token).ConfigureAwait(false);
|
|
}
|
|
catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested)
|
|
{
|
|
_logger.LogWarning(
|
|
"Scene Search API timed out after {TimeoutSeconds}s for {Path}",
|
|
config.TimeoutSeconds,
|
|
path);
|
|
return default;
|
|
}
|
|
catch (HttpRequestException ex)
|
|
{
|
|
_logger.LogWarning(ex, "Scene Search API request failed for {Path}", path);
|
|
return default;
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_httpClient.Dispose();
|
|
GC.SuppressFinalize(this);
|
|
}
|
|
}
|