2013-04-28 22:46:13 +03:00
|
|
|
using System;
|
2013-04-07 10:30:37 +03:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2013-04-29 03:39:17 +03:00
|
|
|
using NLog;
|
2013-09-12 02:33:34 +03:00
|
|
|
using NzbDrone.Core.DecisionEngine.Specifications;
|
2013-04-07 10:30:37 +03:00
|
|
|
using NzbDrone.Core.IndexerSearch.Definitions;
|
2014-03-13 23:12:42 +03:00
|
|
|
using NzbDrone.Core.Instrumentation.Extensions;
|
2013-04-15 04:41:39 +03:00
|
|
|
using NzbDrone.Core.Parser;
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
2013-05-30 00:29:51 +03:00
|
|
|
using NzbDrone.Common.Serializer;
|
2013-04-07 10:30:37 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.DecisionEngine
|
|
|
|
{
|
|
|
|
public interface IMakeDownloadDecision
|
|
|
|
{
|
2013-09-14 02:25:30 +03:00
|
|
|
List<DownloadDecision> GetRssDecision(List<ReleaseInfo> reports);
|
|
|
|
List<DownloadDecision> GetSearchDecision(List<ReleaseInfo> reports, SearchCriteriaBase searchCriteriaBase);
|
2013-04-07 10:30:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public class DownloadDecisionMaker : IMakeDownloadDecision
|
|
|
|
{
|
|
|
|
private readonly IEnumerable<IRejectWithReason> _specifications;
|
2013-04-15 04:41:39 +03:00
|
|
|
private readonly IParsingService _parsingService;
|
2013-04-29 03:39:17 +03:00
|
|
|
private readonly Logger _logger;
|
2013-04-07 10:30:37 +03:00
|
|
|
|
2014-02-22 11:53:29 +03:00
|
|
|
public DownloadDecisionMaker(IEnumerable<IDecisionEngineSpecification> specifications, IParsingService parsingService, Logger logger)
|
2013-04-07 10:30:37 +03:00
|
|
|
{
|
|
|
|
_specifications = specifications;
|
2013-04-15 04:41:39 +03:00
|
|
|
_parsingService = parsingService;
|
2013-04-29 03:39:17 +03:00
|
|
|
_logger = logger;
|
2013-04-07 10:30:37 +03:00
|
|
|
}
|
|
|
|
|
2013-09-14 02:25:30 +03:00
|
|
|
public List<DownloadDecision> GetRssDecision(List<ReleaseInfo> reports)
|
2013-04-07 10:30:37 +03:00
|
|
|
{
|
2013-04-29 03:39:17 +03:00
|
|
|
return GetDecisions(reports).ToList();
|
2013-04-07 10:30:37 +03:00
|
|
|
}
|
|
|
|
|
2013-09-14 02:25:30 +03:00
|
|
|
public List<DownloadDecision> GetSearchDecision(List<ReleaseInfo> reports, SearchCriteriaBase searchCriteriaBase)
|
2013-04-07 10:30:37 +03:00
|
|
|
{
|
2013-08-07 06:18:05 +03:00
|
|
|
return GetDecisions(reports, searchCriteriaBase).ToList();
|
2013-04-07 10:30:37 +03:00
|
|
|
}
|
|
|
|
|
2013-09-14 02:25:30 +03:00
|
|
|
private IEnumerable<DownloadDecision> GetDecisions(List<ReleaseInfo> reports, SearchCriteriaBase searchCriteria = null)
|
2013-04-28 22:46:13 +03:00
|
|
|
{
|
2013-09-04 08:01:28 +03:00
|
|
|
if (reports.Any())
|
|
|
|
{
|
2013-09-11 09:33:47 +03:00
|
|
|
_logger.ProgressInfo("Processing {0} reports", reports.Count);
|
2013-09-04 08:01:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2013-09-11 09:33:47 +03:00
|
|
|
_logger.ProgressInfo("No reports found");
|
2013-09-04 08:01:28 +03:00
|
|
|
}
|
2013-09-05 20:45:12 +03:00
|
|
|
|
|
|
|
var reportNumber = 1;
|
|
|
|
|
2013-04-28 22:46:13 +03:00
|
|
|
foreach (var report in reports)
|
|
|
|
{
|
2013-04-30 06:43:05 +03:00
|
|
|
DownloadDecision decision = null;
|
2013-09-11 09:33:47 +03:00
|
|
|
_logger.ProgressTrace("Processing report {0}/{1}", reportNumber, reports.Count);
|
2013-04-28 22:46:13 +03:00
|
|
|
|
2013-04-30 06:43:05 +03:00
|
|
|
try
|
2013-04-28 22:46:13 +03:00
|
|
|
{
|
2013-04-30 06:43:05 +03:00
|
|
|
var parsedEpisodeInfo = Parser.Parser.ParseTitle(report.Title);
|
2013-04-28 22:46:13 +03:00
|
|
|
|
2014-01-07 11:24:50 +03:00
|
|
|
if (parsedEpisodeInfo == null || parsedEpisodeInfo.IsPossibleSpecialEpisode())
|
|
|
|
{
|
2014-01-08 08:54:23 +03:00
|
|
|
var specialEpisodeInfo = _parsingService.ParseSpecialEpisodeTitle(report.Title, report.TvRageId, searchCriteria);
|
2014-01-07 11:24:50 +03:00
|
|
|
if (specialEpisodeInfo != null)
|
|
|
|
{
|
|
|
|
parsedEpisodeInfo = specialEpisodeInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-28 23:23:41 +03:00
|
|
|
if (parsedEpisodeInfo != null && !string.IsNullOrWhiteSpace(parsedEpisodeInfo.SeriesTitle))
|
2013-04-28 22:46:13 +03:00
|
|
|
{
|
2013-09-15 06:49:58 +03:00
|
|
|
var remoteEpisode = _parsingService.Map(parsedEpisodeInfo, report.TvRageId, searchCriteria);
|
2013-09-14 02:17:58 +03:00
|
|
|
remoteEpisode.Release = report;
|
2013-04-30 06:43:05 +03:00
|
|
|
|
|
|
|
if (remoteEpisode.Series != null)
|
|
|
|
{
|
2013-09-21 08:49:54 +03:00
|
|
|
remoteEpisode.DownloadAllowed = true;
|
2013-06-06 17:42:23 +03:00
|
|
|
decision = GetDecisionForReport(remoteEpisode, searchCriteria);
|
2013-04-30 06:43:05 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
decision = new DownloadDecision(remoteEpisode, "Unknown Series");
|
|
|
|
}
|
2013-04-28 22:46:13 +03:00
|
|
|
}
|
|
|
|
}
|
2013-04-30 06:43:05 +03:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_logger.ErrorException("Couldn't process report.", e);
|
|
|
|
}
|
|
|
|
|
2013-09-05 20:45:12 +03:00
|
|
|
reportNumber++;
|
|
|
|
|
2013-04-30 06:43:05 +03:00
|
|
|
if (decision != null)
|
|
|
|
{
|
2014-03-21 02:52:19 +03:00
|
|
|
if (decision.Rejections.Any())
|
|
|
|
{
|
|
|
|
_logger.Debug("Release rejected for the following reasons: {0}", String.Join(", ", decision.Rejections));
|
|
|
|
}
|
|
|
|
|
2013-04-30 06:43:05 +03:00
|
|
|
yield return decision;
|
|
|
|
}
|
2013-04-28 22:46:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-06 17:42:23 +03:00
|
|
|
private DownloadDecision GetDecisionForReport(RemoteEpisode remoteEpisode, SearchCriteriaBase searchCriteria = null)
|
2013-04-07 10:30:37 +03:00
|
|
|
{
|
2013-06-06 17:42:23 +03:00
|
|
|
var reasons = _specifications.Select(c => EvaluateSpec(c, remoteEpisode, searchCriteria))
|
2014-02-22 11:53:29 +03:00
|
|
|
.Where(c => !string.IsNullOrWhiteSpace(c));
|
2013-04-29 03:39:17 +03:00
|
|
|
|
|
|
|
return new DownloadDecision(remoteEpisode, reasons.ToArray());
|
|
|
|
}
|
|
|
|
|
2013-06-06 17:42:23 +03:00
|
|
|
private string EvaluateSpec(IRejectWithReason spec, RemoteEpisode remoteEpisode, SearchCriteriaBase searchCriteriaBase = null)
|
2013-04-29 03:39:17 +03:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-04-29 04:47:06 +03:00
|
|
|
if (string.IsNullOrWhiteSpace(spec.RejectionReason))
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("[Need Rejection Text]");
|
|
|
|
}
|
|
|
|
|
2013-04-29 03:39:17 +03:00
|
|
|
var generalSpecification = spec as IDecisionEngineSpecification;
|
2013-08-07 06:18:05 +03:00
|
|
|
if (generalSpecification != null && !generalSpecification.IsSatisfiedBy(remoteEpisode, searchCriteriaBase))
|
2013-04-29 03:39:17 +03:00
|
|
|
{
|
|
|
|
return spec.RejectionReason;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2013-09-14 02:17:58 +03:00
|
|
|
e.Data.Add("report", remoteEpisode.Release.ToJson());
|
2013-05-30 00:29:51 +03:00
|
|
|
e.Data.Add("parsed", remoteEpisode.ParsedEpisodeInfo.ToJson());
|
2013-09-14 02:17:58 +03:00
|
|
|
_logger.ErrorException("Couldn't evaluate decision on " + remoteEpisode.Release.Title, e);
|
2013-04-29 03:39:17 +03:00
|
|
|
return string.Format("{0}: {1}", spec.GetType().Name, e.Message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2013-04-07 10:30:37 +03:00
|
|
|
}
|
|
|
|
}
|
2013-09-14 02:17:58 +03:00
|
|
|
}
|