From 21fafb895f4fda7c5478eb62fa2c07d9dbbd610f Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sun, 24 Jan 2021 01:18:08 -0800 Subject: [PATCH] Fixed: Files with lower preferred word scores are imported Closes #4212 --- .../UpgradeSpecificationFixture.cs | 101 ++++++++++++++++++ .../Specifications/UpgradeSpecification.cs | 50 ++++++++- 2 files changed, 146 insertions(+), 5 deletions(-) diff --git a/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/UpgradeSpecificationFixture.cs b/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/UpgradeSpecificationFixture.cs index 1a9426581..b2f4c444a 100644 --- a/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/UpgradeSpecificationFixture.cs +++ b/src/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/UpgradeSpecificationFixture.cs @@ -2,6 +2,7 @@ using FizzWare.NBuilder; using FluentAssertions; using Marr.Data; +using Moq; using NUnit.Framework; using NzbDrone.Core.Configuration; using NzbDrone.Core.MediaFiles; @@ -13,6 +14,7 @@ using NzbDrone.Core.Tv; using NzbDrone.Core.Languages; using NzbDrone.Core.Profiles.Languages; +using NzbDrone.Core.Profiles.Releases; namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications { @@ -278,6 +280,39 @@ public void should_return_false_if_not_a_revision_upgrade_and_prefers_propers() Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeFalse(); } + [Test] + public void should_return_false_if_it_is_not_a_preferred_word_upgrade() + { + Mocker.GetMock() + .Setup(s => s.DownloadPropersAndRepacks) + .Returns(ProperDownloadTypes.DoNotPrefer); + + Mocker.GetMock() + .Setup(s => s.Calculate(It.IsAny(), It.IsAny(), 0)) + .Returns(5); + + Mocker.GetMock() + .Setup(s => s.Calculate(It.IsAny(), It.IsAny())) + .Returns(10); + + _localEpisode.Quality = new QualityModel(Quality.Bluray1080p); + + _localEpisode.Episodes = Builder.CreateListOfSize(1) + .All() + .With(e => e.EpisodeFileId = 1) + .With(e => e.EpisodeFile = new LazyLoaded( + new EpisodeFile + { + Quality = new QualityModel(Quality.Bluray1080p) + })) + .Build() + .ToList(); + + _localEpisode.FileEpisodeInfo = Builder.CreateNew().Build(); + + Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeFalse(); + } + [Test] public void should_return_true_if_not_a_revision_upgrade_and_does_not_prefer_propers() { @@ -322,6 +357,72 @@ public void should_return_true_when_comparing_to_a_lower_quality_proper() Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue(); } + [Test] + public void should_return_true_if_it_is_a_preferred_word_upgrade() + { + Mocker.GetMock() + .Setup(s => s.DownloadPropersAndRepacks) + .Returns(ProperDownloadTypes.DoNotPrefer); + + Mocker.GetMock() + .Setup(s => s.Calculate(It.IsAny(), It.IsAny(), 0)) + .Returns(5); + + Mocker.GetMock() + .Setup(s => s.Calculate(It.IsAny(), It.IsAny())) + .Returns(1); + + _localEpisode.Quality = new QualityModel(Quality.Bluray1080p); + + _localEpisode.Episodes = Builder.CreateListOfSize(1) + .All() + .With(e => e.EpisodeFileId = 1) + .With(e => e.EpisodeFile = new LazyLoaded( + new EpisodeFile + { + Quality = new QualityModel(Quality.Bluray1080p) + })) + .Build() + .ToList(); + + _localEpisode.FileEpisodeInfo = Builder.CreateNew().Build(); + + Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue(); + } + + [Test] + public void should_return_true_if_it_has_an_equal_preferred_word_score() + { + Mocker.GetMock() + .Setup(s => s.DownloadPropersAndRepacks) + .Returns(ProperDownloadTypes.DoNotPrefer); + + Mocker.GetMock() + .Setup(s => s.Calculate(It.IsAny(), It.IsAny(), 0)) + .Returns(5); + + Mocker.GetMock() + .Setup(s => s.Calculate(It.IsAny(), It.IsAny())) + .Returns(5); + + _localEpisode.Quality = new QualityModel(Quality.Bluray1080p); + + _localEpisode.Episodes = Builder.CreateListOfSize(1) + .All() + .With(e => e.EpisodeFileId = 1) + .With(e => e.EpisodeFile = new LazyLoaded( + new EpisodeFile + { + Quality = new QualityModel(Quality.Bluray1080p) + })) + .Build() + .ToList(); + + _localEpisode.FileEpisodeInfo = Builder.CreateNew().Build(); + + Subject.IsSatisfiedBy(_localEpisode, null).Accepted.Should().BeTrue(); + } + [Test] public void should_return_true_if_episode_file_is_null() { diff --git a/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/UpgradeSpecification.cs b/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/UpgradeSpecification.cs index 9f1bb4f98..6b6508617 100644 --- a/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/UpgradeSpecification.cs +++ b/src/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/UpgradeSpecification.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System.Collections.Generic; +using System.Linq; using NLog; using NzbDrone.Core.Configuration; using NzbDrone.Core.DecisionEngine; @@ -6,17 +7,25 @@ using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Qualities; using NzbDrone.Core.Languages; +using NzbDrone.Core.Profiles.Releases; namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications { public class UpgradeSpecification : IImportDecisionEngineSpecification { private readonly IConfigService _configService; + private readonly IPreferredWordService _preferredWordService; + private readonly IEpisodeFilePreferredWordCalculator _episodeFilePreferredWordCalculator; private readonly Logger _logger; - public UpgradeSpecification(IConfigService configService, Logger logger) + public UpgradeSpecification(IConfigService configService, + IPreferredWordService preferredWordService, + IEpisodeFilePreferredWordCalculator episodeFilePreferredWordCalculator, + Logger logger) { _configService = configService; + _preferredWordService = preferredWordService; + _episodeFilePreferredWordCalculator = episodeFilePreferredWordCalculator; _logger = logger; } @@ -25,6 +34,7 @@ public Decision IsSatisfiedBy(LocalEpisode localEpisode, DownloadClientItem down var downloadPropersAndRepacks = _configService.DownloadPropersAndRepacks; var qualityComparer = new QualityModelComparer(localEpisode.Series.QualityProfile); var languageComparer = new LanguageComparer(localEpisode.Series.LanguageProfile); + var preferredWordScore = GetPreferredWordScore(localEpisode); foreach (var episode in localEpisode.Episodes.Where(e => e.EpisodeFileId > 0)) { @@ -45,7 +55,6 @@ public Decision IsSatisfiedBy(LocalEpisode localEpisode, DownloadClientItem down return Decision.Reject("Not an upgrade for existing episode file(s)"); } - // Same quality, is not a language upgrade, propers/repacks are preferred and it is not a revision update // This will allow language upgrades of a lower revision to be imported, which are allowed to be grabbed, // they just don't import automatically. @@ -56,17 +65,48 @@ public Decision IsSatisfiedBy(LocalEpisode localEpisode, DownloadClientItem down localEpisode.Quality.Revision.CompareTo(episodeFile.Quality.Revision) < 0) { _logger.Debug("This file isn't a quality revision upgrade for all episodes. Skipping {0}", localEpisode.Path); - return Decision.Reject("Not an upgrade for existing episode file(s)"); + return Decision.Reject("Not a quality revision upgrade for existing episode file(s)"); } if (languageCompare < 0 && qualityCompare == 0) { _logger.Debug("This file isn't a language upgrade for all episodes. Skipping {0}", localEpisode.Path); - return Decision.Reject("Not an upgrade for existing episode file(s)"); + return Decision.Reject("Not a language upgrade for existing episode file(s)"); + } + + var episodeFilePreferredWordScore = _episodeFilePreferredWordCalculator.Calculate(localEpisode.Series, episodeFile); + + if (qualityCompare == 0 && preferredWordScore < episodeFilePreferredWordScore) + { + _logger.Debug("This file isn't a preferred word upgrade for all episodes. Skipping {0}", localEpisode.Path); + return Decision.Reject("Not a preferred word upgrade for existing episode file(s)"); } } return Decision.Accept(); } + + private int GetPreferredWordScore(LocalEpisode localEpisode) + { + var series = localEpisode.Series; + var scores = new List(); + + if (localEpisode.FileEpisodeInfo != null) + { + scores.Add(_preferredWordService.Calculate(series, localEpisode.FileEpisodeInfo.ReleaseTitle, 0)); + } + + if (localEpisode.FolderEpisodeInfo != null) + { + scores.Add(_preferredWordService.Calculate(series, localEpisode.FolderEpisodeInfo.ReleaseTitle, 0)); + } + + if (localEpisode.DownloadClientEpisodeInfo != null) + { + scores.Add(_preferredWordService.Calculate(series, localEpisode.DownloadClientEpisodeInfo.ReleaseTitle, 0)); + } + + return scores.MaxOrDefault(); + } } }