mirror of
https://github.com/Radarr/Radarr.git
synced 2026-04-21 22:05:43 -04:00
d0e8aef949
* New: Consider all scene SxxE00 releases Specials as well. * Cleanup special episode titles and handle E00 specials with existing scenemappings. * Moved handling of scene mapped E00 to central function.
140 lines
5.7 KiB
C#
140 lines
5.7 KiB
C#
using System;
|
|
using System.Linq;
|
|
using NLog;
|
|
using NzbDrone.Common.Cache;
|
|
using NzbDrone.Common.Extensions;
|
|
using NzbDrone.Core.History;
|
|
using NzbDrone.Core.Parser;
|
|
|
|
namespace NzbDrone.Core.Download.TrackedDownloads
|
|
{
|
|
public interface ITrackedDownloadService
|
|
{
|
|
TrackedDownload Find(string downloadId);
|
|
TrackedDownload TrackDownload(DownloadClientDefinition downloadClient, DownloadClientItem downloadItem);
|
|
}
|
|
|
|
public class TrackedDownloadService : ITrackedDownloadService
|
|
{
|
|
private readonly IParsingService _parsingService;
|
|
private readonly IHistoryService _historyService;
|
|
private readonly Logger _logger;
|
|
private readonly ICached<TrackedDownload> _cache;
|
|
|
|
public TrackedDownloadService(IParsingService parsingService,
|
|
ICacheManager cacheManager,
|
|
IHistoryService historyService,
|
|
Logger logger)
|
|
{
|
|
_parsingService = parsingService;
|
|
_historyService = historyService;
|
|
_cache = cacheManager.GetCache<TrackedDownload>(GetType());
|
|
_logger = logger;
|
|
}
|
|
|
|
public TrackedDownload Find(string downloadId)
|
|
{
|
|
return _cache.Find(downloadId);
|
|
}
|
|
|
|
public TrackedDownload TrackDownload(DownloadClientDefinition downloadClient, DownloadClientItem downloadItem)
|
|
{
|
|
var existingItem = Find(downloadItem.DownloadId);
|
|
|
|
if (existingItem != null && existingItem.State != TrackedDownloadStage.Downloading)
|
|
{
|
|
LogItemChange(existingItem, existingItem.DownloadItem, downloadItem);
|
|
|
|
existingItem.DownloadItem = downloadItem;
|
|
return existingItem;
|
|
}
|
|
|
|
var trackedDownload = new TrackedDownload
|
|
{
|
|
DownloadClient = downloadClient.Id,
|
|
DownloadItem = downloadItem,
|
|
Protocol = downloadClient.Protocol
|
|
};
|
|
|
|
try
|
|
{
|
|
var parsedEpisodeInfo = Parser.Parser.ParseTitle(trackedDownload.DownloadItem.Title);
|
|
var historyItems = _historyService.FindByDownloadId(downloadItem.DownloadId);
|
|
|
|
if (parsedEpisodeInfo != null)
|
|
{
|
|
trackedDownload.RemoteEpisode = _parsingService.Map(parsedEpisodeInfo, 0, 0);
|
|
}
|
|
|
|
if (historyItems.Any())
|
|
{
|
|
var firstHistoryItem = historyItems.OrderByDescending(h => h.Date).First();
|
|
trackedDownload.State = GetStateFromHistory(firstHistoryItem.EventType);
|
|
|
|
if (parsedEpisodeInfo == null ||
|
|
trackedDownload.RemoteEpisode == null ||
|
|
trackedDownload.RemoteEpisode.Series == null ||
|
|
trackedDownload.RemoteEpisode.Episodes.Empty())
|
|
{
|
|
// Try parsing the original source title and if that fails, try parsing it as a special
|
|
// TODO: Pass the TVDB ID and TVRage IDs in as well so we have a better chance for finding the item
|
|
parsedEpisodeInfo = Parser.Parser.ParseTitle(firstHistoryItem.SourceTitle) ?? _parsingService.ParseSpecialEpisodeTitle(parsedEpisodeInfo, firstHistoryItem.SourceTitle, 0, 0);
|
|
|
|
if (parsedEpisodeInfo != null)
|
|
{
|
|
trackedDownload.RemoteEpisode = _parsingService.Map(parsedEpisodeInfo, firstHistoryItem.SeriesId, historyItems.Where(v => v.EventType == HistoryEventType.Grabbed).Select(h => h.EpisodeId).Distinct());
|
|
}
|
|
}
|
|
}
|
|
|
|
if (trackedDownload.RemoteEpisode == null)
|
|
{
|
|
_logger.Trace("No Episode found for download '{0}', not tracking.", trackedDownload.DownloadItem.Title);
|
|
|
|
return null;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.Debug(e, "Failed to find episode for " + downloadItem.Title);
|
|
return null;
|
|
}
|
|
|
|
LogItemChange(trackedDownload, existingItem?.DownloadItem, trackedDownload.DownloadItem);
|
|
|
|
_cache.Set(trackedDownload.DownloadItem.DownloadId, trackedDownload);
|
|
return trackedDownload;
|
|
}
|
|
|
|
private void LogItemChange(TrackedDownload trackedDownload, DownloadClientItem existingItem, DownloadClientItem downloadItem)
|
|
{
|
|
if (existingItem == null ||
|
|
existingItem.Status != downloadItem.Status ||
|
|
existingItem.CanBeRemoved != downloadItem.CanBeRemoved ||
|
|
existingItem.CanMoveFiles != downloadItem.CanMoveFiles)
|
|
{
|
|
_logger.Debug("Tracking '{0}:{1}': ClientState={2}{3} SonarrStage={4} Episode='{5}' OutputPath={6}.",
|
|
downloadItem.DownloadClient, downloadItem.Title,
|
|
downloadItem.Status, downloadItem.CanBeRemoved ? "" :
|
|
downloadItem.CanMoveFiles ? " (busy)" : " (readonly)",
|
|
trackedDownload.State,
|
|
trackedDownload.RemoteEpisode?.ParsedEpisodeInfo,
|
|
downloadItem.OutputPath);
|
|
}
|
|
}
|
|
|
|
private static TrackedDownloadStage GetStateFromHistory(HistoryEventType eventType)
|
|
{
|
|
switch (eventType)
|
|
{
|
|
case HistoryEventType.DownloadFolderImported:
|
|
return TrackedDownloadStage.Imported;
|
|
case HistoryEventType.DownloadFailed:
|
|
return TrackedDownloadStage.DownloadFailed;
|
|
default:
|
|
return TrackedDownloadStage.Downloading;
|
|
}
|
|
}
|
|
}
|
|
}
|