2013-04-28 19:46:13 +00:00
|
|
|
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;
|
2013-04-15 01:41:39 +00:00
|
|
|
using NzbDrone.Core.Parser;
|
|
|
|
using NzbDrone.Core.Parser.Model;
|
2013-04-07 07:30:37 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.DecisionEngine
|
|
|
|
{
|
|
|
|
public interface IMakeDownloadDecision
|
|
|
|
{
|
2013-04-28 19:46:13 +00:00
|
|
|
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;
|
2013-04-15 01:41:39 +00:00
|
|
|
private readonly IParsingService _parsingService;
|
2013-04-07 07:30:37 +00:00
|
|
|
|
2013-04-15 01:41:39 +00:00
|
|
|
public DownloadDecisionMaker(IEnumerable<IRejectWithReason> specifications, IParsingService parsingService)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
|
|
|
_specifications = specifications;
|
2013-04-15 01:41:39 +00:00
|
|
|
_parsingService = parsingService;
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
|
|
|
|
2013-04-28 19:46:13 +00:00
|
|
|
public List<DownloadDecision> GetRssDecision(IEnumerable<ReportInfo> reports)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-04-28 19:46:13 +00:00
|
|
|
return GetDecisions(reports, GetGeneralRejectionReasons).ToList();
|
2013-04-07 07:30:37 +00:00
|
|
|
}
|
|
|
|
|
2013-04-28 19:46:13 +00:00
|
|
|
public List<DownloadDecision> GetSearchDecision(IEnumerable<ReportInfo> reports, SearchDefinitionBase searchDefinitionBase)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
2013-04-28 19:46:13 +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
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-15 01:41:39 +00:00
|
|
|
private IEnumerable<string> GetGeneralRejectionReasons(RemoteEpisode report)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
|
|
|
return _specifications
|
|
|
|
.OfType<IDecisionEngineSpecification>()
|
2013-04-15 01:41:39 +00:00
|
|
|
.Where(spec => !spec.IsSatisfiedBy(report))
|
2013-04-07 07:30:37 +00:00
|
|
|
.Select(spec => spec.RejectionReason);
|
|
|
|
}
|
|
|
|
|
2013-04-28 19:46:13 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-15 01:41:39 +00:00
|
|
|
private IEnumerable<string> GetSearchRejectionReasons(RemoteEpisode report, SearchDefinitionBase searchDefinitionBase)
|
2013-04-07 07:30:37 +00:00
|
|
|
{
|
|
|
|
return _specifications
|
|
|
|
.OfType<IDecisionEngineSearchSpecification>()
|
2013-04-15 01:41:39 +00:00
|
|
|
.Where(spec => !spec.IsSatisfiedBy(report, searchDefinitionBase))
|
2013-04-07 07:30:37 +00:00
|
|
|
.Select(spec => spec.RejectionReason);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|