2018-03-14 20:41:36 +00:00
|
|
|
using System;
|
2014-06-08 08:22:55 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Core.DecisionEngine;
|
|
|
|
using NzbDrone.Core.Download.Pending;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Download
|
|
|
|
{
|
|
|
|
public interface IProcessDownloadDecisions
|
|
|
|
{
|
|
|
|
ProcessedDecisions ProcessDecisions(List<DownloadDecision> decisions);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ProcessDownloadDecisions : IProcessDownloadDecisions
|
|
|
|
{
|
|
|
|
private readonly IDownloadService _downloadService;
|
|
|
|
private readonly IPrioritizeDownloadDecision _prioritizeDownloadDecision;
|
|
|
|
private readonly IPendingReleaseService _pendingReleaseService;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
public ProcessDownloadDecisions(IDownloadService downloadService,
|
|
|
|
IPrioritizeDownloadDecision prioritizeDownloadDecision,
|
|
|
|
IPendingReleaseService pendingReleaseService,
|
|
|
|
Logger logger)
|
|
|
|
{
|
|
|
|
_downloadService = downloadService;
|
|
|
|
_prioritizeDownloadDecision = prioritizeDownloadDecision;
|
|
|
|
_pendingReleaseService = pendingReleaseService;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ProcessedDecisions ProcessDecisions(List<DownloadDecision> decisions)
|
|
|
|
{
|
2017-03-06 21:23:25 +00:00
|
|
|
var qualifiedReports = GetQualifiedReports(decisions);
|
|
|
|
var prioritizedDecisions = _prioritizeDownloadDecision.PrioritizeDecisionsForMovies(qualifiedReports);
|
2014-07-27 00:39:28 +00:00
|
|
|
var grabbed = new List<DownloadDecision>();
|
|
|
|
var pending = new List<DownloadDecision>();
|
2014-06-08 08:22:55 +00:00
|
|
|
|
2015-05-27 20:25:53 +00:00
|
|
|
foreach (var report in prioritizedDecisions)
|
2014-06-08 08:22:55 +00:00
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
var remoteMovie = report.RemoteMovie;
|
2014-06-08 08:22:55 +00:00
|
|
|
|
2018-03-14 20:41:36 +00:00
|
|
|
if (remoteMovie == null || remoteMovie.Movie == null)
|
2015-05-27 20:25:53 +00:00
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
continue;
|
2015-05-27 20:25:53 +00:00
|
|
|
}
|
2018-03-14 20:41:36 +00:00
|
|
|
|
|
|
|
List<int> movieIds = new List<int> { remoteMovie.Movie.Id };
|
|
|
|
|
|
|
|
|
|
|
|
//Skip if already grabbed
|
|
|
|
if (grabbed.Select(r => r.RemoteMovie.Movie)
|
|
|
|
.Select(e => e.Id)
|
|
|
|
.ToList()
|
|
|
|
.Intersect(movieIds)
|
|
|
|
.Any())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (report.TemporarilyRejected)
|
|
|
|
{
|
|
|
|
_pendingReleaseService.Add(report);
|
|
|
|
pending.Add(report);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (report.Rejections.Any())
|
|
|
|
{
|
|
|
|
_logger.Debug("Rejecting release {0} because {1}", report.ToString(), report.Rejections.First().Reason);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (pending.Select(r => r.RemoteMovie.Movie)
|
|
|
|
.Select(e => e.Id)
|
|
|
|
.ToList()
|
|
|
|
.Intersect(movieIds)
|
|
|
|
.Any())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
2015-05-27 20:25:53 +00:00
|
|
|
{
|
2018-03-14 20:41:36 +00:00
|
|
|
_downloadService.DownloadReport(remoteMovie, false);
|
|
|
|
grabbed.Add(report);
|
2014-06-08 08:22:55 +00:00
|
|
|
}
|
2018-03-14 20:41:36 +00:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
//TODO: support for store & forward
|
|
|
|
//We'll need to differentiate between a download client error and an indexer error
|
|
|
|
_logger.Warn(e, "Couldn't add report to download queue. " + remoteMovie);
|
|
|
|
}
|
|
|
|
|
2015-05-27 20:25:53 +00:00
|
|
|
}
|
2014-06-08 08:22:55 +00:00
|
|
|
|
2014-12-03 00:47:45 +00:00
|
|
|
return new ProcessedDecisions(grabbed, pending, decisions.Where(d => d.Rejected).ToList());
|
2014-06-08 08:22:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal List<DownloadDecision> GetQualifiedReports(IEnumerable<DownloadDecision> decisions)
|
|
|
|
{
|
|
|
|
//Process both approved and temporarily rejected
|
2017-03-06 21:23:25 +00:00
|
|
|
return decisions.Where(c => (c.Approved || c.TemporarilyRejected) && (c.RemoteMovie.Movie != null)).ToList();
|
2014-06-08 08:22:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|