Added new Image posters API

pull/4150/head v4.0.1333
tidusjar 4 years ago
parent 5bf8703c66
commit 560454565d

@ -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<OmbiUser> GetUser()
{
if(!Username.HasValue())
{
return null;
}
var username = Username.ToUpper();
return _user ?? (_user = await UserManager.Users.FirstOrDefaultAsync(x => x.NormalizedUserName == username));
}

@ -19,7 +19,7 @@ namespace Ombi.Core.Engine.Interfaces
Task<IEnumerable<SearchMovieViewModel>> NowPlayingMovies(int currentPosition, int amountToLoad);
Task<MovieCollectionsViewModel> GetCollection(int collectionId, CancellationToken cancellationToken, string langCode = null);
Task<int> GetTvDbId(int theMovieDbId);
Task<IEnumerable<SearchMovieViewModel>> PopularMovies(int currentlyLoaded, int toLoad, CancellationToken cancellationToken);
Task<IEnumerable<SearchMovieViewModel>> PopularMovies(int currentlyLoaded, int toLoad, CancellationToken cancellationToken, string langCustomCode = null);
Task<IEnumerable<SearchMovieViewModel>> TopRatedMovies(int currentlyLoaded, int toLoad);
Task<IEnumerable<SearchMovieViewModel>> UpcomingMovies(int currentlyLoaded, int toLoad);
Task<ActorCredits> GetMoviesByActor(int actorId, string langCode);

@ -11,7 +11,7 @@ namespace Ombi.Core
Task<SearchFullInfoTvShowViewModel> GetShowInformation(string tvdbid, CancellationToken token);
Task<SearchFullInfoTvShowViewModel> GetShowByRequest(int requestId, CancellationToken token);
Task<IEnumerable<StreamingData>> GetStreamInformation(int movieDbId, CancellationToken cancellationToken);
Task<IEnumerable<SearchTvShowViewModel>> Popular(int currentlyLoaded, int amountToLoad);
Task<IEnumerable<SearchTvShowViewModel>> Popular(int currentlyLoaded, int amountToLoad, string langCustomCode = null);
Task<IEnumerable<SearchTvShowViewModel>> Anticipated(int currentlyLoaded, int amountToLoad);
Task<IEnumerable<SearchTvShowViewModel>> Trending(int currentlyLoaded, int amountToLoad);
}

@ -124,9 +124,9 @@ namespace Ombi.Core.Engine.V2
/// Gets popular movies by paging
/// </summary>
/// <returns></returns>
public async Task<IEnumerable<SearchMovieViewModel>> PopularMovies(int currentlyLoaded, int toLoad, CancellationToken cancellationToken)
public async Task<IEnumerable<SearchMovieViewModel>> 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);

@ -107,9 +107,9 @@ namespace Ombi.Core.Engine.V2
return await ProcessResult(mapped);
}
public async Task<IEnumerable<SearchTvShowViewModel>> Popular(int currentlyLoaded, int amountToLoad)
public async Task<IEnumerable<SearchTvShowViewModel>> 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<MovieDbSearchResult>();

@ -58,9 +58,6 @@ namespace Ombi.Core.Models.Search
public bool PartlyAvailable { get; set; }
public override RequestType Type => RequestType.TvShow;
/// <summary>
/// Only set on the images call
/// </summary>
public string BackdropPath { get; set; }
}
}

@ -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<LandingPageBackground> options, ICacheService c, IImageService imageService)
IOptions<LandingPageBackground> 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<string> GetTvBanner(int tvdbid)
@ -61,6 +67,50 @@ namespace Ombi.Controllers.V1
return string.Empty;
}
[HttpGet("poster")]
public async Task<string> 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<string> GetMoviePoster(string movieDbId)
{

Loading…
Cancel
Save