refactored code
This commit is contained in:
@@ -43,34 +43,11 @@ public sealed class BlockedTorrentHandler
|
|||||||
ushort page = 1;
|
ushort page = 1;
|
||||||
int totalRecords = 0;
|
int totalRecords = 0;
|
||||||
int processedRecords = 0;
|
int processedRecords = 0;
|
||||||
List<int> seriesToBeRefreshed = [];
|
HashSet<int> seriesToBeRefreshed = [];
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
Uri sonarrUri = new(sonarrInstance.Url, string.Format(QueueListPathTemplate, page));
|
QueueListResponse queueResponse = await ListQueuedTorrentsAsync(sonarrInstance, 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}");
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (Record record in queueResponse.Records)
|
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 })
|
if (torrent is not { CompletionOn: not null, Downloaded: null or 0 })
|
||||||
{
|
{
|
||||||
_logger.LogInformation("skip | {torrent}", record.Title);
|
_logger.LogInformation("skip | {torrent}", record.Title);
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
seriesToBeRefreshed.Add(record.SeriesId);
|
seriesToBeRefreshed.Add(record.SeriesId);
|
||||||
|
|
||||||
sonarrUri = new(sonarrInstance.Url, string.Format(QueueDeletePathTemplate, record.Id));
|
await DeleteTorrentFromQueueAsync(sonarrInstance, record);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (int id in seriesToBeRefreshed)
|
foreach (int id in seriesToBeRefreshed)
|
||||||
{
|
{
|
||||||
sonarrUri = new(sonarrInstance.Url, SonarrCommandUriPath);
|
await RefreshSeriesAsync(sonarrInstance, id);
|
||||||
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)
|
if (queueResponse.Records.Count is 0)
|
||||||
@@ -147,4 +91,77 @@ public sealed class BlockedTorrentHandler
|
|||||||
} while (processedRecords < totalRecords);
|
} 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user