1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 00:12:30 +01:00
Sonarr/NzbDrone.Core/DecisionEngine/DownloadDecisionMaker.cs

105 lines
3.9 KiB
C#
Raw Normal View History

using System;
2013-04-07 09:30:37 +02:00
using System.Collections.Generic;
using System.Linq;
using NLog;
2013-04-07 09:30:37 +02:00
using NzbDrone.Core.DecisionEngine.Specifications.Search;
using NzbDrone.Core.IndexerSearch.Definitions;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
2013-04-07 09:30:37 +02:00
namespace NzbDrone.Core.DecisionEngine
{
public interface IMakeDownloadDecision
{
List<DownloadDecision> GetRssDecision(IEnumerable<ReportInfo> reports);
List<DownloadDecision> GetSearchDecision(IEnumerable<ReportInfo> reports, SearchDefinitionBase searchDefinitionBase);
2013-04-07 09:30:37 +02:00
}
public class DownloadDecisionMaker : IMakeDownloadDecision
{
private readonly IEnumerable<IRejectWithReason> _specifications;
private readonly IParsingService _parsingService;
private readonly Logger _logger;
2013-04-07 09:30:37 +02:00
public DownloadDecisionMaker(IEnumerable<IRejectWithReason> specifications, IParsingService parsingService, Logger logger)
2013-04-07 09:30:37 +02:00
{
_specifications = specifications;
_parsingService = parsingService;
_logger = logger;
2013-04-07 09:30:37 +02:00
}
public List<DownloadDecision> GetRssDecision(IEnumerable<ReportInfo> reports)
2013-04-07 09:30:37 +02:00
{
return GetDecisions(reports).ToList();
2013-04-07 09:30:37 +02:00
}
public List<DownloadDecision> GetSearchDecision(IEnumerable<ReportInfo> reports, SearchDefinitionBase searchDefinitionBase)
2013-04-07 09:30:37 +02:00
{
return GetDecisions(reports).ToList();
2013-04-07 09:30:37 +02:00
}
private IEnumerable<DownloadDecision> GetDecisions(IEnumerable<ReportInfo> reports, SearchDefinitionBase searchDefinition = null)
{
foreach (var report in reports)
{
var parsedEpisodeInfo = Parser.Parser.ParseTitle(report.Title);
if (parsedEpisodeInfo != null)
{
var remoteEpisode = _parsingService.Map(parsedEpisodeInfo);
remoteEpisode.Report = report;
if (remoteEpisode.Series != null)
{
yield return GetDecisionForReport(remoteEpisode, searchDefinition);
}
else
{
yield return new DownloadDecision(remoteEpisode, "Unknown Series");
}
}
}
}
private DownloadDecision GetDecisionForReport(RemoteEpisode remoteEpisode, SearchDefinitionBase searchDefinition = null)
2013-04-07 09:30:37 +02:00
{
var reasons = _specifications.Select(c => EvaluateSpec(c, remoteEpisode, searchDefinition))
.Where(c => !string.IsNullOrWhiteSpace(c));
return new DownloadDecision(remoteEpisode, reasons.ToArray());
}
private string EvaluateSpec(IRejectWithReason spec, RemoteEpisode remoteEpisode, SearchDefinitionBase searchDefinitionBase = null)
{
try
{
if (string.IsNullOrWhiteSpace(spec.RejectionReason))
{
throw new InvalidOperationException("[Need Rejection Text]");
}
var searchSpecification = spec as IDecisionEngineSearchSpecification;
if (searchSpecification != null && searchDefinitionBase != null)
{
if (!searchSpecification.IsSatisfiedBy(remoteEpisode, searchDefinitionBase))
{
return spec.RejectionReason;
}
}
var generalSpecification = spec as IDecisionEngineSpecification;
if (generalSpecification != null && !generalSpecification.IsSatisfiedBy(remoteEpisode))
{
return spec.RejectionReason;
}
}
catch (Exception e)
{
_logger.ErrorException("Couldn't evaluate decision", e);
return string.Format("{0}: {1}", spec.GetType().Name, e.Message);
}
return null;
2013-04-07 09:30:37 +02:00
}
}
}