From e32383e76340643b52d2c4baeea28aa2c66627f1 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sat, 22 Apr 2023 21:59:55 -0500 Subject: [PATCH] Fixed: Throw to manual import if multiple movies found during title mapping --- .../MovieImport/Manual/ManualImportService.cs | 19 +++++++-- src/NzbDrone.Core/Movies/MovieService.cs | 39 ++++++++++++------- .../Movies/MultipleMoviesFoundException.cs | 12 ++++++ src/NzbDrone.Core/Movies/QueryExtensions.cs | 4 +- 4 files changed, 55 insertions(+), 19 deletions(-) create mode 100644 src/NzbDrone.Core/Movies/MultipleMoviesFoundException.cs diff --git a/src/NzbDrone.Core/MediaFiles/MovieImport/Manual/ManualImportService.cs b/src/NzbDrone.Core/MediaFiles/MovieImport/Manual/ManualImportService.cs index 23e1a32d0..3d94dbb65 100644 --- a/src/NzbDrone.Core/MediaFiles/MovieImport/Manual/ManualImportService.cs +++ b/src/NzbDrone.Core/MediaFiles/MovieImport/Manual/ManualImportService.cs @@ -132,12 +132,25 @@ namespace NzbDrone.Core.MediaFiles.MovieImport.Manual private List ProcessFolder(string rootFolder, string baseFolder, string downloadId, int? movieId, bool filterExistingFiles) { DownloadClientItem downloadClientItem = null; + Movie movie = null; var directoryInfo = new DirectoryInfo(baseFolder); - var movie = movieId.HasValue ? - _movieService.GetMovie(movieId.Value) : - _parsingService.GetMovie(directoryInfo.Name); + if (movieId.HasValue) + { + movie = _movieService.GetMovie(movieId.Value); + } + else + { + try + { + movie = _parsingService.GetMovie(directoryInfo.Name); + } + catch (MultipleMoviesFoundException e) + { + _logger.Warn(e, "Unable to match movie by title"); + } + } if (downloadId.IsNotNullOrWhiteSpace()) { diff --git a/src/NzbDrone.Core/Movies/MovieService.cs b/src/NzbDrone.Core/Movies/MovieService.cs index d96d499df..bfce2f4f4 100644 --- a/src/NzbDrone.Core/Movies/MovieService.cs +++ b/src/NzbDrone.Core/Movies/MovieService.cs @@ -124,37 +124,33 @@ namespace NzbDrone.Core.Movies { var cleanTitles = titles.Select(t => t.CleanMovieTitle().ToLowerInvariant()); - var result = candidates.Where(x => cleanTitles.Contains(x.MovieMetadata.Value.CleanTitle)).FirstWithYear(year); + var result = candidates.Where(x => cleanTitles.Contains(x.MovieMetadata.Value.CleanTitle) || cleanTitles.Contains(x.MovieMetadata.Value.CleanOriginalTitle)) + .AllWithYear(year) + .ToList(); - if (result == null) + if (result == null || result.Count == 0) { result = - candidates.Where(movie => cleanTitles.Contains(movie.MovieMetadata.Value.CleanOriginalTitle)).FirstWithYear(year); + candidates.Where(movie => otherTitles.Contains(movie.MovieMetadata.Value.CleanTitle)).AllWithYear(year).ToList(); } - if (result == null) - { - result = - candidates.Where(movie => otherTitles.Contains(movie.MovieMetadata.Value.CleanTitle)).FirstWithYear(year); - } - - if (result == null) + if (result == null || result.Count == 0) { result = candidates .Where(m => m.MovieMetadata.Value.AlternativeTitles.Any(t => cleanTitles.Contains(t.CleanTitle) || otherTitles.Contains(t.CleanTitle))) - .FirstWithYear(year); + .AllWithYear(year).ToList(); } - if (result == null) + if (result == null || result.Count == 0) { result = candidates .Where(m => m.MovieMetadata.Value.Translations.Any(t => cleanTitles.Contains(t.CleanTitle) || otherTitles.Contains(t.CleanTitle))) - .FirstWithYear(year); + .AllWithYear(year).ToList(); } - return result; + return ReturnSingleMovieOrThrow(result.ToList()); } public List FindByTitleCandidates(List titles, out List otherTitles) @@ -395,6 +391,21 @@ namespace NzbDrone.Core.Movies return _movieRepository.ExistsByMetadataId(metadataId); } + private Movie ReturnSingleMovieOrThrow(List movies) + { + if (movies.Count == 0) + { + return null; + } + + if (movies.Count == 1) + { + return movies.First(); + } + + throw new MultipleMoviesFoundException("Expected one movie, but found {0}. Matching movies: {1}", movies.Count, string.Join(",", movies)); + } + public void Handle(MovieFileAddedEvent message) { var movie = message.MovieFile.Movie; diff --git a/src/NzbDrone.Core/Movies/MultipleMoviesFoundException.cs b/src/NzbDrone.Core/Movies/MultipleMoviesFoundException.cs new file mode 100644 index 000000000..1010c15d4 --- /dev/null +++ b/src/NzbDrone.Core/Movies/MultipleMoviesFoundException.cs @@ -0,0 +1,12 @@ +using NzbDrone.Common.Exceptions; + +namespace NzbDrone.Core.Movies +{ + public class MultipleMoviesFoundException : NzbDroneException + { + public MultipleMoviesFoundException(string message, params object[] args) + : base(message, args) + { + } + } +} diff --git a/src/NzbDrone.Core/Movies/QueryExtensions.cs b/src/NzbDrone.Core/Movies/QueryExtensions.cs index ac900eff4..d31e97ea8 100644 --- a/src/NzbDrone.Core/Movies/QueryExtensions.cs +++ b/src/NzbDrone.Core/Movies/QueryExtensions.cs @@ -5,9 +5,9 @@ namespace NzbDrone.Core.Movies { public static class EnumerableExtensions { - public static Movie FirstWithYear(this IEnumerable query, int? year) + public static IEnumerable AllWithYear(this IEnumerable query, int? year) { - return year.HasValue ? query.FirstOrDefault(movie => movie.Year == year || movie.MovieMetadata.Value.SecondaryYear == year) : query.FirstOrDefault(); + return year.HasValue ? query.Where(movie => movie.Year == year || movie.MovieMetadata.Value.SecondaryYear == year) : query; } } }