From d1a9cf98cca2624af16bbd4b5ce66d989ff1f62f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20Payet?= Date: Wed, 6 Apr 2022 22:32:49 +0200 Subject: [PATCH] Fixed: Use Movie Original Language for Custom Format Original Language (#6882) --- .../QueueSpecificationFixture.cs | 2 +- .../AugmentWithOriginalLanguageFixture.cs | 26 ++++++++++++++++++ .../CustomFormatCalculationService.cs | 12 +++++---- .../Specifications/LanguageSpecification.cs | 5 +++- .../DecisionEngine/DownloadDecisionMaker.cs | 2 +- .../Specifications/QueueSpecification.cs | 2 +- .../Download/Pending/PendingReleaseService.cs | 2 +- .../TrackedDownloadService.cs | 2 +- .../Augmenters/AugmentWithOriginalLanguage.cs | 27 +++++++++++++++++++ 9 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/AugmentersTests/AugmentWithOriginalLanguageFixture.cs create mode 100644 src/NzbDrone.Core/Parser/Augmenters/AugmentWithOriginalLanguage.cs diff --git a/src/NzbDrone.Core.Test/DecisionEngineTests/QueueSpecificationFixture.cs b/src/NzbDrone.Core.Test/DecisionEngineTests/QueueSpecificationFixture.cs index 4ed94445c..537413b6d 100644 --- a/src/NzbDrone.Core.Test/DecisionEngineTests/QueueSpecificationFixture.cs +++ b/src/NzbDrone.Core.Test/DecisionEngineTests/QueueSpecificationFixture.cs @@ -58,7 +58,7 @@ namespace NzbDrone.Core.Test.DecisionEngineTests .Build(); Mocker.GetMock() - .Setup(x => x.ParseCustomFormat(It.IsAny())) + .Setup(x => x.ParseCustomFormat(It.IsAny(), _movie)) .Returns(new List()); } diff --git a/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/AugmentersTests/AugmentWithOriginalLanguageFixture.cs b/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/AugmentersTests/AugmentWithOriginalLanguageFixture.cs new file mode 100644 index 000000000..3c44acdbb --- /dev/null +++ b/src/NzbDrone.Core.Test/ParserTests/ParsingServiceTests/AugmentersTests/AugmentWithOriginalLanguageFixture.cs @@ -0,0 +1,26 @@ +using System; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Languages; +using NzbDrone.Core.Parser.Augmenters; +using NzbDrone.Core.Parser.Model; + +namespace NzbDrone.Core.Test.ParserTests.ParsingServiceTests.AugmentersTests +{ + [TestFixture] + public class AugmentWithOriginalLanguageFixture : AugmentMovieInfoFixture + { + [Test] + public void should_add_movie_original_language() + { + var releaseInfo = new ParsedMovieInfo(); + var movie = new Movies.Movie + { + OriginalLanguage = Language.English + }; + var result = Subject.AugmentMovieInfo(releaseInfo, movie); + result.ExtraInfo.Should().ContainKey("OriginalLanguage"); + result.ExtraInfo["OriginalLanguage"].Should().Be(Language.English); + } + } +} diff --git a/src/NzbDrone.Core/CustomFormats/CustomFormatCalculationService.cs b/src/NzbDrone.Core/CustomFormats/CustomFormatCalculationService.cs index 5b4c6736a..b7929f7a5 100644 --- a/src/NzbDrone.Core/CustomFormats/CustomFormatCalculationService.cs +++ b/src/NzbDrone.Core/CustomFormats/CustomFormatCalculationService.cs @@ -14,7 +14,7 @@ namespace NzbDrone.Core.CustomFormats { public interface ICustomFormatCalculationService { - List ParseCustomFormat(ParsedMovieInfo movieInfo); + List ParseCustomFormat(ParsedMovieInfo movieInfo, Movie movie); List ParseCustomFormat(MovieFile movieFile); List ParseCustomFormat(Blocklist blocklist); List ParseCustomFormat(MovieHistory history); @@ -88,15 +88,17 @@ namespace NzbDrone.Core.CustomFormats { { "IndexerFlags", movieFile.IndexerFlags }, { "Size", movieFile.Size }, - { "Filename", System.IO.Path.GetFileName(movieFile.RelativePath) } + { "Filename", Path.GetFileName(movieFile.RelativePath) }, + { "OriginalLanguage", movieFile.Movie.OriginalLanguage } } }; return ParseCustomFormat(info, allCustomFormats); } - public List ParseCustomFormat(ParsedMovieInfo movieInfo) + public List ParseCustomFormat(ParsedMovieInfo movieInfo, Movie movie) { + movieInfo = _parsingService.EnhanceMovieInfo(movieInfo, new List { movie }) ?? movieInfo; return ParseCustomFormat(movieInfo, _formatService.All()); } @@ -127,7 +129,7 @@ namespace NzbDrone.Core.CustomFormats } }; - return ParseCustomFormat(info); + return ParseCustomFormat(info, movie); } public List ParseCustomFormat(MovieHistory history) @@ -155,7 +157,7 @@ namespace NzbDrone.Core.CustomFormats } }; - return ParseCustomFormat(info); + return ParseCustomFormat(info, movie); } } } diff --git a/src/NzbDrone.Core/CustomFormats/Specifications/LanguageSpecification.cs b/src/NzbDrone.Core/CustomFormats/Specifications/LanguageSpecification.cs index 64cb99e66..db7f40389 100644 --- a/src/NzbDrone.Core/CustomFormats/Specifications/LanguageSpecification.cs +++ b/src/NzbDrone.Core/CustomFormats/Specifications/LanguageSpecification.cs @@ -14,7 +14,10 @@ namespace NzbDrone.Core.CustomFormats protected override bool IsSatisfiedByWithoutNegate(ParsedMovieInfo movieInfo) { - return movieInfo?.Languages?.Contains((Language)Value) ?? false; + var comparedLanguage = movieInfo != null && Name == "Original" && movieInfo.ExtraInfo.ContainsKey("OriginalLanguage") + ? (Language)movieInfo.ExtraInfo["OriginalLanguage"] + : (Language)Value; + return movieInfo?.Languages?.Contains(comparedLanguage) ?? false; } } } diff --git a/src/NzbDrone.Core/DecisionEngine/DownloadDecisionMaker.cs b/src/NzbDrone.Core/DecisionEngine/DownloadDecisionMaker.cs index 47dda3cef..91abea174 100644 --- a/src/NzbDrone.Core/DecisionEngine/DownloadDecisionMaker.cs +++ b/src/NzbDrone.Core/DecisionEngine/DownloadDecisionMaker.cs @@ -103,7 +103,7 @@ namespace NzbDrone.Core.DecisionEngine result.ReleaseName = report.Title; var remoteMovie = result.RemoteMovie; - remoteMovie.CustomFormats = _formatCalculator.ParseCustomFormat(parsedMovieInfo); + remoteMovie.CustomFormats = _formatCalculator.ParseCustomFormat(parsedMovieInfo, result?.Movie); remoteMovie.CustomFormatScore = remoteMovie?.Movie?.Profile?.CalculateCustomFormatScore(remoteMovie.CustomFormats) ?? 0; remoteMovie.Release = report; remoteMovie.MappingResult = result.MappingResultType; diff --git a/src/NzbDrone.Core/DecisionEngine/Specifications/QueueSpecification.cs b/src/NzbDrone.Core/DecisionEngine/Specifications/QueueSpecification.cs index be362d03e..3ac2506e3 100644 --- a/src/NzbDrone.Core/DecisionEngine/Specifications/QueueSpecification.cs +++ b/src/NzbDrone.Core/DecisionEngine/Specifications/QueueSpecification.cs @@ -50,7 +50,7 @@ namespace NzbDrone.Core.DecisionEngine.Specifications continue; } - var customFormats = _formatService.ParseCustomFormat(remoteMovie.ParsedMovieInfo); + var customFormats = _formatService.ParseCustomFormat(remoteMovie.ParsedMovieInfo, subject.Movie); _logger.Debug("Checking if existing release in queue meets cutoff. Queued quality is: {0} - {1}", remoteMovie.ParsedMovieInfo.Quality, diff --git a/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs b/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs index 1cc40fe65..91f950b47 100644 --- a/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs +++ b/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs @@ -163,7 +163,7 @@ namespace NzbDrone.Core.Download.Pending { if (pendingRelease.RemoteMovie != null) { - pendingRelease.RemoteMovie.CustomFormats = _formatCalculator.ParseCustomFormat(pendingRelease.ParsedMovieInfo); + pendingRelease.RemoteMovie.CustomFormats = _formatCalculator.ParseCustomFormat(pendingRelease.ParsedMovieInfo, pendingRelease.RemoteMovie.Movie); var ect = pendingRelease.Release.PublishDate.AddMinutes(GetDelay(pendingRelease.RemoteMovie)); diff --git a/src/NzbDrone.Core/Download/TrackedDownloads/TrackedDownloadService.cs b/src/NzbDrone.Core/Download/TrackedDownloads/TrackedDownloadService.cs index 0ca7e2bd3..349b20c4c 100644 --- a/src/NzbDrone.Core/Download/TrackedDownloads/TrackedDownloadService.cs +++ b/src/NzbDrone.Core/Download/TrackedDownloads/TrackedDownloadService.cs @@ -151,7 +151,7 @@ namespace NzbDrone.Core.Download.TrackedDownloads // Calculate custom formats if (trackedDownload.RemoteMovie != null) { - trackedDownload.RemoteMovie.CustomFormats = _formatCalculator.ParseCustomFormat(parsedMovieInfo); + trackedDownload.RemoteMovie.CustomFormats = _formatCalculator.ParseCustomFormat(parsedMovieInfo, trackedDownload.RemoteMovie.Movie); } // Track it so it can be displayed in the queue even though we can't determine which movie it is for diff --git a/src/NzbDrone.Core/Parser/Augmenters/AugmentWithOriginalLanguage.cs b/src/NzbDrone.Core/Parser/Augmenters/AugmentWithOriginalLanguage.cs new file mode 100644 index 000000000..a1b7b6671 --- /dev/null +++ b/src/NzbDrone.Core/Parser/Augmenters/AugmentWithOriginalLanguage.cs @@ -0,0 +1,27 @@ +using System; +using NzbDrone.Core.Movies; +using NzbDrone.Core.Parser.Model; + +namespace NzbDrone.Core.Parser.Augmenters +{ + public class AugmentWithOriginalLanguage : IAugmentParsedMovieInfo + { + public Type HelperType + { + get + { + return typeof(Movie); + } + } + + public ParsedMovieInfo AugmentMovieInfo(ParsedMovieInfo movieInfo, object helper) + { + if (helper is Movie movie && movie?.OriginalLanguage != null && movieInfo != null) + { + movieInfo.ExtraInfo["OriginalLanguage"] = movie.OriginalLanguage; + } + + return movieInfo; + } + } +}