From 54a267d860cdc87cb8c916f0b0a707fc640ddaaa Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 7 Feb 2021 17:28:16 -0800 Subject: [PATCH] Fixed: Don't automatically import if absolutely numbered file if it doesn't match expected season Closes #377 --- .../MatchesFolderSpecificationFixture.cs | 56 ++++++++++++++++++- .../MatchesFolderSpecification.cs | 32 ++++++----- 2 files changed, 73 insertions(+), 15 deletions(-) diff --git a/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/MatchesFolderSpecificationFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/MatchesFolderSpecificationFixture.cs index 6bf5ec207..d7c23b143 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/MatchesFolderSpecificationFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/MatchesFolderSpecificationFixture.cs @@ -1,3 +1,4 @@ +using System.Linq; using FizzWare.NBuilder; using FluentAssertions; using Moq; @@ -6,6 +7,7 @@ using NzbDrone.Core.MediaFiles.EpisodeImport.Specifications; using NzbDrone.Core.Parser; 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 @@ -33,6 +35,24 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications .With(p => p.FullSeason = false) .Build()) .Build(); + + } + + private void GivenEpisodes(ParsedEpisodeInfo parsedEpisodeInfo, int[] episodeNumbers) + { + var seasonNumber = parsedEpisodeInfo.SeasonNumber; + + var episodes = episodeNumbers.Select(n => + Builder.CreateNew() + .With(e => e.Id = seasonNumber * 10 + n) + .With(e => e.SeasonNumber = seasonNumber) + .With(e => e.EpisodeNumber = n) + .Build() + ).ToList(); + + Mocker.GetMock() + .Setup(s => s.GetEpisodes(parsedEpisodeInfo, It.IsAny(), true, null)) + .Returns(episodes); } [Test] @@ -68,6 +88,9 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications _localEpisode.FolderEpisodeInfo.EpisodeNumbers = new int[0]; _localEpisode.FolderEpisodeInfo.FullSeason = true; + GivenEpisodes(_localEpisode.FileEpisodeInfo, _localEpisode.FileEpisodeInfo.EpisodeNumbers); + GivenEpisodes(_localEpisode.FolderEpisodeInfo, new []{ 1, 2, 3, 4, 5 }); + Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue(); } @@ -78,6 +101,9 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications _localEpisode.FolderEpisodeInfo.EpisodeNumbers = new[] { 1 }; _localEpisode.Path = @"C:\Test\Unsorted\Series.Title.S01E01.720p.HDTV-Sonarr\S01E01.mkv".AsOsAgnostic(); + GivenEpisodes(_localEpisode.FileEpisodeInfo, _localEpisode.FileEpisodeInfo.EpisodeNumbers); + GivenEpisodes(_localEpisode.FolderEpisodeInfo, _localEpisode.FolderEpisodeInfo.EpisodeNumbers); + Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue(); } @@ -88,16 +114,22 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications _localEpisode.FolderEpisodeInfo.EpisodeNumbers = new[] { 1 }; _localEpisode.Path = @"C:\Test\Unsorted\Series.Title.S01E01E02.720p.HDTV-Sonarr\S01E01.mkv".AsOsAgnostic(); + GivenEpisodes(_localEpisode.FileEpisodeInfo, _localEpisode.FileEpisodeInfo.EpisodeNumbers); + GivenEpisodes(_localEpisode.FolderEpisodeInfo, _localEpisode.FolderEpisodeInfo.EpisodeNumbers); + Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue(); } [Test] - public void should_be_disregard_subfolder() + public void should_disregard_subfolder() { _localEpisode.FileEpisodeInfo.EpisodeNumbers = new[] { 5, 6 }; _localEpisode.FolderEpisodeInfo.EpisodeNumbers = new[] { 1, 2 }; _localEpisode.Path = @"C:\Test\Unsorted\Series.Title.S01E01E02.720p.HDTV-Sonarr\S01E05E06.mkv".AsOsAgnostic(); + GivenEpisodes(_localEpisode.FileEpisodeInfo, _localEpisode.FileEpisodeInfo.EpisodeNumbers); + GivenEpisodes(_localEpisode.FolderEpisodeInfo, _localEpisode.FolderEpisodeInfo.EpisodeNumbers); + Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeFalse(); } @@ -106,6 +138,9 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications { _localEpisode.Path = @"C:\Test\Unsorted\Series.Title.S01E01.720p.HDTV-Sonarr\S01E05.mkv".AsOsAgnostic(); + GivenEpisodes(_localEpisode.FileEpisodeInfo, _localEpisode.FileEpisodeInfo.EpisodeNumbers); + GivenEpisodes(_localEpisode.FolderEpisodeInfo, _localEpisode.FolderEpisodeInfo.EpisodeNumbers); + Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeFalse(); } @@ -116,6 +151,9 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications _localEpisode.FolderEpisodeInfo.EpisodeNumbers = new[] { 1, 2 }; _localEpisode.Path = @"C:\Test\Unsorted\Series.Title.S01E01E02.720p.HDTV-Sonarr\S01E05E06.mkv".AsOsAgnostic(); + GivenEpisodes(_localEpisode.FileEpisodeInfo, _localEpisode.FileEpisodeInfo.EpisodeNumbers); + GivenEpisodes(_localEpisode.FolderEpisodeInfo, _localEpisode.FolderEpisodeInfo.EpisodeNumbers); + Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeFalse(); } @@ -129,6 +167,9 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications _localEpisode.FolderEpisodeInfo.SeasonNumber = 1; _localEpisode.FolderEpisodeInfo.EpisodeNumbers = new[] { 1, 2 }; + GivenEpisodes(_localEpisode.FileEpisodeInfo, _localEpisode.FileEpisodeInfo.EpisodeNumbers); + GivenEpisodes(_localEpisode.FolderEpisodeInfo, _localEpisode.FolderEpisodeInfo.EpisodeNumbers); + _localEpisode.Path = @"C:\Test\Unsorted\Series.Title.S01.720p.HDTV-Sonarr\S02E01.mkv".AsOsAgnostic(); Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeFalse(); @@ -143,6 +184,9 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications _localEpisode.FolderEpisodeInfo.SeasonNumber = 1; _localEpisode.FolderEpisodeInfo.EpisodeNumbers = new[] { 1, 2 }; + GivenEpisodes(_localEpisode.FileEpisodeInfo, _localEpisode.FileEpisodeInfo.EpisodeNumbers); + GivenEpisodes(_localEpisode.FolderEpisodeInfo, _localEpisode.FolderEpisodeInfo.EpisodeNumbers); + _localEpisode.Path = @"C:\Test\Unsorted\Series.Title.S01.720p.HDTV-Sonarr\S02E01.mkv".AsOsAgnostic(); Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeFalse(); @@ -158,6 +202,9 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications _localEpisode.FolderEpisodeInfo.SeasonNumber = 1; _localEpisode.FolderEpisodeInfo.EpisodeNumbers = new[] { 1, 2 }; + GivenEpisodes(_localEpisode.FileEpisodeInfo, _localEpisode.FileEpisodeInfo.EpisodeNumbers); + GivenEpisodes(_localEpisode.FolderEpisodeInfo, _localEpisode.FolderEpisodeInfo.EpisodeNumbers); + _localEpisode.Path = @"C:\Test\Unsorted\Series.Title.S01.720p.HDTV-Sonarr\S01E01.mkv".AsOsAgnostic(); Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue(); @@ -182,6 +229,8 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications } }; + GivenEpisodes(actualInfo, actualInfo.EpisodeNumbers); + Mocker.GetMock() .Setup(v => v.ParseSpecialEpisodeTitle(fileInfo, It.IsAny(), 0, 0, null)) .Returns(actualInfo); @@ -196,12 +245,15 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications [Test] public void should_be_accepted_if_file_has_absolute_episode_number_and_folder_uses_standard() { - _localEpisode.FileEpisodeInfo.SeasonNumber = 0; + _localEpisode.FileEpisodeInfo.SeasonNumber = 1; _localEpisode.FileEpisodeInfo.AbsoluteEpisodeNumbers = new[] { 1 }; _localEpisode.FolderEpisodeInfo.SeasonNumber = 1; _localEpisode.FolderEpisodeInfo.EpisodeNumbers = new[] { 1, 2 }; + GivenEpisodes(_localEpisode.FileEpisodeInfo, new []{ 1 }); + GivenEpisodes(_localEpisode.FolderEpisodeInfo, _localEpisode.FolderEpisodeInfo.EpisodeNumbers); + _localEpisode.Path = @"C:\Test\Unsorted\Series.Title.S01.720p.HDTV-Sonarr\S02E01.mkv".AsOsAgnostic(); Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue(); diff --git a/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/MatchesFolderSpecification.cs b/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/MatchesFolderSpecification.cs index 43503d2b2..9f10c1030 100644 --- a/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/MatchesFolderSpecification.cs +++ b/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/MatchesFolderSpecification.cs @@ -1,9 +1,12 @@ +using System.Collections.Generic; using System.Linq; using NLog; +using NzbDrone.Common.Extensions; using NzbDrone.Core.DecisionEngine; using NzbDrone.Core.Download; using NzbDrone.Core.Parser; using NzbDrone.Core.Parser.Model; +using NzbDrone.Core.Tv; namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications { @@ -17,6 +20,7 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications _logger = logger; _parsingService = parsingService; } + public Decision IsSatisfiedBy(LocalEpisode localEpisode, DownloadClientItem downloadClientItem) { if (localEpisode.ExistingFile) @@ -49,38 +53,40 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications return Decision.Accept(); } - if (fileInfo.IsAbsoluteNumbering) + var folderEpisodes = _parsingService.GetEpisodes(folderInfo, localEpisode.Series, true); + var fileEpisodes = _parsingService.GetEpisodes(fileInfo, localEpisode.Series, true); + + if (folderEpisodes.Empty()) { - _logger.Debug("File uses absolute episode numbering, skipping check"); + _logger.Debug("No episode numbers in folder ParsedEpisodeInfo, skipping check"); return Decision.Accept(); } - if (folderInfo.SeasonNumber != fileInfo.SeasonNumber) + if (folderEpisodes.First().SeasonNumber != fileEpisodes.FirstOrDefault()?.SeasonNumber) { return Decision.Reject("Season number {0} was unexpected considering the folder name {1}", fileInfo.SeasonNumber, folderInfo.ReleaseTitle); } - if (!folderInfo.EpisodeNumbers.Any()) - { - _logger.Debug("No episode numbers in folder ParsedEpisodeInfo, skipping check"); - return Decision.Accept(); - } - - var unexpected = fileInfo.EpisodeNumbers.Where(f => !folderInfo.EpisodeNumbers.Contains(f)).ToList(); + var unexpected = fileEpisodes.Where(e => folderEpisodes.All(o => o.Id != e.Id)).ToList(); if (unexpected.Any()) { - _logger.Debug("Unexpected episode number(s) in file: {0}", string.Join(", ", unexpected)); + _logger.Debug("Unexpected episode(s) in file: {0}", FormatEpisode(unexpected)); if (unexpected.Count == 1) { - return Decision.Reject("Episode number {0} was unexpected considering the {1} folder name", unexpected.First(), folderInfo.ReleaseTitle); + return Decision.Reject("Episode {0} was unexpected considering the {1} folder name", FormatEpisode(unexpected), folderInfo.ReleaseTitle); } - return Decision.Reject("Episode numbers {0} were unexpected considering the {1} folder name", string.Join(", ", unexpected), folderInfo.ReleaseTitle); + return Decision.Reject("Episodes {0} were unexpected considering the {1} folder name", FormatEpisode(unexpected), folderInfo.ReleaseTitle); } return Decision.Accept(); } + + private string FormatEpisode(List episodes) + { + return string.Join(", ", episodes.Select(e => $"{e.SeasonNumber}x{e.EpisodeNumber:00}")); + } } }