mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-21 05:53:33 -07:00
some preliminary work to move decision engine to use the visitor pattern.
This commit is contained in:
parent
969dff5197
commit
02d842a2b2
51 changed files with 470 additions and 507 deletions
|
@ -0,0 +1,76 @@
|
|||
using NLog;
|
||||
using NzbDrone.Core.Model;
|
||||
using NzbDrone.Core.Qualities;
|
||||
using NzbDrone.Core.Tv;
|
||||
|
||||
namespace NzbDrone.Core.DecisionEngine.Specifications
|
||||
{
|
||||
public class AcceptableSizeSpecification : IFetchableSpecification
|
||||
{
|
||||
private readonly IQualitySizeService _qualityTypeProvider;
|
||||
private readonly IEpisodeService _episodeService;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public AcceptableSizeSpecification(IQualitySizeService qualityTypeProvider, IEpisodeService episodeService, Logger logger)
|
||||
{
|
||||
_qualityTypeProvider = qualityTypeProvider;
|
||||
_episodeService = episodeService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string RejectionReason
|
||||
{
|
||||
get { return "File size too big or small"; }
|
||||
}
|
||||
|
||||
public virtual bool IsSatisfiedBy(EpisodeParseResult subject)
|
||||
{
|
||||
_logger.Trace("Beginning size check for: {0}", subject);
|
||||
|
||||
if (subject.Quality.Quality == Quality.RAWHD)
|
||||
{
|
||||
_logger.Trace("Raw-HD release found, skipping size check.");
|
||||
return true;
|
||||
}
|
||||
|
||||
var qualityType = _qualityTypeProvider.Get((int)subject.Quality.Quality);
|
||||
|
||||
if (qualityType.MaxSize == 0)
|
||||
{
|
||||
_logger.Trace("Max size is 0 (unlimited) - skipping check.");
|
||||
return true;
|
||||
}
|
||||
|
||||
var maxSize = qualityType.MaxSize.Megabytes();
|
||||
var series = subject.Series;
|
||||
|
||||
//Multiply maxSize by Series.Runtime
|
||||
maxSize = maxSize * series.Runtime;
|
||||
|
||||
//Multiply maxSize by the number of episodes parsed (if EpisodeNumbers is null it will be treated as a single episode)
|
||||
//TODO: is this check really necessary? shouldn't we blowup?
|
||||
if (subject.EpisodeNumbers != null)
|
||||
maxSize = maxSize * subject.EpisodeNumbers.Count;
|
||||
|
||||
//Check if there was only one episode parsed
|
||||
//and it is the first or last episode of the season
|
||||
if (subject.EpisodeNumbers != null && subject.EpisodeNumbers.Count == 1 &&
|
||||
_episodeService.IsFirstOrLastEpisodeOfSeason(series.Id,
|
||||
subject.SeasonNumber, subject.EpisodeNumbers[0]))
|
||||
{
|
||||
maxSize = maxSize * 2;
|
||||
}
|
||||
|
||||
//If the parsed size is greater than maxSize we don't want it
|
||||
if (subject.Size > maxSize)
|
||||
{
|
||||
_logger.Trace("Item: {0}, Size: {1} is greater than maximum allowed size ({2}), rejecting.", subject, subject.Size, maxSize);
|
||||
return false;
|
||||
}
|
||||
|
||||
_logger.Trace("Item: {0}, meets size constraints.", subject);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
using System;
|
||||
using NLog;
|
||||
using NzbDrone.Core.Configuration;
|
||||
using NzbDrone.Core.Model;
|
||||
|
||||
namespace NzbDrone.Core.DecisionEngine.Specifications
|
||||
{
|
||||
public class AllowedReleaseGroupSpecification : IFetchableSpecification
|
||||
{
|
||||
private readonly IConfigService _configService;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public AllowedReleaseGroupSpecification(IConfigService configService, Logger logger)
|
||||
{
|
||||
_configService = configService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public string RejectionReason
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Release group is blacklisted.";
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool IsSatisfiedBy(EpisodeParseResult subject)
|
||||
{
|
||||
_logger.Trace("Beginning release group check for: {0}", subject);
|
||||
|
||||
var allowed = _configService.AllowedReleaseGroups;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(allowed))
|
||||
return true;
|
||||
|
||||
foreach (var group in allowed.Trim(',', ' ').Split(','))
|
||||
{
|
||||
if (subject.ReleaseGroup.Equals(group.Trim(' '), StringComparison.CurrentCultureIgnoreCase))
|
||||
{
|
||||
_logger.Trace("Item: {0}'s release group is wanted: {1}", subject, subject.ReleaseGroup);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
_logger.Trace("Item: {0}'s release group is not wanted: {1}", subject, subject.ReleaseGroup);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Core.Model;
|
||||
|
||||
namespace NzbDrone.Core.DecisionEngine.Specifications
|
||||
{
|
||||
public class CustomStartDateSpecification : IFetchableSpecification
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
public CustomStartDateSpecification(Logger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string RejectionReason
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Aired before configured cut-off";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public virtual bool IsSatisfiedBy(EpisodeParseResult subject)
|
||||
{
|
||||
if (!subject.Series.CustomStartDate.HasValue)
|
||||
{
|
||||
_logger.Debug("{0} does not restrict downloads before date.", subject.Series.Title);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (subject.Episodes.Any(episode => episode.AirDate >= subject.Series.CustomStartDate.Value))
|
||||
{
|
||||
_logger.Debug("One or more episodes aired after cutoff, downloading.");
|
||||
return true;
|
||||
}
|
||||
|
||||
_logger.Debug("Episodes aired before cutoff date: {0}", subject.Series.CustomStartDate);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
using NLog;
|
||||
using NzbDrone.Core.Model;
|
||||
|
||||
namespace NzbDrone.Core.DecisionEngine.Specifications
|
||||
{
|
||||
public class LanguageSpecification : IFetchableSpecification
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
public LanguageSpecification(Logger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string RejectionReason
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Not English";
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool IsSatisfiedBy(EpisodeParseResult subject)
|
||||
{
|
||||
_logger.Trace("Checking if report meets language requirements. {0}", subject.Language);
|
||||
if (subject.Language != LanguageType.English)
|
||||
{
|
||||
_logger.Trace("Report Language: {0} rejected because it is not English", subject.Language);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Core.Model;
|
||||
using NzbDrone.Core.Tv;
|
||||
|
||||
namespace NzbDrone.Core.DecisionEngine.Specifications
|
||||
{
|
||||
public class MonitoredEpisodeSpecification : IFetchableSpecification
|
||||
{
|
||||
private readonly IEpisodeService _episodeService;
|
||||
private readonly ISeriesRepository _seriesRepository;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public MonitoredEpisodeSpecification(IEpisodeService episodeService, ISeriesRepository seriesRepository, Logger logger)
|
||||
{
|
||||
_episodeService = episodeService;
|
||||
_seriesRepository = seriesRepository;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string RejectionReason
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Series is not monitored";
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool IsSatisfiedBy(EpisodeParseResult subject)
|
||||
{
|
||||
var series = _seriesRepository.GetByTitle(subject.CleanTitle);
|
||||
|
||||
if (series == null)
|
||||
{
|
||||
_logger.Trace("{0} is not mapped to any series in DB. skipping", subject.CleanTitle);
|
||||
return false;
|
||||
}
|
||||
|
||||
subject.Series = series;
|
||||
|
||||
if (!series.Monitored)
|
||||
{
|
||||
_logger.Debug("{0} is present in the DB but not tracked. skipping.", subject.CleanTitle);
|
||||
return false;
|
||||
}
|
||||
|
||||
var episodes = _episodeService.GetEpisodesByParseResult(subject);
|
||||
subject.Episodes = episodes;
|
||||
|
||||
//return monitored if any of the episodes are monitored
|
||||
if (episodes.Any(episode => !episode.Ignored))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
_logger.Debug("All episodes are ignored. skipping.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
using NLog;
|
||||
using NzbDrone.Core.Download;
|
||||
using NzbDrone.Core.Model;
|
||||
|
||||
namespace NzbDrone.Core.DecisionEngine.Specifications
|
||||
{
|
||||
public class NotInQueueSpecification : IFetchableSpecification
|
||||
{
|
||||
private readonly DownloadProvider _downloadProvider;
|
||||
|
||||
|
||||
public NotInQueueSpecification(DownloadProvider downloadProvider)
|
||||
{
|
||||
_downloadProvider = downloadProvider;
|
||||
|
||||
}
|
||||
|
||||
public string RejectionReason
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Already in download queue.";
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool IsSatisfiedBy(EpisodeParseResult subject)
|
||||
{
|
||||
return !_downloadProvider.GetActiveDownloadClient().IsInQueue(subject);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
using NLog;
|
||||
using NzbDrone.Core.Model;
|
||||
|
||||
namespace NzbDrone.Core.DecisionEngine.Specifications
|
||||
{
|
||||
public class QualityAllowedByProfileSpecification : IFetchableSpecification
|
||||
{
|
||||
private readonly Logger _logger;
|
||||
|
||||
public QualityAllowedByProfileSpecification(Logger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string RejectionReason
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Quality rejected by series profile";
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool IsSatisfiedBy(EpisodeParseResult subject)
|
||||
{
|
||||
_logger.Trace("Checking if report meets quality requirements. {0}", subject.Quality);
|
||||
if (!subject.Series.QualityProfile.Allowed.Contains(subject.Quality.Quality))
|
||||
{
|
||||
_logger.Trace("Quality {0} rejected by Series' quality profile", subject.Quality);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
using NLog;
|
||||
using NzbDrone.Core.Configuration;
|
||||
using NzbDrone.Core.Model;
|
||||
|
||||
namespace NzbDrone.Core.DecisionEngine.Specifications
|
||||
{
|
||||
public class RetentionSpecification : IFetchableSpecification
|
||||
{
|
||||
private readonly IConfigService _configService;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public RetentionSpecification(IConfigService configService, Logger logger)
|
||||
{
|
||||
_configService = configService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public string RejectionReason
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Report past retention limit.";
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool IsSatisfiedBy(EpisodeParseResult subject)
|
||||
{
|
||||
_logger.Trace("Checking if report meets retention requirements. {0}", subject.Age);
|
||||
if (_configService.Retention > 0 && subject.Age > _configService.Retention)
|
||||
{
|
||||
_logger.Trace("Report age: {0} rejected by user's retention limit", subject.Age);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Core.Model;
|
||||
using NzbDrone.Core.Tv;
|
||||
|
||||
namespace NzbDrone.Core.DecisionEngine.Specifications
|
||||
{
|
||||
public class UpgradeDiskSpecification : IFetchableSpecification
|
||||
{
|
||||
private readonly QualityUpgradableSpecification _qualityUpgradableSpecification;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public UpgradeDiskSpecification(QualityUpgradableSpecification qualityUpgradableSpecification, Logger logger)
|
||||
{
|
||||
_qualityUpgradableSpecification = qualityUpgradableSpecification;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string RejectionReason
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Higher quality exists on disk";
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool IsSatisfiedBy(EpisodeParseResult subject)
|
||||
{
|
||||
foreach (var file in subject.Episodes.Select(c => c.EpisodeFile).Where(c => c != null))
|
||||
{
|
||||
_logger.Trace("Comparing file quality with report. Existing file is {0} proper:{1}", file.Quality, file.Proper);
|
||||
if (!_qualityUpgradableSpecification.IsUpgradable(subject.Series.QualityProfile, new QualityModel { Quality = file.Quality, Proper = file.Proper }, subject.Quality))
|
||||
return false;
|
||||
|
||||
if (subject.Quality.Proper && file.DateAdded < DateTime.Today.AddDays(-7))
|
||||
{
|
||||
_logger.Trace("Proper for old file, skipping: {0}", subject);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
using NLog;
|
||||
using NzbDrone.Core.History;
|
||||
using NzbDrone.Core.Model;
|
||||
|
||||
namespace NzbDrone.Core.DecisionEngine.Specifications
|
||||
{
|
||||
public class UpgradeHistorySpecification : IFetchableSpecification
|
||||
{
|
||||
private readonly IHistoryService _historyService;
|
||||
private readonly QualityUpgradableSpecification _qualityUpgradableSpecification;
|
||||
private readonly Logger _logger;
|
||||
|
||||
public UpgradeHistorySpecification(IHistoryService historyService, QualityUpgradableSpecification qualityUpgradableSpecification, Logger logger)
|
||||
{
|
||||
_historyService = historyService;
|
||||
_qualityUpgradableSpecification = qualityUpgradableSpecification;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string RejectionReason
|
||||
{
|
||||
get
|
||||
{
|
||||
return "Higher quality report exists in history";
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool IsSatisfiedBy(EpisodeParseResult subject)
|
||||
{
|
||||
foreach (var episode in subject.Episodes)
|
||||
{
|
||||
var bestQualityInHistory = _historyService.GetBestQualityInHistory(subject.Series.Id, episode.SeasonNumber, episode.EpisodeNumber);
|
||||
if (bestQualityInHistory != null)
|
||||
{
|
||||
_logger.Trace("Comparing history quality with report. History is {0}", bestQualityInHistory);
|
||||
if (!_qualityUpgradableSpecification.IsUpgradable(subject.Series.QualityProfile, bestQualityInHistory, subject.Quality))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue