Files
2026-01-03 23:34:21 +02:00

53 lines
1.7 KiB
C#

using Cleanuparr.Infrastructure.Features.Arr;
using Cleanuparr.Infrastructure.Features.Arr.Interfaces;
using Cleanuparr.Infrastructure.Features.DownloadHunter.Interfaces;
using Cleanuparr.Infrastructure.Features.DownloadHunter.Models;
using Cleanuparr.Persistence;
using Cleanuparr.Shared.Helpers;
using Data.Models.Arr;
using Microsoft.EntityFrameworkCore;
namespace Cleanuparr.Infrastructure.Features.DownloadHunter;
public sealed class DownloadHunter : IDownloadHunter
{
private readonly DataContext _dataContext;
private readonly IArrClientFactory _arrClientFactory;
private readonly TimeProvider _timeProvider;
public DownloadHunter(
DataContext dataContext,
IArrClientFactory arrClientFactory,
TimeProvider timeProvider
)
{
_dataContext = dataContext;
_arrClientFactory = arrClientFactory;
_timeProvider = timeProvider;
}
public async Task HuntDownloadsAsync<T>(DownloadHuntRequest<T> request)
where T : SearchItem
{
var generalConfig = await _dataContext.GeneralConfigs
.AsNoTracking()
.FirstAsync();
if (!generalConfig.SearchEnabled)
{
return;
}
var arrClient = _arrClientFactory.GetClient(request.InstanceType, request.Instance.Version);
await arrClient.SearchItemsAsync(request.Instance, [request.SearchItem]);
// Prevent manual db edits
if (generalConfig.SearchDelay < Constants.MinSearchDelaySeconds)
{
generalConfig.SearchDelay = Constants.DefaultSearchDelaySeconds;
}
// Prevent tracker spamming
await Task.Delay(TimeSpan.FromSeconds(generalConfig.SearchDelay), _timeProvider);
}
}