From c48090db2f57254da1b9c9133fd220ade351b017 Mon Sep 17 00:00:00 2001 From: Flaminel Date: Sun, 10 Nov 2024 03:17:43 +0200 Subject: [PATCH] renamed classes; added series search call --- code/Common/Configuration/QuartzConfig.cs | 2 +- code/Executable/DependencyInjection.cs | 18 +++---- code/Executable/Jobs/BlockedTorrentJob.cs | 29 ++++++++++ code/Executable/Jobs/FrozenTorrentJob.cs | 29 ---------- code/Executable/appsettings.json | 2 +- .../BlockedTorrentHandler.cs} | 53 ++++++++++++++----- 6 files changed, 81 insertions(+), 52 deletions(-) create mode 100644 code/Executable/Jobs/BlockedTorrentJob.cs delete mode 100644 code/Executable/Jobs/FrozenTorrentJob.cs rename code/Infrastructure/Verticals/{FrozenTorrent/FrozenTorrentHandler.cs => BlockedTorrent/BlockedTorrentHandler.cs} (66%) diff --git a/code/Common/Configuration/QuartzConfig.cs b/code/Common/Configuration/QuartzConfig.cs index 3db3fed9..448beec6 100644 --- a/code/Common/Configuration/QuartzConfig.cs +++ b/code/Common/Configuration/QuartzConfig.cs @@ -2,5 +2,5 @@ public sealed class QuartzConfig { - public required string FrozenTorrentTrigger { get; init; } + public required string BlockedTorrentTrigger { get; init; } } \ No newline at end of file diff --git a/code/Executable/DependencyInjection.cs b/code/Executable/DependencyInjection.cs index 5bbd388b..8d6c6b11 100644 --- a/code/Executable/DependencyInjection.cs +++ b/code/Executable/DependencyInjection.cs @@ -1,6 +1,6 @@ using Common.Configuration; using Executable.Jobs; -using Infrastructure.Verticals.FrozenTorrent; +using Infrastructure.Verticals.BlockedTorrent; namespace Executable; using Quartz; @@ -23,8 +23,8 @@ public static class DependencyInjection private static IServiceCollection AddServices(this IServiceCollection services) => services - .AddTransient() - .AddTransient(); + .AddTransient() + .AddTransient(); private static IServiceCollection AddQuartzServices(this IServiceCollection services, IConfiguration configuration) => services @@ -37,24 +37,24 @@ public static class DependencyInjection throw new NullReferenceException("Quartz configuration is null"); } - q.AddFrozenTorrentJob(config.FrozenTorrentTrigger); + q.AddBlockedTorrentJob(config.BlockedTorrentTrigger); }) .AddQuartzHostedService(opt => { opt.WaitForJobsToComplete = true; }); - private static void AddFrozenTorrentJob(this IServiceCollectionQuartzConfigurator q, string trigger) + private static void AddBlockedTorrentJob(this IServiceCollectionQuartzConfigurator q, string trigger) { - q.AddJob(opts => + q.AddJob(opts => { - opts.WithIdentity(nameof(FrozenTorrentJob)); + opts.WithIdentity(nameof(BlockedTorrentJob)); }); q.AddTrigger(opts => { - opts.ForJob(nameof(FrozenTorrentJob)) - .WithIdentity($"{nameof(FrozenTorrentJob)}-trigger") + opts.ForJob(nameof(BlockedTorrentJob)) + .WithIdentity($"{nameof(BlockedTorrentJob)}-trigger") .WithCronSchedule(trigger); }); } diff --git a/code/Executable/Jobs/BlockedTorrentJob.cs b/code/Executable/Jobs/BlockedTorrentJob.cs new file mode 100644 index 00000000..c1287a11 --- /dev/null +++ b/code/Executable/Jobs/BlockedTorrentJob.cs @@ -0,0 +1,29 @@ +using Infrastructure.Verticals.BlockedTorrent; +using Quartz; + +namespace Executable.Jobs; + +[DisallowConcurrentExecution] +public sealed class BlockedTorrentJob : IJob +{ + private ILogger _logger; + private BlockedTorrentHandler _handler; + + public BlockedTorrentJob(ILogger logger, BlockedTorrentHandler handler) + { + _logger = logger; + _handler = handler; + } + + public async Task Execute(IJobExecutionContext context) + { + try + { + await _handler.HandleAsync(); + } + catch (Exception ex) + { + _logger.LogError(ex, $"{nameof(BlockedTorrentJob)} failed"); + } + } +} \ No newline at end of file diff --git a/code/Executable/Jobs/FrozenTorrentJob.cs b/code/Executable/Jobs/FrozenTorrentJob.cs deleted file mode 100644 index 07d91ea7..00000000 --- a/code/Executable/Jobs/FrozenTorrentJob.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Infrastructure.Verticals.FrozenTorrent; -using Quartz; - -namespace Executable.Jobs; - -[DisallowConcurrentExecution] -public sealed class FrozenTorrentJob : IJob -{ - private ILogger _logger; - private FrozenTorrentHandler _handler; - - public FrozenTorrentJob(ILogger logger, FrozenTorrentHandler handler) - { - _logger = logger; - _handler = handler; - } - - public async Task Execute(IJobExecutionContext context) - { - try - { - await _handler.HandleAsync(); - } - catch (Exception ex) - { - _logger.LogError(ex, $"{nameof(FrozenTorrentJob)} failed"); - } - } -} \ No newline at end of file diff --git a/code/Executable/appsettings.json b/code/Executable/appsettings.json index 1c7360a5..44c7f1b7 100644 --- a/code/Executable/appsettings.json +++ b/code/Executable/appsettings.json @@ -8,7 +8,7 @@ } }, "QuartzConfig": { - "FrozenTorrentTrigger": "0 0/5 * * * ?" + "BlockedTorrentTrigger": "0 0/5 * * * ?" }, "QBitConfig": { "Url": "http://localhost:8080", diff --git a/code/Infrastructure/Verticals/FrozenTorrent/FrozenTorrentHandler.cs b/code/Infrastructure/Verticals/BlockedTorrent/BlockedTorrentHandler.cs similarity index 66% rename from code/Infrastructure/Verticals/FrozenTorrent/FrozenTorrentHandler.cs rename to code/Infrastructure/Verticals/BlockedTorrent/BlockedTorrentHandler.cs index 548b3029..7b32d79c 100644 --- a/code/Infrastructure/Verticals/FrozenTorrent/FrozenTorrentHandler.cs +++ b/code/Infrastructure/Verticals/BlockedTorrent/BlockedTorrentHandler.cs @@ -1,25 +1,27 @@ -using Common.Configuration; +using System.Text; +using Common.Configuration; using Domain.Sonarr.Queue; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Newtonsoft.Json; using QBittorrent.Client; -namespace Infrastructure.Verticals.FrozenTorrent; +namespace Infrastructure.Verticals.BlockedTorrent; -public sealed class FrozenTorrentHandler +public sealed class BlockedTorrentHandler { - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly QBitConfig _qBitConfig; private readonly SonarrConfig _sonarrConfig; private readonly HttpClient _httpClient; - private const string SonarListUriTemplate = "/api/v3/queue?page={0}&pageSize=200&sortKey=timeleft"; - private const string SonarDeleteUriTemplate = "/api/v3/queue/{0}?removeFromClient=true&blocklist=true&skipRedownload=true&changeCategory=false"; - - public FrozenTorrentHandler( - ILogger logger, + private const string QueueListPathTemplate = "/api/v3/queue?page={0}&pageSize=200&sortKey=timeleft"; + private const string QueueDeletePathTemplate = "/api/v3/queue/{0}?removeFromClient=true&blocklist=true&skipRedownload=true&changeCategory=false"; + private const string SonarrCommandUriPath = "/api/v3/command"; + private const string SearchCommandPayloadTemplate = "{\"name\":\"SeriesSearch\",\"seriesId\":{0}}"; + + public BlockedTorrentHandler( + ILogger logger, IOptions qBitConfig, IOptions sonarrConfig, IHttpClientFactory httpClientFactory) @@ -41,10 +43,11 @@ public sealed class FrozenTorrentHandler ushort page = 1; int totalRecords = 0; int processedRecords = 0; + List seriesToBeRefreshed = []; do { - Uri sonarrUri = new(sonarrInstance.Url, string.Format(SonarListUriTemplate, page)); + Uri sonarrUri = new(sonarrInstance.Url, string.Format(QueueListPathTemplate, page)); HttpRequestMessage sonarrRequest = new(HttpMethod.Get, sonarrUri); sonarrRequest.Headers.Add("x-api-key", sonarrInstance.ApiKey); @@ -80,7 +83,9 @@ public sealed class FrozenTorrentHandler continue; } - sonarrUri = new(sonarrInstance.Url, string.Format(SonarDeleteUriTemplate, record.Id)); + seriesToBeRefreshed.Add(record.SeriesId); + + sonarrUri = new(sonarrInstance.Url, string.Format(QueueDeletePathTemplate, record.Id)); sonarrRequest = new(HttpMethod.Delete, sonarrUri); sonarrRequest.Headers.Add("x-api-key", sonarrInstance.ApiKey); @@ -96,6 +101,30 @@ public sealed class FrozenTorrentHandler throw; } } + + foreach (int id in seriesToBeRefreshed) + { + sonarrUri = new(sonarrInstance.Url, SonarrCommandUriPath); + sonarrRequest = new(HttpMethod.Post, sonarrUri); + sonarrRequest.Content = new StringContent( + SearchCommandPayloadTemplate.Replace("{0}", id.ToString()), + Encoding.UTF8, + "application/json" + ); + sonarrRequest.Headers.Add("x-api-key", sonarrInstance.ApiKey); + + response = await _httpClient.SendAsync(sonarrRequest); + + try + { + response.EnsureSuccessStatusCode(); + } + catch + { + _logger.LogError("series search failed | series id: {id}", id); + throw; + } + } if (queueResponse.Records.Count is 0) {