1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-05 02:22:31 +01:00

New: Use Translations for Movie Mapping

This commit is contained in:
Qstick 2020-07-21 23:04:20 -04:00
parent 6d4be67e36
commit e2165eb51b
4 changed files with 45 additions and 77 deletions

View File

@ -167,7 +167,7 @@ private ManualImportItem ProcessFile(string rootFolder, string baseFolder, strin
if (relativeParseInfo != null) if (relativeParseInfo != null)
{ {
movie = _movieService.FindByTitle(relativeParseInfo.MovieTitle); movie = _movieService.FindByTitle(relativeParseInfo.MovieTitle, relativeParseInfo.Year);
} }
} }

View File

@ -6,6 +6,7 @@
using NzbDrone.Core.MediaFiles; using NzbDrone.Core.MediaFiles;
using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Movies.AlternativeTitles; using NzbDrone.Core.Movies.AlternativeTitles;
using NzbDrone.Core.Movies.Translations;
using NzbDrone.Core.Profiles; using NzbDrone.Core.Profiles;
using NzbDrone.Core.Qualities; using NzbDrone.Core.Qualities;
@ -15,7 +16,6 @@ public interface IMovieRepository : IBasicRepository<Movie>
{ {
bool MoviePathExists(string path); bool MoviePathExists(string path);
List<Movie> FindByTitles(List<string> titles); List<Movie> FindByTitles(List<string> titles);
List<Movie> FindByTitleInexact(string cleanTitle);
Movie FindByImdbId(string imdbid); Movie FindByImdbId(string imdbid);
Movie FindByTmdbId(int tmdbid); Movie FindByTmdbId(int tmdbid);
List<Movie> FindByTmdbId(List<int> tmdbids); List<Movie> FindByTmdbId(List<int> tmdbids);
@ -45,7 +45,7 @@ public MovieRepository(IMainDatabase database,
.LeftJoin<Movie, AlternativeTitle>((m, t) => m.Id == t.MovieId) .LeftJoin<Movie, AlternativeTitle>((m, t) => m.Id == t.MovieId)
.LeftJoin<Movie, MovieFile>((m, f) => m.Id == f.MovieId); .LeftJoin<Movie, MovieFile>((m, f) => m.Id == f.MovieId);
private Movie Map(Dictionary<int, Movie> dict, Movie movie, Profile profile, AlternativeTitle altTitle, MovieFile movieFile) private Movie Map(Dictionary<int, Movie> dict, Movie movie, Profile profile, AlternativeTitle altTitle, MovieFile movieFile, MovieTranslation translation = null)
{ {
Movie movieEntry; Movie movieEntry;
@ -62,6 +62,11 @@ private Movie Map(Dictionary<int, Movie> dict, Movie movie, Profile profile, Alt
movieEntry.AlternativeTitles.Add(altTitle); movieEntry.AlternativeTitles.Add(altTitle);
} }
if (translation != null)
{
movieEntry.Translations.Add(translation);
}
return movieEntry; return movieEntry;
} }
@ -102,13 +107,19 @@ public bool MoviePathExists(string path)
public List<Movie> FindByTitles(List<string> titles) public List<Movie> FindByTitles(List<string> titles)
{ {
var distinct = titles.Distinct().ToList(); var distinct = titles.Distinct().ToList();
return Query(Builder().OrWhere<Movie>(x => distinct.Contains(x.CleanTitle)) var movieDictionary = new Dictionary<int, Movie>();
.OrWhere<AlternativeTitle>(x => distinct.Contains(x.CleanTitle)));
}
public List<Movie> FindByTitleInexact(string cleanTitle) var builder = Builder()
{ .LeftJoin<Movie, MovieTranslation>((m, tr) => m.Id == tr.MovieId)
return Query(x => cleanTitle.Contains(x.CleanTitle)); .OrWhere<Movie>(x => distinct.Contains(x.CleanTitle))
.OrWhere<AlternativeTitle>(x => distinct.Contains(x.CleanTitle))
.OrWhere<MovieTranslation>(x => distinct.Contains(x.CleanTitle));
_ = _database.QueryJoined<Movie, Profile, AlternativeTitle, MovieFile, MovieTranslation>(
builder,
(movie, profile, altTitle, file, trans) => Map(movieDictionary, movie, profile, altTitle, file, trans));
return movieDictionary.Values.ToList();
} }
public Movie FindByImdbId(string imdbid) public Movie FindByImdbId(string imdbid)

View File

@ -27,7 +27,6 @@ public interface IMovieService
List<Movie> FindByTmdbId(List<int> tmdbids); List<Movie> FindByTmdbId(List<int> tmdbids);
Movie FindByTitle(string title); Movie FindByTitle(string title);
Movie FindByTitle(string title, int year); Movie FindByTitle(string title, int year);
Movie FindByTitleInexact(string title, int? year);
Movie FindByTitleSlug(string slug); Movie FindByTitleSlug(string slug);
Movie FindByPath(string path); Movie FindByPath(string path);
List<string> AllMoviePaths(); List<string> AllMoviePaths();
@ -134,6 +133,7 @@ private Movie FindByTitle(string cleanTitle, int? year)
result = result =
candidates.Where(movie => movie.CleanTitle == cleanTitleWithArabicNumbers).FirstWithYear(year) ?? candidates.Where(movie => movie.CleanTitle == cleanTitleWithArabicNumbers).FirstWithYear(year) ??
candidates.Where(movie => movie.CleanTitle == cleanTitleWithRomanNumbers).FirstWithYear(year); candidates.Where(movie => movie.CleanTitle == cleanTitleWithRomanNumbers).FirstWithYear(year);
}
if (result == null) if (result == null)
{ {
@ -143,6 +143,14 @@ private Movie FindByTitle(string cleanTitle, int? year)
t.CleanTitle == cleanTitleWithRomanNumbers)) t.CleanTitle == cleanTitleWithRomanNumbers))
.FirstWithYear(year); .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; return result;
@ -163,57 +171,6 @@ public List<Movie> FindByTmdbId(List<int> tmdbids)
return _movieRepository.FindByTmdbId(tmdbids); return _movieRepository.FindByTmdbId(tmdbids);
} }
private List<Movie> 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) public Movie FindByPath(string path)
{ {
return _movieRepository.FindByPath(path); return _movieRepository.FindByPath(path);

View File

@ -115,14 +115,14 @@ public Movie GetMovie(string title)
return _movieService.FindByTitle(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) public MappingResult Map(ParsedMovieInfo parsedMovieInfo, string imdbId, SearchCriteriaBase searchCriteria = null)