diff --git a/src/NzbDrone.Core/MediaFiles/MovieImport/Manual/ManualImportService.cs b/src/NzbDrone.Core/MediaFiles/MovieImport/Manual/ManualImportService.cs index 9b6a2bba7..5362af5e7 100644 --- a/src/NzbDrone.Core/MediaFiles/MovieImport/Manual/ManualImportService.cs +++ b/src/NzbDrone.Core/MediaFiles/MovieImport/Manual/ManualImportService.cs @@ -167,7 +167,7 @@ private ManualImportItem ProcessFile(string rootFolder, string baseFolder, strin if (relativeParseInfo != null) { - movie = _movieService.FindByTitle(relativeParseInfo.MovieTitle); + movie = _movieService.FindByTitle(relativeParseInfo.MovieTitle, relativeParseInfo.Year); } } diff --git a/src/NzbDrone.Core/Movies/MovieRepository.cs b/src/NzbDrone.Core/Movies/MovieRepository.cs index 1887cbe0b..58cee2c9c 100644 --- a/src/NzbDrone.Core/Movies/MovieRepository.cs +++ b/src/NzbDrone.Core/Movies/MovieRepository.cs @@ -6,6 +6,7 @@ using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Movies.AlternativeTitles; +using NzbDrone.Core.Movies.Translations; using NzbDrone.Core.Profiles; using NzbDrone.Core.Qualities; @@ -15,7 +16,6 @@ public interface IMovieRepository : IBasicRepository { bool MoviePathExists(string path); List FindByTitles(List titles); - List FindByTitleInexact(string cleanTitle); Movie FindByImdbId(string imdbid); Movie FindByTmdbId(int tmdbid); List FindByTmdbId(List tmdbids); @@ -45,7 +45,7 @@ public MovieRepository(IMainDatabase database, .LeftJoin((m, t) => m.Id == t.MovieId) .LeftJoin((m, f) => m.Id == f.MovieId); - private Movie Map(Dictionary dict, Movie movie, Profile profile, AlternativeTitle altTitle, MovieFile movieFile) + private Movie Map(Dictionary dict, Movie movie, Profile profile, AlternativeTitle altTitle, MovieFile movieFile, MovieTranslation translation = null) { Movie movieEntry; @@ -62,6 +62,11 @@ private Movie Map(Dictionary dict, Movie movie, Profile profile, Alt movieEntry.AlternativeTitles.Add(altTitle); } + if (translation != null) + { + movieEntry.Translations.Add(translation); + } + return movieEntry; } @@ -102,13 +107,19 @@ public bool MoviePathExists(string path) public List FindByTitles(List titles) { var distinct = titles.Distinct().ToList(); - return Query(Builder().OrWhere(x => distinct.Contains(x.CleanTitle)) - .OrWhere(x => distinct.Contains(x.CleanTitle))); - } + var movieDictionary = new Dictionary(); - public List FindByTitleInexact(string cleanTitle) - { - return Query(x => cleanTitle.Contains(x.CleanTitle)); + var builder = Builder() + .LeftJoin((m, tr) => m.Id == tr.MovieId) + .OrWhere(x => distinct.Contains(x.CleanTitle)) + .OrWhere(x => distinct.Contains(x.CleanTitle)) + .OrWhere(x => distinct.Contains(x.CleanTitle)); + + _ = _database.QueryJoined( + builder, + (movie, profile, altTitle, file, trans) => Map(movieDictionary, movie, profile, altTitle, file, trans)); + + return movieDictionary.Values.ToList(); } public Movie FindByImdbId(string imdbid) diff --git a/src/NzbDrone.Core/Movies/MovieService.cs b/src/NzbDrone.Core/Movies/MovieService.cs index 3dd21e090..35e1a964b 100644 --- a/src/NzbDrone.Core/Movies/MovieService.cs +++ b/src/NzbDrone.Core/Movies/MovieService.cs @@ -27,7 +27,6 @@ public interface IMovieService List FindByTmdbId(List tmdbids); Movie FindByTitle(string title); Movie FindByTitle(string title, int year); - Movie FindByTitleInexact(string title, int? year); Movie FindByTitleSlug(string slug); Movie FindByPath(string path); List AllMoviePaths(); @@ -58,10 +57,10 @@ public class MovieService : IMovieService, IHandle, private readonly Logger _logger; public MovieService(IMovieRepository movieRepository, - IEventAggregator eventAggregator, - IConfigService configService, - IBuildMoviePaths moviePathBuilder, - Logger logger) + IEventAggregator eventAggregator, + IConfigService configService, + IBuildMoviePaths moviePathBuilder, + Logger logger) { _movieRepository = movieRepository; _eventAggregator = eventAggregator; @@ -134,15 +133,24 @@ private Movie FindByTitle(string cleanTitle, int? year) result = candidates.Where(movie => movie.CleanTitle == cleanTitleWithArabicNumbers).FirstWithYear(year) ?? candidates.Where(movie => movie.CleanTitle == cleanTitleWithRomanNumbers).FirstWithYear(year); + } - if (result == null) - { - result = candidates - .Where(m => m.AlternativeTitles.Any(t => t.CleanTitle == cleanTitle || - t.CleanTitle == cleanTitleWithArabicNumbers || - t.CleanTitle == cleanTitleWithRomanNumbers)) - .FirstWithYear(year); - } + if (result == null) + { + result = candidates + .Where(m => m.AlternativeTitles.Any(t => t.CleanTitle == cleanTitle || + t.CleanTitle == cleanTitleWithArabicNumbers || + t.CleanTitle == cleanTitleWithRomanNumbers)) + .FirstWithYear(year); + } + + if (result == null) + { + result = candidates + .Where(m => m.Translations.Any(t => t.CleanTitle == cleanTitle || + t.CleanTitle == cleanTitleWithArabicNumbers || + t.CleanTitle == cleanTitleWithRomanNumbers)) + .FirstWithYear(year); } return result; @@ -163,57 +171,6 @@ public List FindByTmdbId(List tmdbids) return _movieRepository.FindByTmdbId(tmdbids); } - private List FindByTitleInexactAll(string title) - { - // find any movie clean title within the provided release title - string cleanTitle = title.CleanMovieTitle(); - var list = _movieRepository.FindByTitleInexact(cleanTitle); - if (!list.Any()) - { - // no movie matched - return list; - } - - // build ordered list of movie by position in the search string - var query = - list.Select(movie => new - { - position = cleanTitle.IndexOf(movie.CleanTitle), - length = movie.CleanTitle.Length, - movie - }) - .Where(s => (s.position >= 0)) - .ToList() - .OrderBy(s => s.position) - .ThenByDescending(s => s.length) - .Select(s => s.movie) - .ToList(); - - return query; - } - - public Movie FindByTitleInexact(string title) - { - var query = FindByTitleInexactAll(title); - - // get the leftmost movie that is the longest - // movie are usually the first thing in release title, so we select the leftmost and longest match - var match = query.First(); - - _logger.Debug("Multiple movie matched {0} from title {1}", match.Title, title); - foreach (var entry in query) - { - _logger.Debug("Multiple movie match candidate: {0} cleantitle: {1}", entry.Title, entry.CleanTitle); - } - - return match; - } - - public Movie FindByTitleInexact(string title, int? year) - { - return FindByTitleInexactAll(title).FirstWithYear(year); - } - public Movie FindByPath(string path) { return _movieRepository.FindByPath(path); diff --git a/src/NzbDrone.Core/Parser/ParsingService.cs b/src/NzbDrone.Core/Parser/ParsingService.cs index 8d48a0db9..e8d77a19c 100644 --- a/src/NzbDrone.Core/Parser/ParsingService.cs +++ b/src/NzbDrone.Core/Parser/ParsingService.cs @@ -115,14 +115,14 @@ public Movie GetMovie(string title) return _movieService.FindByTitle(title); } - var movies = _movieService.FindByTitle(parsedMovieInfo.MovieTitle, parsedMovieInfo.Year); + var movie = _movieService.FindByTitle(parsedMovieInfo.MovieTitle, parsedMovieInfo.Year); - if (movies == null) + if (movie == null) { - movies = _movieService.FindByTitle(parsedMovieInfo.MovieTitle.Replace("DC", "").Trim()); + movie = _movieService.FindByTitle(parsedMovieInfo.MovieTitle.Replace("DC", "").Trim()); } - return movies; + return movie; } public MappingResult Map(ParsedMovieInfo parsedMovieInfo, string imdbId, SearchCriteriaBase searchCriteria = null)