From 560454565d82e599d11bcca1e93db2bbb18833b6 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Sun, 11 Apr 2021 22:21:42 +0100 Subject: [PATCH] Added new Image posters API --- src/Ombi.Core/Engine/Interfaces/BaseEngine.cs | 5 ++ .../Engine/Interfaces/IMovieEngineV2.cs | 2 +- .../Engine/Interfaces/ITvSearchEngineV2.cs | 2 +- .../Engine/V2/MovieSearchEngineV2.cs | 4 +- src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs | 4 +- .../Models/Search/SearchTvShowViewModel.cs | 3 -- src/Ombi/Controllers/V1/ImagesController.cs | 52 ++++++++++++++++++- 7 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/Ombi.Core/Engine/Interfaces/BaseEngine.cs b/src/Ombi.Core/Engine/Interfaces/BaseEngine.cs index 8ddf27656..6fd22bced 100644 --- a/src/Ombi.Core/Engine/Interfaces/BaseEngine.cs +++ b/src/Ombi.Core/Engine/Interfaces/BaseEngine.cs @@ -9,6 +9,7 @@ using Ombi.Store.Entities.Requests; using Ombi.Store.Entities; using Microsoft.EntityFrameworkCore; using Ombi.Core.Authentication; +using Ombi.Helpers; namespace Ombi.Core.Engine.Interfaces { @@ -29,6 +30,10 @@ namespace Ombi.Core.Engine.Interfaces private OmbiUser _user; protected async Task GetUser() { + if(!Username.HasValue()) + { + return null; + } var username = Username.ToUpper(); return _user ?? (_user = await UserManager.Users.FirstOrDefaultAsync(x => x.NormalizedUserName == username)); } diff --git a/src/Ombi.Core/Engine/Interfaces/IMovieEngineV2.cs b/src/Ombi.Core/Engine/Interfaces/IMovieEngineV2.cs index c4bd0aa0e..442359f0f 100644 --- a/src/Ombi.Core/Engine/Interfaces/IMovieEngineV2.cs +++ b/src/Ombi.Core/Engine/Interfaces/IMovieEngineV2.cs @@ -19,7 +19,7 @@ namespace Ombi.Core.Engine.Interfaces Task> NowPlayingMovies(int currentPosition, int amountToLoad); Task GetCollection(int collectionId, CancellationToken cancellationToken, string langCode = null); Task GetTvDbId(int theMovieDbId); - Task> PopularMovies(int currentlyLoaded, int toLoad, CancellationToken cancellationToken); + Task> PopularMovies(int currentlyLoaded, int toLoad, CancellationToken cancellationToken, string langCustomCode = null); Task> TopRatedMovies(int currentlyLoaded, int toLoad); Task> UpcomingMovies(int currentlyLoaded, int toLoad); Task GetMoviesByActor(int actorId, string langCode); diff --git a/src/Ombi.Core/Engine/Interfaces/ITvSearchEngineV2.cs b/src/Ombi.Core/Engine/Interfaces/ITvSearchEngineV2.cs index 2fc78b6bb..3e2d859c8 100644 --- a/src/Ombi.Core/Engine/Interfaces/ITvSearchEngineV2.cs +++ b/src/Ombi.Core/Engine/Interfaces/ITvSearchEngineV2.cs @@ -11,7 +11,7 @@ namespace Ombi.Core Task GetShowInformation(string tvdbid, CancellationToken token); Task GetShowByRequest(int requestId, CancellationToken token); Task> GetStreamInformation(int movieDbId, CancellationToken cancellationToken); - Task> Popular(int currentlyLoaded, int amountToLoad); + Task> Popular(int currentlyLoaded, int amountToLoad, string langCustomCode = null); Task> Anticipated(int currentlyLoaded, int amountToLoad); Task> Trending(int currentlyLoaded, int amountToLoad); } diff --git a/src/Ombi.Core/Engine/V2/MovieSearchEngineV2.cs b/src/Ombi.Core/Engine/V2/MovieSearchEngineV2.cs index 3714e58a8..e651cc719 100644 --- a/src/Ombi.Core/Engine/V2/MovieSearchEngineV2.cs +++ b/src/Ombi.Core/Engine/V2/MovieSearchEngineV2.cs @@ -124,9 +124,9 @@ namespace Ombi.Core.Engine.V2 /// Gets popular movies by paging /// /// - public async Task> PopularMovies(int currentlyLoaded, int toLoad, CancellationToken cancellationToken) + public async Task> PopularMovies(int currentlyLoaded, int toLoad, CancellationToken cancellationToken, string langCustomCode = null) { - var langCode = await DefaultLanguageCode(null); + var langCode = await DefaultLanguageCode(langCustomCode); var pages = PaginationHelper.GetNextPages(currentlyLoaded, toLoad, _theMovieDbMaxPageItems); diff --git a/src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs b/src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs index 9b9b40906..952c897d6 100644 --- a/src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs +++ b/src/Ombi.Core/Engine/V2/TvSearchEngineV2.cs @@ -107,9 +107,9 @@ namespace Ombi.Core.Engine.V2 return await ProcessResult(mapped); } - public async Task> Popular(int currentlyLoaded, int amountToLoad) + public async Task> Popular(int currentlyLoaded, int amountToLoad, string langCustomCode = null) { - var langCode = await DefaultLanguageCode(null); + var langCode = await DefaultLanguageCode(langCustomCode); var pages = PaginationHelper.GetNextPages(currentlyLoaded, amountToLoad, ResultLimit); var results = new List(); diff --git a/src/Ombi.Core/Models/Search/SearchTvShowViewModel.cs b/src/Ombi.Core/Models/Search/SearchTvShowViewModel.cs index dfd038a91..1a3f47175 100644 --- a/src/Ombi.Core/Models/Search/SearchTvShowViewModel.cs +++ b/src/Ombi.Core/Models/Search/SearchTvShowViewModel.cs @@ -58,9 +58,6 @@ namespace Ombi.Core.Models.Search public bool PartlyAvailable { get; set; } public override RequestType Type => RequestType.TvShow; - /// - /// Only set on the images call - /// public string BackdropPath { get; set; } } } \ No newline at end of file diff --git a/src/Ombi/Controllers/V1/ImagesController.cs b/src/Ombi/Controllers/V1/ImagesController.cs index c119efe70..847cd8777 100644 --- a/src/Ombi/Controllers/V1/ImagesController.cs +++ b/src/Ombi/Controllers/V1/ImagesController.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Options; using Ombi.Api.FanartTv; using Ombi.Config; using Ombi.Core; +using Ombi.Core.Engine.Interfaces; using Ombi.Helpers; using Ombi.Store.Repository; @@ -17,13 +18,16 @@ namespace Ombi.Controllers.V1 public class ImagesController : ControllerBase { public ImagesController(IFanartTvApi fanartTvApi, IApplicationConfigRepository config, - IOptions options, ICacheService c, IImageService imageService) + IOptions options, ICacheService c, IImageService imageService, + IMovieEngineV2 movieEngineV2, ITVSearchEngineV2 tVSearchEngineV2) { FanartTvApi = fanartTvApi; Config = config; Options = options.Value; _cache = c; _imageService = imageService; + _movieEngineV2 = movieEngineV2; + _tvSearchEngineV2 = tVSearchEngineV2; } private IFanartTvApi FanartTvApi { get; } @@ -31,6 +35,8 @@ namespace Ombi.Controllers.V1 private LandingPageBackground Options { get; } private readonly ICacheService _cache; private readonly IImageService _imageService; + private readonly IMovieEngineV2 _movieEngineV2; + private readonly ITVSearchEngineV2 _tvSearchEngineV2; [HttpGet("tv/{tvdbid}")] public async Task GetTvBanner(int tvdbid) @@ -61,6 +67,50 @@ namespace Ombi.Controllers.V1 return string.Empty; } + [HttpGet("poster")] + public async Task GetRandomPoster() + { + var key = await _cache.GetOrAdd(CacheKeys.FanartTv, async () => await Config.GetAsync(Store.Entities.ConfigurationTypes.FanartTv), DateTime.Now.AddDays(1)); + var rand = new Random(); + var val = rand.Next(1, 3); + if (val == 1) + { + var movies = (await _movieEngineV2.PopularMovies(0, 10, HttpContext.RequestAborted ,"en")).ToArray(); + var selectedMovieIndex = rand.Next(movies.Count()); + var movie = movies[selectedMovieIndex]; + + var images = await _cache.GetOrAdd($"{CacheKeys.FanartTv}movie{movie.Id}", async () => await FanartTvApi.GetMovieImages(movie.Id.ToString(), key.Value), DateTime.Now.AddDays(1)); + if (images == null) + { + return string.Empty; + } + + if (images.movieposter?.Any() ?? false) + { + var enImage = images.movieposter.Where(x => x.lang == "en").OrderByDescending(x => x.likes).Select(x => x.url).FirstOrDefault(); + if (enImage == null) + { + return images.movieposter.OrderByDescending(x => x.likes).Select(x => x.url).FirstOrDefault(); + } + return enImage; + } + + if (images.moviethumb?.Any() ?? false) + { + return images.moviethumb.OrderBy(x => x.likes).Select(x => x.url).FirstOrDefault(); + } + } + else + { + var tv = (await _tvSearchEngineV2.Popular(0, 10, "en")).ToArray(); + var selectedMovieIndex = rand.Next(tv.Count()); + var selected = tv[selectedMovieIndex]; + + return $"https://image.tmdb.org/t/p/original{selected.BackdropPath}"; + } + return ""; + } + [HttpGet("poster/movie/{movieDbId}")] public async Task GetMoviePoster(string movieDbId) {