2014-12-19 00:26:42 +00:00
|
|
|
using System;
|
2018-08-05 14:28:05 +00:00
|
|
|
using System.Collections.Generic;
|
2015-01-19 23:53:31 +00:00
|
|
|
using System.Linq;
|
2014-12-19 00:26:42 +00:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.Cache;
|
2015-01-19 23:53:31 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2017-06-17 13:02:58 +00:00
|
|
|
using NzbDrone.Core.Configuration;
|
2014-12-19 00:26:42 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-12-23 01:17:48 +00:00
|
|
|
public class TrackedDownloadService : ITrackedDownloadService
|
2014-12-19 00:26:42 +00:00
|
|
|
{
|
|
|
|
private readonly IParsingService _parsingService;
|
|
|
|
private readonly IHistoryService _historyService;
|
2017-06-17 13:02:58 +00:00
|
|
|
private readonly IConfigService _config;
|
2014-12-19 00:26:42 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
private readonly ICached<TrackedDownload> _cache;
|
|
|
|
|
|
|
|
public TrackedDownloadService(IParsingService parsingService,
|
|
|
|
ICacheManager cacheManager,
|
|
|
|
IHistoryService historyService,
|
2017-06-17 13:02:58 +00:00
|
|
|
IConfigService config,
|
2014-12-19 00:26:42 +00:00
|
|
|
Logger logger)
|
|
|
|
{
|
|
|
|
_parsingService = parsingService;
|
|
|
|
_historyService = historyService;
|
|
|
|
_cache = cacheManager.GetCache<TrackedDownload>(GetType());
|
2017-06-17 13:02:58 +00:00
|
|
|
_config = config;
|
2014-12-19 00:26:42 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2015-01-22 00:27:07 +00:00
|
|
|
public TrackedDownload Find(string downloadId)
|
2014-12-19 00:26:42 +00:00
|
|
|
{
|
2015-01-22 00:27:07 +00:00
|
|
|
return _cache.Find(downloadId);
|
2014-12-19 00:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public TrackedDownload TrackDownload(DownloadClientDefinition downloadClient, DownloadClientItem downloadItem)
|
|
|
|
{
|
|
|
|
var existingItem = Find(downloadItem.DownloadId);
|
|
|
|
|
2014-12-23 01:17:48 +00:00
|
|
|
if (existingItem != null && existingItem.State != TrackedDownloadStage.Downloading)
|
2014-12-19 00:26:42 +00:00
|
|
|
{
|
|
|
|
existingItem.DownloadItem = downloadItem;
|
|
|
|
return existingItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
var trackedDownload = new TrackedDownload
|
|
|
|
{
|
|
|
|
DownloadClient = downloadClient.Id,
|
|
|
|
DownloadItem = downloadItem,
|
|
|
|
Protocol = downloadClient.Protocol
|
|
|
|
};
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2018-08-05 14:28:05 +00:00
|
|
|
|
2015-06-29 18:04:12 +00:00
|
|
|
var historyItems = _historyService.FindByDownloadId(downloadItem.DownloadId);
|
2018-08-05 14:28:05 +00:00
|
|
|
var firstHistoryItem = historyItems.OrderByDescending(h => h.Date).FirstOrDefault(h => h.EventType == HistoryEventType.Grabbed);
|
|
|
|
//TODO: Create release info from history and use that here, so we don't loose indexer flags!
|
|
|
|
var parsedMovieInfo = _parsingService.ParseMovieInfo(trackedDownload.DownloadItem.Title, new List<object>{firstHistoryItem});
|
2014-12-19 00:26:42 +00:00
|
|
|
|
2017-01-05 11:39:48 +00:00
|
|
|
if (parsedMovieInfo != null)
|
|
|
|
{
|
2017-06-18 21:12:14 +00:00
|
|
|
trackedDownload.RemoteMovie = _parsingService.Map(parsedMovieInfo, "", null).RemoteMovie;
|
2017-01-05 11:39:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-05 14:28:05 +00:00
|
|
|
if (firstHistoryItem != null)
|
2014-12-19 00:26:42 +00:00
|
|
|
{
|
2015-06-29 18:04:12 +00:00
|
|
|
trackedDownload.State = GetStateFromHistory(firstHistoryItem.EventType);
|
2015-01-19 23:53:31 +00:00
|
|
|
|
2017-02-07 08:03:15 +00:00
|
|
|
if (parsedMovieInfo == null ||
|
|
|
|
trackedDownload.RemoteMovie == null ||
|
|
|
|
trackedDownload.RemoteMovie.Movie == null)
|
2015-01-19 23:53:31 +00:00
|
|
|
{
|
2018-08-05 14:28:05 +00:00
|
|
|
parsedMovieInfo = _parsingService.ParseMovieInfo(firstHistoryItem.SourceTitle, new List<object>{firstHistoryItem});
|
2016-01-06 06:56:34 +00:00
|
|
|
|
2017-02-07 08:03:15 +00:00
|
|
|
if (parsedMovieInfo != null)
|
2015-06-29 18:04:12 +00:00
|
|
|
{
|
2017-06-18 21:12:14 +00:00
|
|
|
trackedDownload.RemoteMovie = _parsingService.Map(parsedMovieInfo, "", null).RemoteMovie;
|
2015-06-29 18:04:12 +00:00
|
|
|
}
|
2015-01-19 23:53:31 +00:00
|
|
|
}
|
2014-12-19 00:26:42 +00:00
|
|
|
}
|
|
|
|
|
2017-02-07 08:03:15 +00:00
|
|
|
if (trackedDownload.RemoteMovie == null)
|
2015-06-29 18:04:12 +00:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2014-12-19 00:26:42 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
_logger.Debug(e, "Failed to find movie for " + downloadItem.Title);
|
2014-12-19 00:26:42 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-01-22 00:27:07 +00:00
|
|
|
_cache.Set(trackedDownload.DownloadItem.DownloadId, trackedDownload);
|
2014-12-19 00:26:42 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-21 15:57:54 +00:00
|
|
|
}
|