You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
135 lines
5.5 KiB
135 lines
5.5 KiB
4 years ago
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
3 years ago
|
using Microsoft.AspNetCore.Mvc;
|
||
4 years ago
|
using NzbDrone.Common.Extensions;
|
||
4 years ago
|
using NzbDrone.Core.Configuration;
|
||
4 years ago
|
using NzbDrone.Core.ImportLists;
|
||
4 years ago
|
using NzbDrone.Core.ImportLists.ImportExclusions;
|
||
|
using NzbDrone.Core.ImportLists.ImportListMovies;
|
||
4 years ago
|
using NzbDrone.Core.Languages;
|
||
4 years ago
|
using NzbDrone.Core.MediaCover;
|
||
|
using NzbDrone.Core.MetadataSource;
|
||
|
using NzbDrone.Core.Movies;
|
||
|
using NzbDrone.Core.Organizer;
|
||
|
using Radarr.Http;
|
||
|
|
||
|
namespace Radarr.Api.V3.ImportLists
|
||
|
{
|
||
3 years ago
|
[V3ApiController("importlist/movie")]
|
||
|
public class ImportListMoviesController : Controller
|
||
4 years ago
|
{
|
||
|
private readonly IMovieService _movieService;
|
||
|
private readonly IProvideMovieInfo _movieInfo;
|
||
|
private readonly IBuildFileNames _fileNameBuilder;
|
||
|
private readonly IImportListMovieService _listMovieService;
|
||
4 years ago
|
private readonly IImportListFactory _importListFactory;
|
||
4 years ago
|
private readonly IImportExclusionsService _importExclusionService;
|
||
4 years ago
|
private readonly IConfigService _configService;
|
||
4 years ago
|
|
||
3 years ago
|
public ImportListMoviesController(IMovieService movieService,
|
||
4 years ago
|
IProvideMovieInfo movieInfo,
|
||
|
IBuildFileNames fileNameBuilder,
|
||
|
IImportListMovieService listMovieService,
|
||
4 years ago
|
IImportListFactory importListFactory,
|
||
4 years ago
|
IImportExclusionsService importExclusionsService,
|
||
|
IConfigService configService)
|
||
4 years ago
|
{
|
||
|
_movieService = movieService;
|
||
|
_movieInfo = movieInfo;
|
||
|
_fileNameBuilder = fileNameBuilder;
|
||
|
_listMovieService = listMovieService;
|
||
4 years ago
|
_importListFactory = importListFactory;
|
||
4 years ago
|
_importExclusionService = importExclusionsService;
|
||
4 years ago
|
_configService = configService;
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
[HttpGet]
|
||
|
public object GetDiscoverMovies(bool includeRecommendations = false)
|
||
4 years ago
|
{
|
||
4 years ago
|
var movieLanguge = (Language)_configService.MovieInfoLanguage;
|
||
4 years ago
|
|
||
|
var realResults = new List<ImportListMoviesResource>();
|
||
|
var listExclusions = _importExclusionService.GetAllExclusions();
|
||
|
var existingTmdbIds = _movieService.AllMovieTmdbIds();
|
||
|
|
||
|
if (includeRecommendations)
|
||
|
{
|
||
|
var mapped = new List<Movie>();
|
||
|
|
||
|
var results = _movieService.GetRecommendedTmdbIds();
|
||
|
|
||
|
if (results.Count > 0)
|
||
|
{
|
||
|
mapped = _movieInfo.GetBulkMovieInfo(results);
|
||
|
}
|
||
|
|
||
4 years ago
|
realResults.AddRange(MapToResource(mapped.Where(x => x != null), movieLanguge));
|
||
4 years ago
|
realResults.ForEach(x => x.IsRecommendation = true);
|
||
|
}
|
||
|
|
||
4 years ago
|
var listMovies = MapToResource(_listMovieService.GetAllForLists(_importListFactory.Enabled().Select(x => x.Definition.Id).ToList()), movieLanguge).ToList();
|
||
4 years ago
|
|
||
|
realResults.AddRange(listMovies);
|
||
|
|
||
|
var groupedListMovies = realResults.GroupBy(x => x.TmdbId);
|
||
|
|
||
|
// Distinct Movies
|
||
|
realResults = groupedListMovies.Select(x =>
|
||
|
{
|
||
|
var movie = x.First();
|
||
|
|
||
|
movie.Lists = x.SelectMany(m => m.Lists).ToHashSet();
|
||
|
movie.IsExcluded = listExclusions.Any(e => e.TmdbId == movie.TmdbId);
|
||
|
movie.IsExisting = existingTmdbIds.Any(e => e == movie.TmdbId);
|
||
|
movie.IsRecommendation = x.Any(m => m.IsRecommendation);
|
||
|
|
||
|
return movie;
|
||
|
}).ToList();
|
||
|
|
||
|
return realResults;
|
||
|
}
|
||
|
|
||
4 years ago
|
private IEnumerable<ImportListMoviesResource> MapToResource(IEnumerable<Movie> movies, Language language)
|
||
4 years ago
|
{
|
||
|
foreach (var currentMovie in movies)
|
||
|
{
|
||
|
var resource = DiscoverMoviesResourceMapper.ToResource(currentMovie);
|
||
|
var poster = currentMovie.Images.FirstOrDefault(c => c.CoverType == MediaCoverTypes.Poster);
|
||
|
if (poster != null)
|
||
|
{
|
||
|
resource.RemotePoster = poster.Url;
|
||
|
}
|
||
|
|
||
4 years ago
|
var translation = currentMovie.Translations.FirstOrDefault(t => t.Language == language);
|
||
|
|
||
|
resource.Title = translation?.Title ?? resource.Title;
|
||
|
resource.Overview = translation?.Overview ?? resource.Overview;
|
||
4 years ago
|
resource.Folder = _fileNameBuilder.GetMovieFolder(currentMovie);
|
||
|
|
||
|
yield return resource;
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
private IEnumerable<ImportListMoviesResource> MapToResource(IEnumerable<ImportListMovie> movies, Language language)
|
||
4 years ago
|
{
|
||
|
foreach (var currentMovie in movies)
|
||
|
{
|
||
|
var resource = DiscoverMoviesResourceMapper.ToResource(currentMovie);
|
||
|
var poster = currentMovie.Images.FirstOrDefault(c => c.CoverType == MediaCoverTypes.Poster);
|
||
|
if (poster != null)
|
||
|
{
|
||
|
resource.RemotePoster = poster.Url;
|
||
|
}
|
||
|
|
||
4 years ago
|
var translation = currentMovie.Translations.FirstOrDefault(t => t.Language == language);
|
||
|
|
||
|
resource.Title = translation?.Title ?? resource.Title;
|
||
|
resource.Overview = translation?.Overview ?? resource.Overview;
|
||
4 years ago
|
resource.Folder = _fileNameBuilder.GetMovieFolder(new Movie { Title = currentMovie.Title, Year = currentMovie.Year, ImdbId = currentMovie.ImdbId, TmdbId = currentMovie.TmdbId });
|
||
|
|
||
|
yield return resource;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|