|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common;
|
|
|
|
using NzbDrone.Common.Instrumentation.Extensions;
|
|
|
|
using NzbDrone.Common.Serializer;
|
|
|
|
using NzbDrone.Core.IndexerSearch.Definitions;
|
|
|
|
using NzbDrone.Core.Parser;
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.DecisionEngine
|
|
|
|
{
|
|
|
|
public interface IMakeDownloadDecision
|
|
|
|
{
|
|
|
|
List<DownloadDecision> GetRssDecision(List<ReleaseInfo> reports);
|
|
|
|
List<DownloadDecision> GetSearchDecision(List<ReleaseInfo> reports, SearchCriteriaBase searchCriteriaBase);
|
|
|
|
DownloadDecision GetDecisionForReport(RemoteEpisode remoteEpisode, SearchCriteriaBase searchCriteria = null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class DownloadDecisionMaker : IMakeDownloadDecision
|
|
|
|
{
|
|
|
|
private readonly IEnumerable<IRejectWithReason> _specifications;
|
|
|
|
private readonly IParsingService _parsingService;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
public DownloadDecisionMaker(IEnumerable<IDecisionEngineSpecification> specifications, IParsingService parsingService, Logger logger)
|
|
|
|
{
|
|
|
|
_specifications = specifications;
|
|
|
|
_parsingService = parsingService;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<DownloadDecision> GetRssDecision(List<ReleaseInfo> reports)
|
|
|
|
{
|
|
|
|
return GetDecisions(reports).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<DownloadDecision> GetSearchDecision(List<ReleaseInfo> reports, SearchCriteriaBase searchCriteriaBase)
|
|
|
|
{
|
|
|
|
return GetDecisions(reports, searchCriteriaBase).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerable<DownloadDecision> GetDecisions(List<ReleaseInfo> reports, SearchCriteriaBase searchCriteria = null)
|
|
|
|
{
|
|
|
|
if (reports.Any())
|
|
|
|
{
|
|
|
|
_logger.ProgressInfo("Processing {0} reports", reports.Count);
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.ProgressInfo("No reports found");
|
|
|
|
}
|
|
|
|
|
|
|
|
var reportNumber = 1;
|
|
|
|
|
|
|
|
foreach (var report in reports)
|
|
|
|
{
|
|
|
|
DownloadDecision decision = null;
|
|
|
|
_logger.ProgressTrace("Processing report {0}/{1}", reportNumber, reports.Count);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var parsedEpisodeInfo = Parser.Parser.ParseTitle(report.Title);
|
|
|
|
|
|
|
|
if (parsedEpisodeInfo == null || parsedEpisodeInfo.IsPossibleSpecialEpisode)
|
|
|
|
{
|
|
|
|
var specialEpisodeInfo = _parsingService.ParseSpecialEpisodeTitle(report.Title, report.TvRageId, searchCriteria);
|
|
|
|
|
|
|
|
if (specialEpisodeInfo != null)
|
|
|
|
{
|
|
|
|
parsedEpisodeInfo = specialEpisodeInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parsedEpisodeInfo != null && !parsedEpisodeInfo.SeriesTitle.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
var remoteEpisode = _parsingService.Map(parsedEpisodeInfo, report.TvRageId, searchCriteria);
|
|
|
|
remoteEpisode.Release = report;
|
|
|
|
|
|
|
|
if (remoteEpisode.Series != null)
|
|
|
|
{
|
|
|
|
remoteEpisode.DownloadAllowed = true;
|
|
|
|
decision = GetDecisionForReport(remoteEpisode, searchCriteria);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
decision = new DownloadDecision(remoteEpisode, new Rejection("Unknown Series"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_logger.ErrorException("Couldn't process report.", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
reportNumber++;
|
|
|
|
|
|
|
|
if (decision != null)
|
|
|
|
{
|
|
|
|
if (decision.Rejections.Any())
|
|
|
|
{
|
|
|
|
_logger.Debug("Release rejected for the following reasons: {0}", String.Join(", ", decision.Rejections));
|
|
|
|
}
|
|
|
|
|
|
|
|
yield return decision;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public DownloadDecision GetDecisionForReport(RemoteEpisode remoteEpisode, SearchCriteriaBase searchCriteria = null)
|
|
|
|
{
|
|
|
|
var reasons = _specifications.Select(c => EvaluateSpec(c, remoteEpisode, searchCriteria))
|
|
|
|
.Where(c => c != null);
|
|
|
|
|
|
|
|
return new DownloadDecision(remoteEpisode, reasons.ToArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
private Rejection EvaluateSpec(IRejectWithReason spec, RemoteEpisode remoteEpisode, SearchCriteriaBase searchCriteriaBase = null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (spec.RejectionReason.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("[Need Rejection Text]");
|
|
|
|
}
|
|
|
|
|
|
|
|
var generalSpecification = spec as IDecisionEngineSpecification;
|
|
|
|
if (generalSpecification != null && !generalSpecification.IsSatisfiedBy(remoteEpisode, searchCriteriaBase))
|
|
|
|
{
|
|
|
|
return new Rejection(spec.RejectionReason, generalSpecification.Type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
e.Data.Add("report", remoteEpisode.Release.ToJson());
|
|
|
|
e.Data.Add("parsed", remoteEpisode.ParsedEpisodeInfo.ToJson());
|
|
|
|
_logger.ErrorException("Couldn't evaluate decision on " + remoteEpisode.Release.Title, e);
|
|
|
|
return new Rejection(String.Format("{0}: {1}", spec.GetType().Name, e.Message));
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|