Delay import when absolute episode number is missing

New: Delay import is absolute episode number is required and is missing for EpisodeFormat
This commit is contained in:
Mark McDowall 2018-12-01 00:35:46 -08:00 committed by Taloth Saldono
parent f345977e3f
commit 5f6f4915a1
6 changed files with 214 additions and 0 deletions

View File

@ -0,0 +1,77 @@
using System;
using System.Linq;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.MediaFiles.EpisodeImport.Specifications;
using NzbDrone.Core.Organizer;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
using NzbDrone.Test.Common;
namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications
{
[TestFixture]
public class AbsoluteEpisodeNumberSpecificationFixture : CoreTest<AbsoluteEpisodeNumberSpecification>
{
private Series _series;
private LocalEpisode _localEpisode;
[SetUp]
public void Setup()
{
_series = Builder<Series>.CreateNew()
.With(s => s.SeriesType = SeriesTypes.Anime)
.With(s => s.Path = @"C:\Test\TV\30 Rock".AsOsAgnostic())
.Build();
var episodes = Builder<Episode>.CreateListOfSize(1)
.All()
.With(e => e.SeasonNumber = 1)
.With(e => e.AirDateUtc = DateTime.UtcNow)
.Build()
.ToList();
_localEpisode = new LocalEpisode
{
Path = @"C:\Test\Unsorted\30 Rock\30.rock.s01e01.avi".AsOsAgnostic(),
Episodes = episodes,
Series = _series
};
Mocker.GetMock<IBuildFileNames>()
.Setup(s => s.RequiresAbsoluteEpisodeNumber(_series, episodes))
.Returns(true);
}
[Test]
public void should_reject_when_absolute_episode_number_is_null()
{
_localEpisode.Episodes.First().AbsoluteEpisodeNumber = null;
Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeFalse();
}
[Test]
public void should_accept_when_did_not_air_recently_but_absolute_episode_number_is_null()
{
_localEpisode.Episodes.First().AirDateUtc = DateTime.UtcNow.AddDays(-7);
_localEpisode.Episodes.First().AbsoluteEpisodeNumber = null;
Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue();
}
[Test]
public void should_accept_when_absolute_episode_number_is_not_required()
{
_localEpisode.Episodes.First().AbsoluteEpisodeNumber = null;
Mocker.GetMock<IBuildFileNames>()
.Setup(s => s.RequiresAbsoluteEpisodeNumber(_series, _localEpisode.Episodes))
.Returns(false);
Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue();
}
}
}

View File

@ -312,6 +312,7 @@
<Compile Include="MediaFiles\EpisodeImport\Aggregation\Aggregators\Augmenters\Quality\AugmentQualityFromMediaInfoFixture.cs" />
<Compile Include="MediaFiles\EpisodeImport\ImportDecisionMakerFixture.cs" />
<Compile Include="MediaFiles\EpisodeImport\DetectSampleFixture.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\AbsoluteEpisodeNumberSpecificationFixture.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\EpisodeTitleSpecificationFixture.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\FreeSpaceSpecificationFixture.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\FullSeasonSpecificationFixture.cs" />
@ -332,6 +333,7 @@
<Compile Include="MetadataSource\SkyHook\SkyHookProxyFixture.cs" />
<Compile Include="NotificationTests\NotificationBaseFixture.cs" />
<Compile Include="NotificationTests\SynologyIndexerFixture.cs" />
<Compile Include="OrganizerTests\FileNameBuilderTests\RequiresAbsoluteEpisodeNumberFixture.cs" />
<Compile Include="OrganizerTests\FileNameBuilderTests\RequiresEpisodeTitleFixture.cs" />
<Compile Include="OrganizerTests\FileNameBuilderTests\CleanTitleFixture.cs" />
<Compile Include="OrganizerTests\FileNameBuilderTests\EpisodeTitleCollapseFixture.cs" />

View File

@ -0,0 +1,55 @@
using System.Collections.Generic;
using FizzWare.NBuilder;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Organizer;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Test.OrganizerTests.FileNameBuilderTests
{
[TestFixture]
public class RequiresAbsoluteEpisodeNumberFixture : CoreTest<FileNameBuilder>
{
private Series _series;
private Episode _episode;
private NamingConfig _namingConfig;
[SetUp]
public void Setup()
{
_series = Builder<Series>
.CreateNew()
.With(s => s.SeriesType = SeriesTypes.Anime)
.With(s => s.Title = "South Park")
.Build();
_episode = Builder<Episode>.CreateNew()
.With(e => e.Title = "City Sushi")
.With(e => e.SeasonNumber = 15)
.With(e => e.EpisodeNumber = 6)
.With(e => e.AbsoluteEpisodeNumber = 100)
.Build();
_namingConfig = NamingConfig.Default;
_namingConfig.RenameEpisodes = true;
Mocker.GetMock<INamingConfigService>()
.Setup(c => c.GetConfig()).Returns(_namingConfig);
}
[Test]
public void should_return_false_when_absolute_episode_number_is_not_part_of_the_pattern()
{
_namingConfig.AnimeEpisodeFormat = "{Series Title} S{season:00}E{episode:00}";
Subject.RequiresAbsoluteEpisodeNumber(_series, new List<Episode> { _episode }).Should().BeFalse();
}
[Test]
public void should_return_true_when_absolute_episode_number_is_part_of_the_pattern()
{
_namingConfig.AnimeEpisodeFormat = "{Series Title} {absolute:00}";
Subject.RequiresAbsoluteEpisodeNumber(_series, new List<Episode> { _episode }).Should().BeTrue();
}
}
}

