refactored code

This commit is contained in:
Flaminel
2024-11-10 19:42:25 +02:00
parent c48090db2f
commit 9945ed20bf
@@ -43,34 +43,11 @@ public sealed class BlockedTorrentHandler
ushort page = 1;
int totalRecords = 0;
int processedRecords = 0;
List<int> seriesToBeRefreshed = [];
HashSet<int> seriesToBeRefreshed = [];
do
{
Uri sonarrUri = new(sonarrInstance.Url, string.Format(QueueListPathTemplate, page));
HttpRequestMessage sonarrRequest = new(HttpMethod.Get, sonarrUri);
sonarrRequest.Headers.Add("x-api-key", sonarrInstance.ApiKey);
HttpResponseMessage response = await _httpClient.SendAsync(sonarrRequest);
try
{
response.EnsureSuccessStatusCode();
}
catch
{
_logger.LogError("queue list failed | {uri}", sonarrUri);
throw;
}
string responseBody = await response.Content.ReadAsStringAsync();
QueueListResponse? queueResponse = JsonConvert.DeserializeObject<QueueListResponse>(responseBody);
if (queueResponse is null)
{
throw new Exception($"Failed to process response body:{responseBody}");
}
QueueListResponse queueResponse = await ListQueuedTorrentsAsync(sonarrInstance, page);
foreach (Record record in queueResponse.Records)
{
@@ -80,50 +57,17 @@ public sealed class BlockedTorrentHandler
if (torrent is not { CompletionOn: not null, Downloaded: null or 0 })
{
_logger.LogInformation("skip | {torrent}", record.Title);
continue;
return;
}
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);
response = await _httpClient.SendAsync(sonarrRequest);
try
{
response.EnsureSuccessStatusCode();
}
catch
{
_logger.LogError("queue delete failed | {uri}", sonarrUri);
throw;
}
await DeleteTorrentFromQueueAsync(sonarrInstance, record);
}
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;
}
await RefreshSeriesAsync(sonarrInstance, id);
}
if (queueResponse.Records.Count is 0)
@@ -147,4 +91,77 @@ public sealed class BlockedTorrentHandler
} while (processedRecords < totalRecords);
}
}
private async Task<QueueListResponse> ListQueuedTorrentsAsync(SonarrInstance sonarrInstance, int page)
{
Uri sonarrUri = new(sonarrInstance.Url, string.Format(QueueListPathTemplate, page));
using HttpRequestMessage sonarrRequest = new(HttpMethod.Get, sonarrUri);
sonarrRequest.Headers.Add("x-api-key", sonarrInstance.ApiKey);
using HttpResponseMessage response = await _httpClient.SendAsync(sonarrRequest);
try
{
response.EnsureSuccessStatusCode();
}
catch
{
_logger.LogError("queue list failed | {uri}", sonarrUri);
throw;
}
string responseBody = await response.Content.ReadAsStringAsync();
QueueListResponse? queueResponse = JsonConvert.DeserializeObject<QueueListResponse>(responseBody);
if (queueResponse is null)
{
throw new Exception($"unrecognized response | {responseBody}");
}
return queueResponse;
}
private async Task DeleteTorrentFromQueueAsync(SonarrInstance sonarrInstance, Record record)
{
Uri sonarrUri = new(sonarrInstance.Url, string.Format(QueueDeletePathTemplate, record.Id));
using HttpRequestMessage sonarrRequest = new(HttpMethod.Delete, sonarrUri);
sonarrRequest.Headers.Add("x-api-key", sonarrInstance.ApiKey);
using HttpResponseMessage response = await _httpClient.SendAsync(sonarrRequest);
try
{
response.EnsureSuccessStatusCode();
}
catch
{
_logger.LogError("queue delete failed | {uri}", sonarrUri);
throw;
}
}
private async Task RefreshSeriesAsync(SonarrInstance sonarrInstance, int seriesId)
{
Uri sonarrUri = new(sonarrInstance.Url, SonarrCommandUriPath);
using HttpRequestMessage sonarrRequest = new(HttpMethod.Post, sonarrUri);
sonarrRequest.Content = new StringContent(
SearchCommandPayloadTemplate.Replace("{0}", seriesId.ToString()),
Encoding.UTF8,
"application/json"
);
sonarrRequest.Headers.Add("x-api-key", sonarrInstance.ApiKey);
using HttpResponseMessage response = await _httpClient.SendAsync(sonarrRequest);
try
{
response.EnsureSuccessStatusCode();
}
catch
{
_logger.LogError("series search failed | series id: {id}", seriesId);
throw;
}
}
}