Sonarr/NzbDrone.Core/DecisionEngine/DownloadDecisionMaker.cs

83 lines
3.2 KiB
C#
Raw Normal View History

using System;
2013-04-07 07:30:37 +00:00
using System.Collections.Generic;
using System.Linq;
using NzbDrone.Core.DecisionEngine.Specifications.Search;
using NzbDrone.Core.IndexerSearch.Definitions;
using NzbDrone.Core.Parser;
using NzbDrone.Core.Parser.Model;
2013-04-07 07:30:37 +00: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 07:30:37 +00:00
}
public class DownloadDecisionMaker : IMakeDownloadDecision
{
private readonly IEnumerable<IRejectWithReason> _specifications;
private readonly IParsingService _parsingService;
2013-04-07 07:30:37 +00:00
public DownloadDecisionMaker(IEnumerable<IRejectWithReason> specifications, IParsingService parsingService)
2013-04-07 07:30:37 +00:00
{
_specifications = specifications;
_parsingService = parsingService;
2013-04-07 07:30:37 +00:00
}
public List<DownloadDecision> GetRssDecision(IEnumerable<ReportInfo> reports)
2013-04-07 07:30:37 +00:00
{
return GetDecisions(reports, GetGeneralRejectionReasons).ToList();
2013-04-07 07:30:37 +00:00
}
public List<DownloadDecision> GetSearchDecision(IEnumerable<ReportInfo> reports, SearchDefinitionBase searchDefinitionBase)
2013-04-07 07:30:37 +00:00
{
return GetDecisions(reports, remoteEpisode =>
{
var generalReasons = GetGeneralRejectionReasons(remoteEpisode);
var searchReasons = GetSearchRejectionReasons(remoteEpisode, searchDefinitionBase);
return generalReasons.Union(searchReasons);
}).ToList();
2013-04-07 07:30:37 +00:00
}
private IEnumerable<string> GetGeneralRejectionReasons(RemoteEpisode report)
2013-04-07 07:30:37 +00:00
{
return _specifications
.OfType<IDecisionEngineSpecification>()
.Where(spec => !spec.IsSatisfiedBy(report))
2013-04-07 07:30:37 +00:00
.Select(spec => spec.RejectionReason);
}
private IEnumerable<DownloadDecision> GetDecisions(IEnumerable<ReportInfo> reports, Func<RemoteEpisode, IEnumerable<string>> decisionCallback)
{
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 new DownloadDecision(remoteEpisode, decisionCallback(remoteEpisode).ToArray());
}
else
{
yield return new DownloadDecision(remoteEpisode, "Unknown Series");
}
}
}
}
private IEnumerable<string> GetSearchRejectionReasons(RemoteEpisode report, SearchDefinitionBase searchDefinitionBase)
2013-04-07 07:30:37 +00:00
{
return _specifications
.OfType<IDecisionEngineSearchSpecification>()
.Where(spec => !spec.IsSatisfiedBy(report, searchDefinitionBase))
2013-04-07 07:30:37 +00:00
.Select(spec => spec.RejectionReason);
}
}
}