Sonarr/NzbDrone.Core/DecisionEngine/DownloadDecisionMaker.cs

69 lines
2.6 KiB
C#
Raw Normal View History

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
{
IEnumerable<DownloadDecision> GetRssDecision(IEnumerable<ReportInfo> reports);
IEnumerable<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 IEnumerable<DownloadDecision> GetRssDecision(IEnumerable<ReportInfo> reports)
2013-04-07 07:30:37 +00:00
{
foreach (var report in reports)
2013-04-07 07:30:37 +00:00
{
var parseResult = _parsingService.Map(report);
if (parseResult != null)
{
2013-04-07 07:30:37 +00:00
yield return new DownloadDecision(parseResult, GetGeneralRejectionReasons(parseResult).ToArray());
}
}
2013-04-07 07:30:37 +00:00
}
public IEnumerable<DownloadDecision> GetSearchDecision(IEnumerable<ReportInfo> reports, SearchDefinitionBase searchDefinitionBase)
2013-04-07 07:30:37 +00:00
{
foreach (var report in reports)
2013-04-07 07:30:37 +00:00
{
var remoteEpisode = _parsingService.Map(report);
var generalReasons = GetGeneralRejectionReasons(remoteEpisode);
var searchReasons = GetSearchRejectionReasons(remoteEpisode, searchDefinitionBase);
2013-04-07 07:30:37 +00:00
yield return new DownloadDecision(remoteEpisode, generalReasons.Union(searchReasons).ToArray());
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<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);
}
}
}