|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
existingItem.DownloadItem = downloadItem;
|
|
|
|
|
return existingItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var trackedDownload = new TrackedDownload
|
|
|
|
|
{
|
|
|
|
|
DownloadClient = downloadClient.Id,
|
|
|
|
|
DownloadItem = downloadItem,
|
|
|
|
|
Protocol = downloadClient.Protocol
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var parsedAlbumInfo = Parser.Parser.ParseAlbumTitle(trackedDownload.DownloadItem.Title);
|
|
|
|
|
var historyItems = _historyService.FindByDownloadId(downloadItem.DownloadId);
|
|
|
|
|
|
|
|
|
|
if (parsedAlbumInfo != null)
|
|
|
|
|
{
|
|
|
|
|
trackedDownload.RemoteAlbum = _parsingService.Map(parsedAlbumInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (historyItems.Any())
|
|
|
|
|
{
|
|
|
|
|
var firstHistoryItem = historyItems.OrderByDescending(h => h.Date).First();
|
|
|
|
|
trackedDownload.State = GetStateFromHistory(firstHistoryItem.EventType);
|
|
|
|
|
|
|
|
|
|
if (parsedAlbumInfo == null ||
|
|
|
|
|
trackedDownload.RemoteAlbum == null ||
|
|
|
|
|
trackedDownload.RemoteAlbum.Artist == null ||
|
|
|
|
|
trackedDownload.RemoteAlbum.Albums.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
|
|
|
|
|
parsedAlbumInfo = Parser.Parser.ParseAlbumTitle(firstHistoryItem.SourceTitle);
|
|
|
|
|
|
|
|
|
|
if (parsedAlbumInfo != null)
|
|
|
|
|
{
|
|
|
|
|
trackedDownload.RemoteAlbum = _parsingService.Map(parsedAlbumInfo, firstHistoryItem.ArtistId, historyItems.Where(v => v.EventType == HistoryEventType.Grabbed).Select(h => h.AlbumId).Distinct());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (trackedDownload.RemoteAlbum == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.Debug(e, "Failed to find album for " + downloadItem.Title);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_cache.Set(trackedDownload.DownloadItem.DownloadId, trackedDownload);
|
|
|
|
|
return trackedDownload;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static TrackedDownloadStage GetStateFromHistory(HistoryEventType eventType)
|
|
|
|
|
{
|
|
|
|
|
switch (eventType)
|
|
|
|
|
{
|
|
|
|
|
case HistoryEventType.DownloadFolderImported:
|
|
|
|
|
return TrackedDownloadStage.Imported;
|
|
|
|
|
case HistoryEventType.DownloadFailed:
|
|
|
|
|
return TrackedDownloadStage.DownloadFailed;
|
|
|
|
|
default:
|
|
|
|
|
return TrackedDownloadStage.Downloading;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|