2013-04-15 01:41:39 +00:00
using System.Linq ;
2012-02-07 05:08:07 +00:00
using NLog ;
2014-10-27 05:51:50 +00:00
using NzbDrone.Common.Extensions ;
2013-08-07 03:18:05 +00:00
using NzbDrone.Core.IndexerSearch.Definitions ;
2013-04-15 01:41:39 +00:00
using NzbDrone.Core.Parser.Model ;
2013-02-27 03:19:22 +00:00
using NzbDrone.Core.Qualities ;
2013-02-19 06:01:03 +00:00
using NzbDrone.Core.Tv ;
2014-05-02 22:04:58 +00:00
using System.Collections.Generic ;
2012-02-07 05:08:07 +00:00
2013-03-07 00:19:49 +00:00
namespace NzbDrone.Core.DecisionEngine.Specifications
2012-02-07 05:08:07 +00:00
{
2013-04-07 07:30:37 +00:00
public class AcceptableSizeSpecification : IDecisionEngineSpecification
2012-02-07 05:08:07 +00:00
{
2014-01-18 11:44:36 +00:00
private readonly IQualityDefinitionService _qualityDefinitionService ;
2013-02-22 00:47:09 +00:00
private readonly IEpisodeService _episodeService ;
2013-03-07 00:19:49 +00:00
private readonly Logger _logger ;
2012-02-07 05:08:07 +00:00
2014-01-18 11:44:36 +00:00
public AcceptableSizeSpecification ( IQualityDefinitionService qualityDefinitionService , IEpisodeService episodeService , Logger logger )
2012-02-07 05:08:07 +00:00
{
2014-01-18 11:44:36 +00:00
_qualityDefinitionService = qualityDefinitionService ;
2013-02-20 02:05:15 +00:00
_episodeService = episodeService ;
2013-03-07 00:19:49 +00:00
_logger = logger ;
2012-02-07 05:08:07 +00:00
}
2014-06-08 08:22:55 +00:00
public RejectionType Type { get { return RejectionType . Permanent ; } }
2014-10-27 05:51:50 +00:00
public Decision IsSatisfiedBy ( RemoteEpisode subject , SearchCriteriaBase searchCriteria )
2012-02-07 05:08:07 +00:00
{
2014-03-13 20:12:42 +00:00
_logger . Debug ( "Beginning size check for: {0}" , subject ) ;
2012-02-07 05:08:07 +00:00
2013-06-06 23:05:06 +00:00
var quality = subject . ParsedEpisodeInfo . Quality . Quality ;
2014-12-15 18:52:16 +00:00
if ( subject . ParsedEpisodeInfo . Special )
{
_logger . Debug ( "Special release found, skipping size check." ) ;
2014-10-27 05:51:50 +00:00
return Decision . Accept ( ) ;
2013-01-09 08:15:06 +00:00
}
2015-05-14 20:57:45 +00:00
if ( subject . Release . Size = = 0 )
{
_logger . Debug ( "Release has unknown size, skipping size check." ) ;
return Decision . Accept ( ) ;
}
2014-01-18 11:44:36 +00:00
var qualityDefinition = _qualityDefinitionService . Get ( quality ) ;
2015-05-19 22:57:01 +00:00
if ( qualityDefinition . MinSize . HasValue )
{
var minSize = qualityDefinition . MinSize . Value . Megabytes ( ) ;
2012-02-07 05:08:07 +00:00
2015-05-19 22:57:01 +00:00
//Multiply maxSize by Series.Runtime
minSize = minSize * subject . Series . Runtime * subject . Episodes . Count ;
2012-02-07 05:08:07 +00:00
2015-05-19 22:57:01 +00:00
//If the parsed size is smaller than minSize we don't want it
if ( subject . Release . Size < minSize )
{
2016-09-22 20:38:38 +00:00
var runtimeMessage = subject . Episodes . Count = = 1 ? $"{subject.Series.Runtime}min" : $"{subject.Episodes.Count}x {subject.Series.Runtime}min" ;
_logger . Debug ( "Item: {0}, Size: {1} is smaller than minimum allowed size ({2} bytes for {3}), rejecting." , subject , subject . Release . Size , minSize , runtimeMessage ) ;
return Decision . Reject ( "{0} is smaller than minimum allowed {1} (for {2})" , subject . Release . Size . SizeSuffix ( ) , minSize . SizeSuffix ( ) , runtimeMessage ) ;
2015-05-19 22:57:01 +00:00
}
2014-02-15 10:51:52 +00:00
}
2015-12-27 20:03:27 +00:00
if ( ! qualityDefinition . MaxSize . HasValue | | qualityDefinition . MaxSize . Value = = 0 )
2012-02-07 05:08:07 +00:00
{
2015-05-19 22:57:01 +00:00
_logger . Debug ( "Max size is unlimited - skipping check." ) ;
2012-02-07 05:08:07 +00:00
}
2014-02-15 10:51:52 +00:00
else
2012-04-17 03:42:18 +00:00
{
2015-05-19 22:57:01 +00:00
var maxSize = qualityDefinition . MaxSize . Value . Megabytes ( ) ;
2014-02-15 10:51:52 +00:00
//Multiply maxSize by Series.Runtime
maxSize = maxSize * subject . Series . Runtime * subject . Episodes . Count ;
2014-05-02 22:04:58 +00:00
if ( subject . Episodes . Count = = 1 )
2014-02-15 10:51:52 +00:00
{
2014-05-02 22:04:58 +00:00
Episode episode = subject . Episodes . First ( ) ;
List < Episode > seasonEpisodes ;
var seasonSearchCriteria = searchCriteria as SeasonSearchCriteria ;
if ( seasonSearchCriteria ! = null & & ! seasonSearchCriteria . Series . UseSceneNumbering & & seasonSearchCriteria . Episodes . Any ( v = > v . Id = = episode . Id ) )
{
seasonEpisodes = ( searchCriteria as SeasonSearchCriteria ) . Episodes ;
}
else
{
seasonEpisodes = _episodeService . GetEpisodesBySeason ( episode . SeriesId , episode . SeasonNumber ) ;
}
//Ensure that this is either the first episode
//or is the last episode in a season that has 10 or more episodes
if ( seasonEpisodes . First ( ) . Id = = episode . Id | | ( seasonEpisodes . Count ( ) > = 10 & & seasonEpisodes . Last ( ) . Id = = episode . Id ) )
{
2014-11-26 20:21:38 +00:00
_logger . Debug ( "Possible double episode, doubling allowed size." ) ;
2014-05-02 22:04:58 +00:00
maxSize = maxSize * 2 ;
}
2014-02-15 10:51:52 +00:00
}
//If the parsed size is greater than maxSize we don't want it
if ( subject . Release . Size > maxSize )
{
2016-09-22 20:38:38 +00:00
var runtimeMessage = subject . Episodes . Count = = 1 ? $"{subject.Series.Runtime}min" : $"{subject.Episodes.Count}x {subject.Series.Runtime}min" ;
_logger . Debug ( "Item: {0}, Size: {1} is greater than maximum allowed size ({2} for {3}), rejecting." , subject , subject . Release . Size , maxSize , runtimeMessage ) ;
return Decision . Reject ( "{0} is larger than maximum allowed {1} (for {2})" , subject . Release . Size . SizeSuffix ( ) , maxSize . SizeSuffix ( ) , runtimeMessage ) ;
2014-02-15 10:51:52 +00:00
}
2012-04-17 03:42:18 +00:00
}
2014-10-27 05:51:50 +00:00
2014-03-13 20:12:42 +00:00
_logger . Debug ( "Item: {0}, meets size constraints." , subject ) ;
2014-10-27 05:51:50 +00:00
return Decision . Accept ( ) ;
2012-02-07 05:08:07 +00:00
}
}
}