mirror of
https://github.com/Sonarr/Sonarr.git
synced 2024-12-16 11:37:58 +02:00
New: Updated Nzbget Download Client proxy with time estimation for both download and post-processing stages.
This commit is contained in:
parent
b8c9f6d42e
commit
f304ad50d1
@ -52,7 +52,12 @@ public void Setup()
|
||||
Name = "Droned.S01E01.Pilot.1080p.WEB-DL-DRONE",
|
||||
DestDir = "somedirectory",
|
||||
Parameters = new List<NzbgetParameter> { new NzbgetParameter { Name = "drone", Value = "id" } },
|
||||
ParStatus = "Some Error"
|
||||
ParStatus = "Some Error",
|
||||
UnpackStatus = "NONE",
|
||||
MoveStatus = "NONE",
|
||||
ScriptStatus = "NONE",
|
||||
DeleteStatus = "NONE",
|
||||
MarkStatus = "NONE"
|
||||
};
|
||||
|
||||
_completed = new NzbgetHistoryItem
|
||||
@ -63,8 +68,19 @@ public void Setup()
|
||||
DestDir = "somedirectory",
|
||||
Parameters = new List<NzbgetParameter> { new NzbgetParameter { Name = "drone", Value = "id" } },
|
||||
ParStatus = "SUCCESS",
|
||||
ScriptStatus = "NONE"
|
||||
UnpackStatus = "NONE",
|
||||
MoveStatus = "SUCCESS",
|
||||
ScriptStatus = "NONE",
|
||||
DeleteStatus = "NONE",
|
||||
MarkStatus = "NONE"
|
||||
};
|
||||
|
||||
Mocker.GetMock<INzbgetProxy>()
|
||||
.Setup(s => s.GetGlobalStatus(It.IsAny<NzbgetSettings>()))
|
||||
.Returns(new NzbgetGlobalStatus
|
||||
{
|
||||
DownloadRate = 7000000
|
||||
});
|
||||
}
|
||||
|
||||
protected void WithFailedDownload()
|
||||
@ -93,6 +109,10 @@ protected virtual void WithQueue(NzbgetQueueItem queue)
|
||||
Mocker.GetMock<INzbgetProxy>()
|
||||
.Setup(s => s.GetQueue(It.IsAny<NzbgetSettings>()))
|
||||
.Returns(list);
|
||||
|
||||
Mocker.GetMock<INzbgetProxy>()
|
||||
.Setup(s => s.GetPostQueue(It.IsAny<NzbgetSettings>()))
|
||||
.Returns(new List<NzbgetPostQueueItem>());
|
||||
}
|
||||
|
||||
protected virtual void WithHistory(NzbgetHistoryItem history)
|
||||
@ -134,7 +154,7 @@ public void queued_item_should_have_required_properties()
|
||||
[Test]
|
||||
public void paused_item_should_have_required_properties()
|
||||
{
|
||||
_queued.PausedSizeLo = _queued.FileSizeLo;
|
||||
_queued.PausedSizeLo = _queued.RemainingSizeLo;
|
||||
|
||||
WithQueue(_queued);
|
||||
WithHistory(null);
|
||||
|
@ -44,7 +44,7 @@ private void GivenCompletedDownloadHandling(bool? enabled = null)
|
||||
|
||||
Mocker.GetMock<IDownloadTrackingService>()
|
||||
.Setup(v => v.GetCompletedDownloads())
|
||||
.Returns(_completed.ToList());
|
||||
.Returns(_completed.ToArray());
|
||||
}
|
||||
|
||||
private void GivenDroneFactoryFolder(bool exists = false)
|
||||
|
@ -8,7 +8,7 @@ public class NzbgetQueueItem
|
||||
public Int32 NzbId { get; set; }
|
||||
public Int32 FirstId { get; set; }
|
||||
public Int32 LastId { get; set; }
|
||||
public string NzbName { get; set; }
|
||||
public String NzbName { get; set; }
|
||||
public String Category { get; set; }
|
||||
public UInt32 FileSizeLo { get; set; }
|
||||
public UInt32 FileSizeHi { get; set; }
|
||||
@ -16,6 +16,8 @@ public class NzbgetQueueItem
|
||||
public UInt32 RemainingSizeHi { get; set; }
|
||||
public UInt32 PausedSizeLo { get; set; }
|
||||
public UInt32 PausedSizeHi { get; set; }
|
||||
public Int32 MinPriority { get; set; }
|
||||
public Int32 MaxPriority { get; set; }
|
||||
public Int32 ActiveDownloads { get; set; }
|
||||
public List<NzbgetParameter> Parameters { get; set; }
|
||||
}
|
||||
|
@ -56,11 +56,15 @@ public override string Download(RemoteEpisode remoteEpisode)
|
||||
|
||||
private IEnumerable<DownloadClientItem> GetQueue()
|
||||
{
|
||||
NzbgetGlobalStatus globalStatus;
|
||||
List<NzbgetQueueItem> queue;
|
||||
Dictionary<Int32, NzbgetPostQueueItem> postQueue;
|
||||
|
||||
try
|
||||
{
|
||||
globalStatus = _proxy.GetGlobalStatus(Settings);
|
||||
queue = _proxy.GetQueue(Settings);
|
||||
postQueue = _proxy.GetPostQueue(Settings).ToDictionary(v => v.NzbId);
|
||||
}
|
||||
catch (DownloadClientException ex)
|
||||
{
|
||||
@ -70,28 +74,57 @@ private IEnumerable<DownloadClientItem> GetQueue()
|
||||
|
||||
var queueItems = new List<DownloadClientItem>();
|
||||
|
||||
Int64 totalRemainingSize = 0;
|
||||
|
||||
foreach (var item in queue)
|
||||
{
|
||||
var postQueueItem = postQueue.GetValueOrDefault(item.NzbId);
|
||||
|
||||
Int64 totalSize = MakeInt64(item.FileSizeHi, item.FileSizeLo);
|
||||
Int64 pausedSize = MakeInt64(item.PausedSizeHi, item.PausedSizeLo);
|
||||
Int64 remainingSize = MakeInt64(item.RemainingSizeHi, item.RemainingSizeLo);
|
||||
|
||||
var droneParameter = item.Parameters.SingleOrDefault(p => p.Name == "drone");
|
||||
|
||||
var queueItem = new DownloadClientItem();
|
||||
queueItem.DownloadClientId = droneParameter == null ? item.NzbId.ToString() : droneParameter.Value.ToString();
|
||||
queueItem.Title = item.NzbName;
|
||||
queueItem.TotalSize = MakeInt64(item.FileSizeHi, item.FileSizeLo);
|
||||
queueItem.RemainingSize = MakeInt64(item.RemainingSizeHi, item.RemainingSizeLo);
|
||||
queueItem.TotalSize = totalSize;
|
||||
queueItem.Category = item.Category;
|
||||
|
||||
if (queueItem.TotalSize == MakeInt64(item.PausedSizeHi, item.PausedSizeLo))
|
||||
if (postQueueItem != null)
|
||||
{
|
||||
queueItem.Status = DownloadItemStatus.Downloading;
|
||||
queueItem.Message = postQueueItem.ProgressLabel;
|
||||
|
||||
if (postQueueItem.StageProgress != 0)
|
||||
{
|
||||
queueItem.RemainingTime = TimeSpan.FromSeconds(postQueueItem.StageTimeSec * 1000 / postQueueItem.StageProgress - postQueueItem.StageTimeSec);
|
||||
}
|
||||
}
|
||||
else if (globalStatus.DownloadPaused || remainingSize == pausedSize)
|
||||
{
|
||||
queueItem.Status = DownloadItemStatus.Paused;
|
||||
}
|
||||
else if (item.ActiveDownloads == 0 && queueItem.RemainingSize != 0)
|
||||
{
|
||||
queueItem.Status = DownloadItemStatus.Queued;
|
||||
queueItem.RemainingSize = remainingSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
queueItem.Status = DownloadItemStatus.Downloading;
|
||||
if (item.ActiveDownloads == 0 && remainingSize != 0)
|
||||
{
|
||||
queueItem.Status = DownloadItemStatus.Queued;
|
||||
}
|
||||
else
|
||||
{
|
||||
queueItem.Status = DownloadItemStatus.Downloading;
|
||||
}
|
||||
|
||||
queueItem.RemainingSize = remainingSize - pausedSize;
|
||||
|
||||
if (globalStatus.DownloadRate != 0)
|
||||
{
|
||||
queueItem.RemainingTime = TimeSpan.FromSeconds((totalRemainingSize + queueItem.RemainingSize) / globalStatus.DownloadRate);
|
||||
totalRemainingSize += queueItem.RemainingSize;
|
||||
}
|
||||
}
|
||||
|
||||
queueItems.Add(queueItem);
|
||||
@ -130,13 +163,17 @@ private IEnumerable<DownloadClientItem> GetHistory()
|
||||
historyItem.Category = item.Category;
|
||||
historyItem.Message = String.Format("PAR Status: {0} - Unpack Status: {1} - Move Status: {2} - Script Status: {3} - Delete Status: {4} - Mark Status: {5}", item.ParStatus, item.UnpackStatus, item.MoveStatus, item.ScriptStatus, item.DeleteStatus, item.MarkStatus);
|
||||
historyItem.Status = DownloadItemStatus.Completed;
|
||||
historyItem.RemainingTime = TimeSpan.Zero;
|
||||
|
||||
if (item.DeleteStatus == "MANUAL")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!successStatus.Contains(item.ParStatus) ||
|
||||
!successStatus.Contains(item.UnpackStatus) ||
|
||||
!successStatus.Contains(item.MoveStatus) ||
|
||||
!successStatus.Contains(item.ScriptStatus) ||
|
||||
!successStatus.Contains(item.DeleteStatus) ||
|
||||
!successStatus.Contains(item.MarkStatus))
|
||||
!successStatus.Contains(item.ScriptStatus))
|
||||
{
|
||||
historyItem.Status = DownloadItemStatus.Failed;
|
||||
}
|
||||
@ -179,7 +216,7 @@ public override void Test()
|
||||
_proxy.GetVersion(Settings);
|
||||
}
|
||||
|
||||
private VersionResponse GetVersion(string host = null, int port = 0, string username = null, string password = null)
|
||||
private String GetVersion(string host = null, int port = 0, string username = null, string password = null)
|
||||
{
|
||||
return _proxy.GetVersion(Settings);
|
||||
}
|
||||
|
@ -1,10 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace NzbDrone.Core.Download.Clients.Nzbget
|
||||
{
|
||||
public class NzbgetBooleanResponse
|
||||
{
|
||||
public String Version { get; set; }
|
||||
public Boolean Result { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace NzbDrone.Core.Download.Clients.Nzbget
|
||||
{
|
||||
public class NzbgetGlobalStatus
|
||||
{
|
||||
public UInt32 RemainingSizeLo { get; set; }
|
||||
public UInt32 RemainingSizeHi { get; set; }
|
||||
public UInt32 DownloadedSizeLo { get; set; }
|
||||
public UInt32 DownloadedSizeHi { get; set; }
|
||||
public UInt32 DownloadRate { get; set; }
|
||||
public UInt32 AverageDownloadRate { get; set; }
|
||||
public UInt32 DownloadLimit { get; set; }
|
||||
public Boolean DownloadPaused { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace NzbDrone.Core.Download.Clients.Nzbget
|
||||
{
|
||||
public class NzbgetPostQueueItem
|
||||
{
|
||||
public Int32 NzbId { get; set; }
|
||||
public String NzbName { get; set; }
|
||||
public String Stage { get; set; }
|
||||
public String ProgressLabel { get; set; }
|
||||
public Int32 FileProgress { get; set; }
|
||||
public Int32 StageProgress { get; set; }
|
||||
public Int32 TotalTimeSec { get; set; }
|
||||
public Int32 StageTimeSec { get; set; }
|
||||
}
|
||||
}
|
@ -13,9 +13,11 @@ namespace NzbDrone.Core.Download.Clients.Nzbget
|
||||
public interface INzbgetProxy
|
||||
{
|
||||
string DownloadNzb(Stream nzb, string title, string category, int priority, NzbgetSettings settings);
|
||||
NzbgetGlobalStatus GetGlobalStatus(NzbgetSettings settings);
|
||||
List<NzbgetQueueItem> GetQueue(NzbgetSettings settings);
|
||||
List<NzbgetPostQueueItem> GetPostQueue(NzbgetSettings settings);
|
||||
List<NzbgetHistoryItem> GetHistory(NzbgetSettings settings);
|
||||
VersionResponse GetVersion(NzbgetSettings settings);
|
||||
String GetVersion(NzbgetSettings settings);
|
||||
void RemoveFromHistory(string id, NzbgetSettings settings);
|
||||
void RetryDownload(string id, NzbgetSettings settings);
|
||||
}
|
||||
@ -34,7 +36,7 @@ public string DownloadNzb(Stream nzb, string title, string category, int priorit
|
||||
var parameters = new object[] { title, category, priority, false, Convert.ToBase64String(nzb.ToBytes()) };
|
||||
var request = BuildRequest(new JsonRequest("append", parameters));
|
||||
|
||||
var response = Json.Deserialize<NzbgetBooleanResponse>(ProcessRequest(request, settings));
|
||||
var response = Json.Deserialize<NzbgetResponse<Boolean>>(ProcessRequest(request, settings));
|
||||
_logger.Debug("Queue Response: [{0}]", response.Result);
|
||||
|
||||
if (!response.Result)
|
||||
@ -61,25 +63,39 @@ public string DownloadNzb(Stream nzb, string title, string category, int priorit
|
||||
return droneId;
|
||||
}
|
||||
|
||||
public NzbgetGlobalStatus GetGlobalStatus(NzbgetSettings settings)
|
||||
{
|
||||
var request = BuildRequest(new JsonRequest("status"));
|
||||
|
||||
return Json.Deserialize<NzbgetResponse<NzbgetGlobalStatus>>(ProcessRequest(request, settings)).Result;
|
||||
}
|
||||
|
||||
public List<NzbgetQueueItem> GetQueue(NzbgetSettings settings)
|
||||
{
|
||||
var request = BuildRequest(new JsonRequest("listgroups"));
|
||||
|
||||
return Json.Deserialize<NzbgetListResponse<NzbgetQueueItem>>(ProcessRequest(request, settings)).QueueItems;
|
||||
return Json.Deserialize<NzbgetResponse<List<NzbgetQueueItem>>>(ProcessRequest(request, settings)).Result;
|
||||
}
|
||||
|
||||
public List<NzbgetPostQueueItem> GetPostQueue(NzbgetSettings settings)
|
||||
{
|
||||
var request = BuildRequest(new JsonRequest("postqueue"));
|
||||
|
||||
return Json.Deserialize<NzbgetResponse<List<NzbgetPostQueueItem>>>(ProcessRequest(request, settings)).Result;
|
||||
}
|
||||
|
||||
public List<NzbgetHistoryItem> GetHistory(NzbgetSettings settings)
|
||||
{
|
||||
var request = BuildRequest(new JsonRequest("history"));
|
||||
|
||||
return Json.Deserialize<NzbgetListResponse<NzbgetHistoryItem>>(ProcessRequest(request, settings)).QueueItems;
|
||||
return Json.Deserialize<NzbgetResponse<List<NzbgetHistoryItem>>>(ProcessRequest(request, settings)).Result;
|
||||
}
|
||||
|
||||
public VersionResponse GetVersion(NzbgetSettings settings)
|
||||
public String GetVersion(NzbgetSettings settings)
|
||||
{
|
||||
var request = BuildRequest(new JsonRequest("version"));
|
||||
|
||||
return Json.Deserialize<VersionResponse>(ProcessRequest(request, settings));
|
||||
return Json.Deserialize<NzbgetResponse<String>>(ProcessRequest(request, settings)).Version;
|
||||
}
|
||||
|
||||
public void RemoveFromHistory(string id, NzbgetSettings settings)
|
||||
@ -120,7 +136,7 @@ private bool EditQueue(string command, int offset, string editText, int id, Nzbg
|
||||
{
|
||||
var parameters = new object[] { command, offset, editText, id };
|
||||
var request = BuildRequest(new JsonRequest("editqueue", parameters));
|
||||
var response = Json.Deserialize<NzbgetBooleanResponse>(ProcessRequest(request, settings));
|
||||
var response = Json.Deserialize<NzbgetResponse<Boolean>>(ProcessRequest(request, settings));
|
||||
|
||||
return response.Result;
|
||||
}
|
||||
|
@ -4,11 +4,11 @@
|
||||
|
||||
namespace NzbDrone.Core.Download.Clients.Nzbget
|
||||
{
|
||||
public class NzbgetListResponse<T>
|
||||
public class NzbgetResponse<T>
|
||||
{
|
||||
public String Version { get; set; }
|
||||
|
||||
[JsonProperty(PropertyName = "result")]
|
||||
public List<T> QueueItems { get; set; }
|
||||
public T Result { get; set; }
|
||||
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace NzbDrone.Core.Download.Clients.Nzbget
|
||||
{
|
||||
public class VersionResponse
|
||||
{
|
||||
public String Version { get; set; }
|
||||
public String Result { get; set; }
|
||||
}
|
||||
}
|
@ -83,6 +83,8 @@ public override IEnumerable<DownloadClientItem> GetItems()
|
||||
else
|
||||
{
|
||||
historyItem.Status = DownloadItemStatus.Completed;
|
||||
|
||||
historyItem.RemainingTime = TimeSpan.Zero;
|
||||
}
|
||||
|
||||
historyItem.RemoteEpisode = GetRemoteEpisode(historyItem.Title);
|
||||
|
@ -8,22 +8,23 @@ namespace NzbDrone.Core.Download
|
||||
{
|
||||
public class DownloadClientItem
|
||||
{
|
||||
public string DownloadClient { get; set; }
|
||||
public string DownloadClientId { get; set; }
|
||||
public string Category { get; set; }
|
||||
public string Title { get; set; }
|
||||
public String DownloadClient { get; set; }
|
||||
public String DownloadClientId { get; set; }
|
||||
public String Category { get; set; }
|
||||
public String Title { get; set; }
|
||||
|
||||
public long TotalSize { get; set; }
|
||||
public long RemainingSize { get; set; }
|
||||
public Int64 TotalSize { get; set; }
|
||||
public Int64 RemainingSize { get; set; }
|
||||
public TimeSpan DownloadTime { get; set; }
|
||||
public TimeSpan RemainingTime { get; set; }
|
||||
|
||||
public string OutputPath { get; set; }
|
||||
public string Message { get; set; }
|
||||
public String OutputPath { get; set; }
|
||||
public String Message { get; set; }
|
||||
|
||||
public DownloadItemStatus Status { get; set; }
|
||||
public bool IsEncrypted { get; set; }
|
||||
public bool IsReadOnly { get; set; }
|
||||
public Boolean IsEncrypted { get; set; }
|
||||
public Boolean IsReadOnly { get; set; }
|
||||
|
||||
public RemoteEpisode RemoteEpisode { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -15,12 +15,12 @@ namespace NzbDrone.Core.Download
|
||||
{
|
||||
public interface IDownloadTrackingService
|
||||
{
|
||||
List<TrackedDownload> GetTrackedDownloads();
|
||||
List<TrackedDownload> GetCompletedDownloads();
|
||||
List<TrackedDownload> GetQueuedDownloads();
|
||||
TrackedDownload[] GetTrackedDownloads();
|
||||
TrackedDownload[] GetCompletedDownloads();
|
||||
TrackedDownload[] GetQueuedDownloads();
|
||||
}
|
||||
|
||||
public class DownloadTrackingService : IDownloadTrackingService, IExecute<CheckForFinishedDownloadCommand>, IHandle<ApplicationStartedEvent>
|
||||
public class DownloadTrackingService : IDownloadTrackingService, IExecute<CheckForFinishedDownloadCommand>, IHandle<ApplicationStartedEvent>, IHandle<EpisodeGrabbedEvent>
|
||||
{
|
||||
private readonly IProvideDownloadClient _downloadClientProvider;
|
||||
private readonly IHistoryService _historyService;
|
||||
@ -30,8 +30,7 @@ public class DownloadTrackingService : IDownloadTrackingService, IExecute<CheckF
|
||||
private readonly ICompletedDownloadService _completedDownloadService;
|
||||
private readonly Logger _logger;
|
||||
|
||||
private readonly ICached<TrackedDownload> _trackedDownloads;
|
||||
private readonly ICached<List<TrackedDownload>> _queuedDownloads;
|
||||
private readonly ICached<TrackedDownload[]> _trackedDownloadCache;
|
||||
|
||||
public static string DOWNLOAD_CLIENT = "downloadClient";
|
||||
public static string DOWNLOAD_CLIENT_ID = "downloadClientId";
|
||||
@ -53,108 +52,105 @@ public DownloadTrackingService(IProvideDownloadClient downloadClientProvider,
|
||||
_completedDownloadService = completedDownloadService;
|
||||
_logger = logger;
|
||||
|
||||
_trackedDownloads = cacheManager.GetCache<TrackedDownload>(GetType());
|
||||
_queuedDownloads = cacheManager.GetCache<List<TrackedDownload>>(GetType(), "queued");
|
||||
_trackedDownloadCache = cacheManager.GetCache<TrackedDownload[]>(GetType());
|
||||
}
|
||||
|
||||
public List<TrackedDownload> GetTrackedDownloads()
|
||||
public TrackedDownload[] GetTrackedDownloads()
|
||||
{
|
||||
return _trackedDownloads.Values.ToList();
|
||||
return _trackedDownloadCache.Get("tracked", () => new TrackedDownload[0]);
|
||||
}
|
||||
|
||||
public List<TrackedDownload> GetCompletedDownloads()
|
||||
public TrackedDownload[] GetCompletedDownloads()
|
||||
{
|
||||
return _trackedDownloads.Values.Where(v => v.State == TrackedDownloadState.Downloading && v.DownloadItem.Status == DownloadItemStatus.Completed).ToList();
|
||||
return GetTrackedDownloads()
|
||||
.Where(v => v.State == TrackedDownloadState.Downloading && v.DownloadItem.Status == DownloadItemStatus.Completed)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
public List<TrackedDownload> GetQueuedDownloads()
|
||||
public TrackedDownload[] GetQueuedDownloads()
|
||||
{
|
||||
return _queuedDownloads.Get("queued", () =>
|
||||
return _trackedDownloadCache.Get("queued", () =>
|
||||
{
|
||||
UpdateTrackedDownloads();
|
||||
|
||||
var enabledFailedDownloadHandling = _configService.EnableFailedDownloadHandling;
|
||||
var enabledCompletedDownloadHandling = _configService.EnableCompletedDownloadHandling;
|
||||
|
||||
return _trackedDownloads.Values
|
||||
.Where(v => v.State == TrackedDownloadState.Downloading)
|
||||
.Where(v =>
|
||||
v.DownloadItem.Status == DownloadItemStatus.Queued ||
|
||||
v.DownloadItem.Status == DownloadItemStatus.Paused ||
|
||||
v.DownloadItem.Status == DownloadItemStatus.Downloading ||
|
||||
v.DownloadItem.Status == DownloadItemStatus.Failed && enabledFailedDownloadHandling ||
|
||||
v.DownloadItem.Status == DownloadItemStatus.Completed && enabledCompletedDownloadHandling)
|
||||
.ToList();
|
||||
return FilterQueuedDownloads(GetTrackedDownloads());
|
||||
|
||||
}, TimeSpan.FromSeconds(5.0));
|
||||
}
|
||||
|
||||
private TrackedDownload GetTrackedDownload(IDownloadClient downloadClient, DownloadClientItem queueItem)
|
||||
private TrackedDownload[] FilterQueuedDownloads(IEnumerable<TrackedDownload> trackedDownloads)
|
||||
{
|
||||
var id = String.Format("{0}-{1}", downloadClient.Definition.Id, queueItem.DownloadClientId);
|
||||
var trackedDownload = _trackedDownloads.Get(id, () => new TrackedDownload
|
||||
{
|
||||
TrackingId = id,
|
||||
DownloadClient = downloadClient.Definition.Id,
|
||||
StartedTracking = DateTime.UtcNow,
|
||||
State = TrackedDownloadState.Unknown
|
||||
});
|
||||
var enabledFailedDownloadHandling = _configService.EnableFailedDownloadHandling;
|
||||
var enabledCompletedDownloadHandling = _configService.EnableCompletedDownloadHandling;
|
||||
|
||||
trackedDownload.DownloadItem = queueItem;
|
||||
|
||||
return trackedDownload;
|
||||
return trackedDownloads
|
||||
.Where(v => v.State == TrackedDownloadState.Downloading)
|
||||
.Where(v =>
|
||||
v.DownloadItem.Status == DownloadItemStatus.Queued ||
|
||||
v.DownloadItem.Status == DownloadItemStatus.Paused ||
|
||||
v.DownloadItem.Status == DownloadItemStatus.Downloading ||
|
||||
v.DownloadItem.Status == DownloadItemStatus.Failed && enabledFailedDownloadHandling ||
|
||||
v.DownloadItem.Status == DownloadItemStatus.Completed && enabledCompletedDownloadHandling)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
|
||||
private List<History.History> GetHistoryItems(List<History.History> grabbedHistory, string downloadClientId)
|
||||
{
|
||||
return grabbedHistory.Where(h => downloadClientId.Equals(h.Data.GetValueOrDefault(DOWNLOAD_CLIENT_ID)))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
|
||||
private Boolean UpdateTrackedDownloads()
|
||||
{
|
||||
var downloadClients = _downloadClientProvider.GetDownloadClients();
|
||||
|
||||
var oldTrackedDownloads = new HashSet<TrackedDownload>(_trackedDownloads.Values);
|
||||
var newTrackedDownloads = new HashSet<TrackedDownload>();
|
||||
var oldTrackedDownloads = GetTrackedDownloads().ToDictionary(v => v.TrackingId);
|
||||
var newTrackedDownloads = new List<TrackedDownload>();
|
||||
|
||||
var stateChanged = false;
|
||||
|
||||
foreach (var downloadClient in downloadClients)
|
||||
{
|
||||
var downloadClientHistory = downloadClient.GetItems().Select(v => GetTrackedDownload(downloadClient, v)).ToList();
|
||||
foreach (var trackedDownload in downloadClientHistory)
|
||||
var downloadClientHistory = downloadClient.GetItems().ToList();
|
||||
foreach (var downloadItem in downloadClientHistory)
|
||||
{
|
||||
if (!oldTrackedDownloads.Contains(trackedDownload))
|
||||
var trackingId = String.Format("{0}-{1}", downloadClient.Definition.Id, downloadItem.DownloadClientId);
|
||||
TrackedDownload trackedDownload;
|
||||
|
||||
if (!oldTrackedDownloads.TryGetValue(trackingId, out trackedDownload))
|
||||
{
|
||||
_logger.Trace("Started tracking download from history: {0}", trackedDownload.TrackingId);
|
||||
trackedDownload = new TrackedDownload
|
||||
{
|
||||
TrackingId = trackingId,
|
||||
DownloadClient = downloadClient.Definition.Id,
|
||||
StartedTracking = DateTime.UtcNow,
|
||||
State = TrackedDownloadState.Unknown
|
||||
};
|
||||
|
||||
_logger.Trace("Started tracking download from history: {0}: {1}", trackedDownload.TrackingId, downloadItem.Title);
|
||||
stateChanged = true;
|
||||
}
|
||||
|
||||
trackedDownload.DownloadItem = downloadItem;
|
||||
|
||||
newTrackedDownloads.Add(trackedDownload);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var item in oldTrackedDownloads.Except(newTrackedDownloads))
|
||||
foreach (var downloadItem in oldTrackedDownloads.Values.Except(newTrackedDownloads))
|
||||
{
|
||||
if (item.State != TrackedDownloadState.Removed)
|
||||
if (downloadItem.State != TrackedDownloadState.Removed)
|
||||
{
|
||||
item.State = TrackedDownloadState.Removed;
|
||||
downloadItem.State = TrackedDownloadState.Removed;
|
||||
stateChanged = true;
|
||||
|
||||
_logger.Debug("Item removed from download client by user: {0}", item.TrackingId);
|
||||
_logger.Debug("Item removed from download client by user: {0}: {1}", downloadItem.TrackingId, downloadItem.DownloadItem.Title);
|
||||
}
|
||||
|
||||
_logger.Trace("Stopped tracking download: {0}: {1}", downloadItem.TrackingId, downloadItem.DownloadItem.Title);
|
||||
}
|
||||
|
||||
foreach (var item in newTrackedDownloads.Union(oldTrackedDownloads).Where(v => v.State == TrackedDownloadState.Removed))
|
||||
{
|
||||
_trackedDownloads.Remove(item.TrackingId);
|
||||
|
||||
_logger.Trace("Stopped tracking download: {0}", item.TrackingId);
|
||||
}
|
||||
|
||||
_queuedDownloads.Clear();
|
||||
_trackedDownloadCache.Set("tracked", newTrackedDownloads.ToArray());
|
||||
|
||||
return stateChanged;
|
||||
}
|
||||
@ -168,7 +164,7 @@ private void ProcessTrackedDownloads()
|
||||
var stateChanged = UpdateTrackedDownloads();
|
||||
|
||||
var downloadClients = _downloadClientProvider.GetDownloadClients();
|
||||
var trackedDownloads = _trackedDownloads.Values.ToArray();
|
||||
var trackedDownloads = GetTrackedDownloads();
|
||||
|
||||
foreach (var trackedDownload in trackedDownloads)
|
||||
{
|
||||
@ -190,6 +186,8 @@ private void ProcessTrackedDownloads()
|
||||
}
|
||||
}
|
||||
|
||||
_trackedDownloadCache.Set("queued", FilterQueuedDownloads(trackedDownloads), TimeSpan.FromSeconds(5.0));
|
||||
|
||||
if (stateChanged)
|
||||
{
|
||||
_eventAggregator.PublishEvent(new UpdateQueueEvent());
|
||||
@ -205,5 +203,10 @@ public void Handle(ApplicationStartedEvent message)
|
||||
{
|
||||
ProcessTrackedDownloads();
|
||||
}
|
||||
|
||||
public void Handle(EpisodeGrabbedEvent message)
|
||||
{
|
||||
ProcessTrackedDownloads();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -239,6 +239,8 @@
|
||||
<Compile Include="DecisionEngine\Specifications\RssSync\HistorySpecification.cs" />
|
||||
<Compile Include="DiskSpace\DiskSpace.cs" />
|
||||
<Compile Include="DiskSpace\DiskSpaceService.cs" />
|
||||
<Compile Include="Download\Clients\Nzbget\NzbgetGlobalStatus.cs" />
|
||||
<Compile Include="Download\Clients\Nzbget\NzbgetPostQueueItem.cs" />
|
||||
<Compile Include="Download\Clients\Sabnzbd\SabnzbdDownloadStatus.cs" />
|
||||
<Compile Include="Download\Clients\UsenetBlackhole\UsenetBlackhole.cs" />
|
||||
<Compile Include="Download\Clients\UsenetBlackhole\TestUsenetBlackholeCommand.cs" />
|
||||
@ -525,13 +527,11 @@
|
||||
<Compile Include="Instrumentation\LogService.cs" />
|
||||
<Compile Include="Instrumentation\DatabaseTarget.cs" />
|
||||
<Compile Include="MediaFiles\MediaInfo\MediaInfoModel.cs" />
|
||||
<Compile Include="Download\Clients\Nzbget\NzbgetBooleanResponse.cs" />
|
||||
<Compile Include="Download\Clients\Nzbget\ErrorModel.cs" />
|
||||
<Compile Include="Download\Clients\Nzbget\JsonError.cs" />
|
||||
<Compile Include="Download\Clients\Nzbget\NzbgetListResponse.cs" />
|
||||
<Compile Include="Download\Clients\Nzbget\NzbgetResponse.cs" />
|
||||
<Compile Include="Download\Clients\Nzbget\NzbgetQueueItem.cs" />
|
||||
<Compile Include="Download\Clients\Nzbget\NzbgetPriority.cs" />
|
||||
<Compile Include="Download\Clients\Nzbget\VersionResponse.cs" />
|
||||
<Compile Include="Organizer\NamingConfig.cs" />
|
||||
<Compile Include="Parser\Language.cs" />
|
||||
<Compile Include="Parser\Model\LocalEpisode.cs" />
|
||||
|
Loading…
Reference in New Issue
Block a user