2013-04-28 21:46:13 +02:00
|
|
|
using System;
|
2013-04-07 09:30:37 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2013-04-29 02:39:17 +02:00
|
|
|
using NLog;
|
2013-04-07 09:30:37 +02:00
|
|
|
using NzbDrone.Core.DecisionEngine.Specifications.Search;
|
|
|
|
using NzbDrone.Core.IndexerSearch.Definitions;
|
2013-04-15 03:41:39 +02:00
|
|
|
using NzbDrone.Core.Parser;
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
2013-05-29 23:29:51 +02:00
|
|
|
using NzbDrone.Common.Serializer;
|
2013-04-07 09:30:37 +02:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.DecisionEngine
|
|
|
|
{
|
|
|
|
public interface IMakeDownloadDecision
|
|
|
|
{
|
2013-04-28 21:46:13 +02:00
|
|
|
List<DownloadDecision> GetRssDecision(IEnumerable<ReportInfo> reports);
|
2013-06-06 16:42:23 +02:00
|
|
|
List<DownloadDecision> GetSearchDecision(IEnumerable<ReportInfo> reports, SearchCriteriaBase searchCriteriaBase);
|
2013-04-07 09:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public class DownloadDecisionMaker : IMakeDownloadDecision
|
|
|
|
{
|
|
|
|
private readonly IEnumerable<IRejectWithReason> _specifications;
|
2013-04-15 03:41:39 +02:00
|
|
|
private readonly IParsingService _parsingService;
|
2013-04-29 02:39:17 +02:00
|
|
|
private readonly Logger _logger;
|
2013-04-07 09:30:37 +02:00
|
|
|
|
2013-04-29 02:39:17 +02:00
|
|
|
public DownloadDecisionMaker(IEnumerable<IRejectWithReason> specifications, IParsingService parsingService, Logger logger)
|
2013-04-07 09:30:37 +02:00
|
|
|
{
|
|
|
|
_specifications = specifications;
|
2013-04-15 03:41:39 +02:00
|
|
|
_parsingService = parsingService;
|
2013-04-29 02:39:17 +02:00
|
|
|
_logger = logger;
|
2013-04-07 09:30:37 +02:00
|
|
|
}
|
|
|
|
|
2013-04-28 21:46:13 +02:00
|
|
|
public List<DownloadDecision> GetRssDecision(IEnumerable<ReportInfo> reports)
|
2013-04-07 09:30:37 +02:00
|
|
|
{
|
2013-04-29 02:39:17 +02:00
|
|
|
return GetDecisions(reports).ToList();
|
2013-04-07 09:30:37 +02:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:42:23 +02:00
|
|
|
public List<DownloadDecision> GetSearchDecision(IEnumerable<ReportInfo> reports, SearchCriteriaBase searchCriteriaBase)
|
2013-04-07 09:30:37 +02:00
|
|
|
{
|
2013-08-07 05:18:05 +02:00
|
|
|
return GetDecisions(reports, searchCriteriaBase).ToList();
|
2013-04-07 09:30:37 +02:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:42:23 +02:00
|
|
|
private IEnumerable<DownloadDecision> GetDecisions(IEnumerable<ReportInfo> reports, SearchCriteriaBase searchCriteria = null)
|
2013-04-28 21:46:13 +02:00
|
|
|
{
|
|
|
|
foreach (var report in reports)
|
|
|
|
{
|
2013-04-30 05:43:05 +02:00
|
|
|
DownloadDecision decision = null;
|
2013-04-28 21:46:13 +02:00
|
|
|
|
2013-04-30 05:43:05 +02:00
|
|
|
try
|
2013-04-28 21:46:13 +02:00
|
|
|
{
|
2013-04-30 05:43:05 +02:00
|
|
|
var parsedEpisodeInfo = Parser.Parser.ParseTitle(report.Title);
|
2013-04-28 21:46:13 +02:00
|
|
|
|
2013-06-28 22:23:41 +02:00
|
|
|
if (parsedEpisodeInfo != null && !string.IsNullOrWhiteSpace(parsedEpisodeInfo.SeriesTitle))
|
2013-04-28 21:46:13 +02:00
|
|
|
{
|
2013-08-19 07:53:26 +02:00
|
|
|
var remoteEpisode = _parsingService.Map(parsedEpisodeInfo, report.TvRageId);
|
2013-04-30 05:43:05 +02:00
|
|
|
remoteEpisode.Report = report;
|
|
|
|
|
|
|
|
if (remoteEpisode.Series != null)
|
|
|
|
{
|
2013-06-06 16:42:23 +02:00
|
|
|
decision = GetDecisionForReport(remoteEpisode, searchCriteria);
|
2013-04-30 05:43:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
decision = new DownloadDecision(remoteEpisode, "Unknown Series");
|
|
|
|
}
|
2013-04-28 21:46:13 +02:00
|
|
|
}
|
|
|
|
}
|
2013-04-30 05:43:05 +02:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
_logger.ErrorException("Couldn't process report.", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decision != null)
|
|
|
|
{
|
|
|
|
yield return decision;
|
|
|
|
}
|
2013-04-28 21:46:13 +02:00
|
|
|
}
|
2013-04-30 05:43:05 +02:00
|
|
|
|
2013-04-28 21:46:13 +02:00
|
|
|
}
|
|
|
|
|
2013-06-06 16:42:23 +02:00
|
|
|
private DownloadDecision GetDecisionForReport(RemoteEpisode remoteEpisode, SearchCriteriaBase searchCriteria = null)
|
2013-04-07 09:30:37 +02:00
|
|
|
{
|
2013-06-06 16:42:23 +02:00
|
|
|
var reasons = _specifications.Select(c => EvaluateSpec(c, remoteEpisode, searchCriteria))
|
2013-04-29 02:39:17 +02:00
|
|
|
.Where(c => !string.IsNullOrWhiteSpace(c));
|
|
|
|
|
|
|
|
return new DownloadDecision(remoteEpisode, reasons.ToArray());
|
|
|
|
}
|
|
|
|
|
2013-06-06 16:42:23 +02:00
|
|
|
private string EvaluateSpec(IRejectWithReason spec, RemoteEpisode remoteEpisode, SearchCriteriaBase searchCriteriaBase = null)
|
2013-04-29 02:39:17 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-04-29 03:47:06 +02:00
|
|
|
if (string.IsNullOrWhiteSpace(spec.RejectionReason))
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("[Need Rejection Text]");
|
|
|
|
}
|
|
|
|
|
2013-04-29 02:39:17 +02:00
|
|
|
var generalSpecification = spec as IDecisionEngineSpecification;
|
2013-08-07 05:18:05 +02:00
|
|
|
if (generalSpecification != null && !generalSpecification.IsSatisfiedBy(remoteEpisode, searchCriteriaBase))
|
2013-04-29 02:39:17 +02:00
|
|
|
{
|
|
|
|
return spec.RejectionReason;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2013-05-29 23:29:51 +02:00
|
|
|
e.Data.Add("report", remoteEpisode.Report.ToJson());
|
|
|
|
e.Data.Add("parsed", remoteEpisode.ParsedEpisodeInfo.ToJson());
|
2013-06-06 16:42:23 +02:00
|
|
|
_logger.ErrorException("Couldn't evaluate decision on " + remoteEpisode.Report.Title, e);
|
2013-04-29 02:39:17 +02:00
|
|
|
return string.Format("{0}: {1}", spec.GetType().Name, e.Message);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2013-04-07 09:30:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|