Skip to content

Usenet streaming with a WebDAV server and a SABnzbd-compatible API

C#

Added RefreshMonitoredDownloads api.

nzbdav-dev79d40bcb239f
diff --git a/backend/Clients/RadarrSonarr/ArrClient.cs b/backend/Clients/RadarrSonarr/ArrClient.cs
index 2e4f07b0..4c0fa36a 100644
--- a/backend/Clients/RadarrSonarr/ArrClient.cs
+++ b/backend/Clients/RadarrSonarr/ArrClient.cs
@@ -21,6 +21,9 @@ public class ArrClient(string host, string apiKey)
public Task<List<ArrRootFolder>> GetRootFolders() =>
Get<List<ArrRootFolder>>($"/rootfolder");
+ public Task<List<ArrDownloadClient>> GetDownloadClientsAsync() =>
+ Get<List<ArrDownloadClient>>($"/downloadClient");
+
public Task<ArrCommand> RefreshMonitoredDownloads() =>
CommandAsync(new { name = "RefreshMonitoredDownloads" });
@@ -30,6 +33,9 @@ public class ArrClient(string host, string apiKey)
public Task<ArrQueue<ArrQueueRecord>> GetQueueAsync() =>
Get<ArrQueue<ArrQueueRecord>>($"/queue?protocol=usenet&pageSize=5000");
+ public async Task<int> GetQueueCountAsync() =>
+ (await Get<ArrQueue<ArrQueueRecord>>($"/queue?pageSize=1")).TotalRecords;
+
public Task<HttpStatusCode> DeleteQueueRecord(int id, DeleteQueueRecordRequest request) =>
Delete($"/queue/{id}", request.GetQueryParams());
diff --git a/backend/Clients/RadarrSonarr/BaseModels/ArrDownloadClient.cs b/backend/Clients/RadarrSonarr/BaseModels/ArrDownloadClient.cs
new file mode 100644
index 00000000..fd689905
--- /dev/null
+++ b/backend/Clients/RadarrSonarr/BaseModels/ArrDownloadClient.cs
@@ -0,0 +1,22 @@
+using System.Text.Json.Serialization;
+
+namespace NzbWebDAV.Clients.RadarrSonarr.BaseModels;
+
+public class ArrDownloadClient
+{
+ [JsonPropertyName("enable")]
+ public bool Enable { get; set; }
+
+ [JsonPropertyName("protocol")]
+ public string Protocol { get; set; }
+
+ [JsonPropertyName("name")]
+ public string Name { get; set; }
+
+ [JsonPropertyName("fields")]
+ public List<ArrField> Fields { get; set; }
+
+ public string? Category => (string?)Fields
+ .FirstOrDefault(x => x.Name is "movieCategory" or "tvCategory")
+ ?.Value;
+}
\ No newline at end of file
diff --git a/backend/Clients/RadarrSonarr/BaseModels/ArrField.cs b/backend/Clients/RadarrSonarr/BaseModels/ArrField.cs
new file mode 100644
index 00000000..c392e4fa
--- /dev/null
+++ b/backend/Clients/RadarrSonarr/BaseModels/ArrField.cs
@@ -0,0 +1,23 @@
+using System.Text.Json;
+using System.Text.Json.Serialization;
+
+namespace NzbWebDAV.Clients.RadarrSonarr.BaseModels;
+
+public class ArrField
+{
+ [JsonPropertyName("name")]
+ public string Name { get; set; }
+
+ [JsonPropertyName("label")]
+ public string Label { get; set; }
+
+ [JsonPropertyName("value")]
+ public JsonElement? ValueJson { get; set; }
+
+ public object? Value => ValueJson?.ValueKind == JsonValueKind.Null ? null
+ : ValueJson?.ValueKind == JsonValueKind.String ? ValueJson?.ToString()
+ : ValueJson?.ValueKind == JsonValueKind.Number ? ValueJson?.GetInt64()
+ : ValueJson?.ValueKind == JsonValueKind.True ? true
+ : ValueJson?.ValueKind == JsonValueKind.False ? false
+ : ValueJson?.GetRawText();
+}
\ No newline at end of file
diff --git a/backend/Queue/QueueItemProcessor.cs b/backend/Queue/QueueItemProcessor.cs
index 6972d48e..8600d113 100644
--- a/backend/Queue/QueueItemProcessor.cs
+++ b/backend/Queue/QueueItemProcessor.cs
@@ -1,6 +1,7 @@
using System.Text;
using Microsoft.EntityFrameworkCore;
using NzbWebDAV.Api.SabControllers.GetHistory;
+using NzbWebDAV.Clients.RadarrSonarr;
using NzbWebDAV.Clients.Usenet;
using NzbWebDAV.Clients.Usenet.Connections;
using NzbWebDAV.Config;
@@ -294,5 +295,30 @@ public class QueueItemProcessor(
await dbClient.Ctx.SaveChangesAsync(ct);
_ = websocketManager.SendMessage(WebsocketTopic.QueueItemRemoved, queueItem.Id.ToString());
_ = websocketManager.SendMessage(WebsocketTopic.HistoryItemAdded, historySlot.ToJson());
+ _ = RefreshMonitoredDownloads();
+ }
+
+ private async Task RefreshMonitoredDownloads()
+ {
+ var tasks = configManager
+ .GetArrConfig()
+ .GetArrClients()
+ .Select(RefreshMonitoredDownloads);
+ await Task.WhenAll(tasks);
+ }
+
+ private async Task RefreshMonitoredDownloads(ArrClient arrClient)
+ {
+ try
+ {
+ var downloadClients = await arrClient.GetDownloadClientsAsync();
+ if (downloadClients.All(x => x.Category != queueItem.Category)) return;
+ var queueCount = await arrClient.GetQueueCountAsync();
+ if (queueCount < 60) await arrClient.RefreshMonitoredDownloads();
+ }
+ catch (Exception e)
+ {
+ Log.Debug($"Could not refresh monitored downloads for Arr instance: `{arrClient.Host}`. {e.Message}");
+ }
}
}
\ No newline at end of file