1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-19 17:32:38 +01:00

Revert cover mapping for collections, optimize translation mapping

This commit is contained in:
Qstick 2023-09-17 10:19:22 -05:00
parent 017f272201
commit 3d46bd2d8f
4 changed files with 83 additions and 60 deletions

View File

@ -2,9 +2,7 @@
using System.Linq;
using NLog;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Languages;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Movies.Translations;
namespace NzbDrone.Core.Movies
{
@ -45,37 +43,7 @@ public List<MovieMetadata> FindById(List<int> tmdbIds)
public List<MovieMetadata> GetMoviesWithCollections()
{
var movieDictionary = new Dictionary<int, MovieMetadata>();
var builder = new SqlBuilder(_database.DatabaseType)
.LeftJoin<MovieMetadata, MovieTranslation>((mm, t) => mm.Id == t.MovieMetadataId)
.Where<MovieMetadata>(x => x.CollectionTmdbId > 0);
_ = _database.QueryJoined<MovieMetadata, MovieTranslation>(
builder,
(metadata, translation) =>
{
if (!movieDictionary.TryGetValue(metadata.Id, out var movieEntry))
{
movieEntry = metadata;
movieDictionary.Add(movieEntry.Id, movieEntry);
}
if (translation != null)
{
movieEntry.Translations.Add(translation);
}
else
{
// Add a translation to avoid filename builder making another call thinking translations are not loaded
// Optimize this later by pulling translations with metadata always
movieEntry.Translations.Add(new MovieTranslation { Title = movieEntry.Title, Language = Language.English });
}
return movieEntry;
});
return movieDictionary.Values.ToList();
return Query(x => x.CollectionTmdbId > 0);
}
public List<MovieMetadata> GetMoviesByCollectionTmdbId(int collectionId)

View File

@ -1,17 +1,17 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Datastore.Events;
using NzbDrone.Core.MediaCover;
using NzbDrone.Core.Languages;
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Movies;
using NzbDrone.Core.Movies.Collections;
using NzbDrone.Core.Movies.Commands;
using NzbDrone.Core.Movies.Events;
using NzbDrone.Core.Movies.Translations;
using NzbDrone.Core.Organizer;
using NzbDrone.SignalR;
using Radarr.Http;
@ -29,27 +29,30 @@ public class CollectionController : RestControllerWithSignalR<CollectionResource
private readonly IMovieCollectionService _collectionService;
private readonly IMovieService _movieService;
private readonly IMovieMetadataService _movieMetadataService;
private readonly IMovieTranslationService _movieTranslationService;
private readonly IConfigService _configService;
private readonly IBuildFileNames _fileNameBuilder;
private readonly INamingConfigService _namingService;
private readonly IMapCoversToLocal _coverMapper;
private readonly IManageCommandQueue _commandQueueManager;
public CollectionController(IBroadcastSignalRMessage signalRBroadcaster,
IMovieCollectionService collectionService,
IMovieService movieService,
IMovieMetadataService movieMetadataService,
IMovieTranslationService movieTranslationService,
IConfigService configService,
IBuildFileNames fileNameBuilder,
INamingConfigService namingService,
IMapCoversToLocal coverMapper,
IManageCommandQueue commandQueueManager)
: base(signalRBroadcaster)
{
_collectionService = collectionService;
_movieService = movieService;
_movieMetadataService = movieMetadataService;
_movieTranslationService = movieTranslationService;
_configService = configService;
_fileNameBuilder = fileNameBuilder;
_namingService = namingService;
_coverMapper = coverMapper;
_commandQueueManager = commandQueueManager;
}
@ -62,7 +65,6 @@ protected override CollectionResource GetResourceById(int id)
public List<CollectionResource> GetCollections(int? tmdbId)
{
var collectionResources = new List<CollectionResource>();
var coverFileInfos = _coverMapper.GetCoverFileInfos();
if (tmdbId.HasValue)
{
@ -75,7 +77,7 @@ public List<CollectionResource> GetCollections(int? tmdbId)
}
else
{
collectionResources = MapToResource(_collectionService.GetAllCollections(), coverFileInfos).ToList();
collectionResources = MapToResource(_collectionService.GetAllCollections()).ToList();
}
return collectionResources;
@ -137,20 +139,31 @@ public ActionResult UpdateCollections(CollectionUpdateResource resource)
return Accepted(updated);
}
private IEnumerable<CollectionResource> MapToResource(List<MovieCollection> collections, Dictionary<string, FileInfo> coverFileInfos)
private IEnumerable<CollectionResource> MapToResource(List<MovieCollection> collections)
{
// Avoid calling for naming spec on every movie in filenamebuilder
var namingConfig = _namingService.GetConfig();
var collectionMovies = _movieMetadataService.GetMoviesWithCollections();
var existingMoviesTmdbIds = _movieService.AllMovieWithCollectionsTmdbIds();
var configLanguage = (Language)_configService.MovieInfoLanguage;
var allCollectionMovies = _movieMetadataService.GetMoviesWithCollections()
.GroupBy(x => x.CollectionTmdbId)
.ToDictionary(x => x.Key, x => (IEnumerable<MovieMetadata>)x);
var translations = _movieTranslationService.GetAllTranslationsForLanguage(configLanguage);
var tdict = translations.ToDictionary(x => x.MovieMetadataId);
foreach (var collection in collections)
{
var resource = collection.ToResource();
foreach (var movie in collectionMovies.Where(m => m.CollectionTmdbId == collection.TmdbId))
allCollectionMovies.TryGetValue(collection.TmdbId, out var collectionMovies);
foreach (var movie in collectionMovies)
{
var movieResource = movie.ToResource();
var translation = GetTranslationFromDict(tdict, movie, configLanguage);
var movieResource = movie.ToResource(translation);
movieResource.Folder = _fileNameBuilder.GetMovieFolder(new Movie { MovieMetadata = movie }, namingConfig);
if (!existingMoviesTmdbIds.Contains(movie.TmdbId))
@ -161,8 +174,6 @@ private IEnumerable<CollectionResource> MapToResource(List<MovieCollection> coll
resource.Movies.Add(movieResource);
}
MapCoversToLocal(resource.Movies, coverFileInfos);
yield return resource;
}
}
@ -172,13 +183,15 @@ private CollectionResource MapToResource(MovieCollection collection)
var resource = collection.ToResource();
var existingMoviesTmdbIds = _movieService.AllMovieWithCollectionsTmdbIds();
var namingConfig = _namingService.GetConfig();
var configLanguage = (Language)_configService.MovieInfoLanguage;
foreach (var movie in _movieMetadataService.GetMoviesByCollectionTmdbId(collection.TmdbId))
{
var movieResource = movie.ToResource();
movieResource.Folder = _fileNameBuilder.GetMovieFolder(new Movie { MovieMetadata = movie }, namingConfig);
var translations = _movieTranslationService.GetAllTranslationsForMovieMetadata(movie.Id);
var translation = GetMovieTranslation(translations, movie, configLanguage);
_coverMapper.ConvertToLocalUrls(0, movieResource.Images);
var movieResource = movie.ToResource(translation);
movieResource.Folder = _fileNameBuilder.GetMovieFolder(new Movie { MovieMetadata = movie }, namingConfig);
if (!existingMoviesTmdbIds.Contains(movie.TmdbId))
{
@ -191,9 +204,52 @@ private CollectionResource MapToResource(MovieCollection collection)
return resource;
}
private void MapCoversToLocal(IEnumerable<CollectionMovieResource> movies, Dictionary<string, FileInfo> coverFileInfos)
private MovieTranslation GetMovieTranslation(List<MovieTranslation> translations, MovieMetadata movieMetadata, Language configLanguage)
{
_coverMapper.ConvertToLocalUrls(movies.Select(x => Tuple.Create(0, x.Images.AsEnumerable())), coverFileInfos);
if (configLanguage == Language.Original)
{
return new MovieTranslation
{
Title = movieMetadata.OriginalTitle,
Overview = movieMetadata.Overview
};
}
var translation = translations.FirstOrDefault(t => t.Language == configLanguage && t.MovieMetadataId == movieMetadata.Id);
if (translation == null)
{
translation = new MovieTranslation
{
Title = movieMetadata.Title,
Language = Language.English
};
}
return translation;
}
private MovieTranslation GetTranslationFromDict(Dictionary<int, MovieTranslation> translations, MovieMetadata movieMetadata, Language configLanguage)
{
if (configLanguage == Language.Original)
{
return new MovieTranslation
{
Title = movieMetadata.OriginalTitle,
Overview = movieMetadata.Overview
};
}
if (!translations.TryGetValue(movieMetadata.Id, out var translation))
{
translation = new MovieTranslation
{
Title = movieMetadata.Title,
Language = Language.English
};
}
return translation;
}
[NonAction]

View File

@ -1,6 +1,7 @@
using System.Collections.Generic;
using NzbDrone.Core.MediaCover;
using NzbDrone.Core.Movies;
using NzbDrone.Core.Movies.Translations;
namespace Radarr.Api.V3.Collections
{
@ -22,18 +23,21 @@ public class CollectionMovieResource
public static class CollectionMovieResourceMapper
{
public static CollectionMovieResource ToResource(this MovieMetadata model)
public static CollectionMovieResource ToResource(this MovieMetadata model, MovieTranslation movieTranslation = null)
{
if (model == null)
{
return null;
}
var translatedTitle = movieTranslation?.Title ?? model.Title;
var translatedOverview = movieTranslation?.Overview ?? model.Overview;
return new CollectionMovieResource
{
TmdbId = model.TmdbId,
Title = model.Title,
Overview = model.Overview,
Title = translatedTitle,
Overview = translatedOverview,
SortTitle = model.SortTitle,
Images = model.Images,
ImdbId = model.ImdbId,

View File

@ -28,7 +28,6 @@ public class ImportListMoviesController : Controller
private readonly IImportExclusionsService _importExclusionService;
private readonly INamingConfigService _namingService;
private readonly IMovieTranslationService _movieTranslationService;
private readonly IMapCoversToLocal _coverMapper;
private readonly IConfigService _configService;
public ImportListMoviesController(IMovieService movieService,
@ -40,7 +39,6 @@ public ImportListMoviesController(IMovieService movieService,
IImportExclusionsService importExclusionsService,
INamingConfigService namingService,
IMovieTranslationService movieTranslationService,
IMapCoversToLocal coverMapper,
IConfigService configService)
{
_movieService = movieService;
@ -52,7 +50,6 @@ public ImportListMoviesController(IMovieService movieService,
_importExclusionService = importExclusionsService;
_namingService = namingService;
_movieTranslationService = movieTranslationService;
_coverMapper = coverMapper;
_configService = configService;
}
@ -118,7 +115,6 @@ private IEnumerable<ImportListMoviesResource> MapToResource(IEnumerable<Movie> m
foreach (var currentMovie in movies)
{
var resource = currentMovie.ToResource();
_coverMapper.ConvertToLocalUrls(0, resource.Images);
var poster = currentMovie.MovieMetadata.Value.Images.FirstOrDefault(c => c.CoverType == MediaCoverTypes.Poster);
if (poster != null)
@ -148,7 +144,6 @@ private IEnumerable<ImportListMoviesResource> MapToResource(IEnumerable<ImportLi
foreach (var currentMovie in movies)
{
var resource = currentMovie.ToResource();
_coverMapper.ConvertToLocalUrls(0, resource.Images);
var poster = currentMovie.MovieMetadata.Value.Images.FirstOrDefault(c => c.CoverType == MediaCoverTypes.Poster);
if (poster != null)