View File

@ -0,0 +1,58 @@
using System;
using NLog;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.DecisionEngine;
using NzbDrone.Core.Download;
using NzbDrone.Core.Organizer;
using NzbDrone.Core.Parser.Model;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications
{
public class AbsoluteEpisodeNumberSpecification : IImportDecisionEngineSpecification
{
private readonly IBuildFileNames _buildFileNames;
private readonly Logger _logger;
public AbsoluteEpisodeNumberSpecification(IBuildFileNames buildFileNames, Logger logger)
{
_buildFileNames = buildFileNames;
_logger = logger;
}
public Decision IsSatisfiedBy(LocalEpisode localEpisode, DownloadClientItem downloadClientItem)
{
if (localEpisode.Series.SeriesType != SeriesTypes.Anime)
{
_logger.Debug("Series type is not Anime, skipping check");
return Decision.Accept();
}
if (!_buildFileNames.RequiresAbsoluteEpisodeNumber(localEpisode.Series, localEpisode.Episodes))
{
_logger.Debug("File name format does not require absolute episode number, skipping check");
return Decision.Accept();
}
foreach (var episode in localEpisode.Episodes)
{
var airDateUtc = episode.AirDateUtc;
var absoluteEpisodeNumber = episode.AbsoluteEpisodeNumber;
if (airDateUtc.HasValue && airDateUtc.Value.Before(DateTime.UtcNow.AddDays(-1)))
{
_logger.Debug("Episode aired more than 1 day ago");
continue;
}
if (!absoluteEpisodeNumber.HasValue)
{
_logger.Debug("Episode does not have an absolute episode number and recently aired");
return Decision.Reject("Episode does not have an absolute episode number and recently aired");
}
}
return Decision.Accept();
}
}
}

View File

@ -776,6 +776,7 @@
<Compile Include="Languages\LanguageComparer.cs" />
<Compile Include="Languages\LanguagesBelowCutoff.cs" />
<Compile Include="MediaFiles\EpisodeImport\Aggregation\Aggregators\AggregateLanguage.cs" />
<Compile Include="MediaFiles\EpisodeImport\Specifications\AbsoluteEpisodeNumberSpecification.cs" />
<Compile Include="Notifications\Plex\PlexTv\PlexTvPinUrlResponse.cs" />
<Compile Include="Notifications\Plex\PlexTv\PlexTvSignInUrlResponse.cs" />
<Compile Include="Notifications\Plex\PlexTv\PlexTvPinResponse.cs" />

View File

@ -25,6 +25,7 @@ namespace NzbDrone.Core.Organizer
string GetSeriesFolder(Series series, NamingConfig namingConfig = null);
string GetSeasonFolder(Series series, int seasonNumber, NamingConfig namingConfig = null);
bool RequiresEpisodeTitle(Series series, List<Episode> episodes);
bool RequiresAbsoluteEpisodeNumber(Series series, List<Episode> episodes);
}
public class FileNameBuilder : IBuildFileNames
@ -35,6 +36,7 @@ namespace NzbDrone.Core.Organizer
private readonly ICached<EpisodeFormat[]> _episodeFormatCache;
private readonly ICached<AbsoluteEpisodeFormat[]> _absoluteEpisodeFormatCache;
private readonly ICached<bool> _requiresEpisodeTitleCache;
private readonly ICached<bool> _requiresAbsoluteEpisodeNumberCache;
private readonly Logger _logger;
private static readonly Regex TitleRegex = new Regex(@"\{(?<prefix>[- ._\[(]*)(?<token>(?:[a-z0-9]+)(?:(?<separator>[- ._]+)(?:[a-z0-9]+))?)(?::(?<customFormat>[a-z0-9]+))?(?<suffix>[- ._)\]]*)\}",
@ -87,6 +89,7 @@ namespace NzbDrone.Core.Organizer
_episodeFormatCache = cacheManager.GetCache<EpisodeFormat[]>(GetType(), "episodeFormat");
_absoluteEpisodeFormatCache = cacheManager.GetCache<AbsoluteEpisodeFormat[]>(GetType(), "absoluteEpisodeFormat");
_requiresEpisodeTitleCache = cacheManager.GetCache<bool>(GetType(), "requiresEpisodeTitle");
_requiresAbsoluteEpisodeNumberCache = cacheManager.GetCache<bool>(GetType(), "requiresAbsoluteEpisodeNumber");
_logger = logger;
}
@ -340,6 +343,24 @@ namespace NzbDrone.Core.Organizer
});
}
public bool RequiresAbsoluteEpisodeNumber(Series series, List<Episode> episodes)
{
if (series.SeriesType != SeriesTypes.Anime)
{
return false;
}
var namingConfig = _namingConfigService.GetConfig();
var pattern = namingConfig.AnimeEpisodeFormat;
return _requiresAbsoluteEpisodeNumberCache.Get(pattern, () =>
{
var matches = AbsoluteEpisodeRegex.Matches(pattern);
return matches.Count > 0;
});
}
private void AddSeriesTokens(Dictionary<string, Func<TokenMatch, string>> tokenHandlers, Series series)
{
tokenHandlers["{Series Title}"] = m => series.Title;