2013-04-07 09:30:37 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
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-04-07 09:30:37 +02:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.DecisionEngine
|
|
|
|
{
|
|
|
|
public interface IMakeDownloadDecision
|
|
|
|
{
|
2013-04-15 03:41:39 +02:00
|
|
|
IEnumerable<DownloadDecision> GetRssDecision(IEnumerable<ReportInfo> reports);
|
|
|
|
IEnumerable<DownloadDecision> GetSearchDecision(IEnumerable<ReportInfo> reports, SearchDefinitionBase searchDefinitionBase);
|
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-07 09:30:37 +02:00
|
|
|
|
2013-04-15 03:41:39 +02:00
|
|
|
public DownloadDecisionMaker(IEnumerable<IRejectWithReason> specifications, IParsingService parsingService)
|
2013-04-07 09:30:37 +02:00
|
|
|
{
|
|
|
|
_specifications = specifications;
|
2013-04-15 03:41:39 +02:00
|
|
|
_parsingService = parsingService;
|
2013-04-07 09:30:37 +02:00
|
|
|
}
|
|
|
|
|
2013-04-15 03:41:39 +02:00
|
|
|
public IEnumerable<DownloadDecision> GetRssDecision(IEnumerable<ReportInfo> reports)
|
2013-04-07 09:30:37 +02:00
|
|
|
{
|
2013-04-15 03:41:39 +02:00
|
|
|
foreach (var report in reports)
|
2013-04-07 09:30:37 +02:00
|
|
|
{
|
2013-04-15 03:41:39 +02:00
|
|
|
var parseResult = _parsingService.Map(report);
|
|
|
|
yield return new DownloadDecision(parseResult, GetGeneralRejectionReasons(parseResult).ToArray());
|
2013-04-07 09:30:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-15 03:41:39 +02:00
|
|
|
public IEnumerable<DownloadDecision> GetSearchDecision(IEnumerable<ReportInfo> reports, SearchDefinitionBase searchDefinitionBase)
|
2013-04-07 09:30:37 +02:00
|
|
|
{
|
2013-04-15 03:41:39 +02:00
|
|
|
foreach (var report in reports)
|
2013-04-07 09:30:37 +02:00
|
|
|
{
|
2013-04-15 03:41:39 +02:00
|
|
|
var parseResult = _parsingService.Map(report);
|
2013-04-07 09:30:37 +02:00
|
|
|
var generalReasons = GetGeneralRejectionReasons(parseResult);
|
|
|
|
var searchReasons = GetSearchRejectionReasons(parseResult, searchDefinitionBase);
|
|
|
|
|
2013-04-15 03:41:39 +02:00
|
|
|
yield return new DownloadDecision(parseResult, generalReasons.Union(searchReasons).ToArray());
|
2013-04-07 09:30:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-15 03:41:39 +02:00
|
|
|
private IEnumerable<string> GetGeneralRejectionReasons(RemoteEpisode report)
|
2013-04-07 09:30:37 +02:00
|
|
|
{
|
|
|
|
return _specifications
|
|
|
|
.OfType<IDecisionEngineSpecification>()
|
2013-04-15 03:41:39 +02:00
|
|
|
.Where(spec => !spec.IsSatisfiedBy(report))
|
2013-04-07 09:30:37 +02:00
|
|
|
.Select(spec => spec.RejectionReason);
|
|
|
|
}
|
|
|
|
|
2013-04-15 03:41:39 +02:00
|
|
|
private IEnumerable<string> GetSearchRejectionReasons(RemoteEpisode report, SearchDefinitionBase searchDefinitionBase)
|
2013-04-07 09:30:37 +02:00
|
|
|
{
|
|
|
|
return _specifications
|
|
|
|
.OfType<IDecisionEngineSearchSpecification>()
|
2013-04-15 03:41:39 +02:00
|
|
|
.Where(spec => !spec.IsSatisfiedBy(report, searchDefinitionBase))
|
2013-04-07 09:30:37 +02:00
|
|
|
.Select(spec => spec.RejectionReason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|