1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-29 02:57:15 +02:00
Sonarr/src/NzbDrone.Core/DecisionEngine/DownloadDecisionMaker.cs

145 lines
5.4 KiB
C#
Raw Normal View History

using System;
2013-04-07 10:30:37 +03:00
using System.Collections.Generic;
using System.Linq;
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;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
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;
private readonly IParsingService _parsingService;
private readonly Logger _logger;
2013-04-07 10:30:37 +03:00
public DownloadDecisionMaker(IEnumerable<IDecisionEngineSpecification> specifications, IParsingService parsingService, Logger logger)
2013-04-07 10:30:37 +03:00
{
_specifications = specifications;
_parsingService = parsingService;
_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
{
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
{
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)
{
if (reports.Any())
{
2013-09-11 09:33:47 +03:00
_logger.ProgressInfo("Processing {0} reports", reports.Count);
}
else
{
2013-09-11 09:33:47 +03:00
_logger.ProgressInfo("No reports found");
}
var reportNumber = 1;
foreach (var report in reports)
{
DownloadDecision decision = null;
2013-09-11 09:33:47 +03:00
_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 && !string.IsNullOrWhiteSpace(parsedEpisodeInfo.SeriesTitle))
{
var remoteEpisode = _parsingService.Map(parsedEpisodeInfo, report.TvRageId, searchCriteria);
2013-09-14 02:17:58 +03:00
remoteEpisode.Release = report;
if (remoteEpisode.Series != null)
{
2013-09-21 08:49:54 +03:00
remoteEpisode.DownloadAllowed = true;
decision = GetDecisionForReport(remoteEpisode, searchCriteria);
}
else
{
decision = new DownloadDecision(remoteEpisode, "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;
}
}
}
private DownloadDecision GetDecisionForReport(RemoteEpisode remoteEpisode, SearchCriteriaBase searchCriteria = null)
2013-04-07 10:30:37 +03:00
{
var reasons = _specifications.Select(c => EvaluateSpec(c, remoteEpisode, searchCriteria))
.Where(c => !string.IsNullOrWhiteSpace(c));
return new DownloadDecision(remoteEpisode, reasons.ToArray());
}
private string EvaluateSpec(IRejectWithReason spec, RemoteEpisode remoteEpisode, SearchCriteriaBase searchCriteriaBase = null)
{
try
{
if (string.IsNullOrWhiteSpace(spec.RejectionReason))
{
throw new InvalidOperationException("[Need Rejection Text]");
}
var generalSpecification = spec as IDecisionEngineSpecification;
if (generalSpecification != null && !generalSpecification.IsSatisfiedBy(remoteEpisode, searchCriteriaBase))
{
return spec.RejectionReason;
}
}
catch (Exception e)
{
2013-09-14 02:17:58 +03:00
e.Data.Add("report", remoteEpisode.Release.ToJson());
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);
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
}