Added another API

pull/3338/head
tidusjar 5 years ago
parent cdcea0ca8b
commit 92195a1598

@ -26,8 +26,8 @@ namespace Ombi.Core.Engine.Demo
public DemoTvSearchEngine(IPrincipal identity, IRequestServiceMain service, ITvMazeApi tvMaze, IMapper mapper,
ITraktApi trakt, IRuleEvaluator r, OmbiUserManager um, ICacheService memCache,
ISettingsService<OmbiSettings> s, IRepository<RequestSubscription> sub, IOptions<DemoLists> lists)
: base(identity, service, tvMaze, mapper, trakt, r, um, memCache, s, sub)
ISettingsService<OmbiSettings> s, IRepository<RequestSubscription> sub, IOptions<DemoLists> lists, IImageService imageService)
: base(identity, service, tvMaze, mapper, trakt, r, um, memCache, s, sub, imageService)
{
_demoLists = lists.Value;
}
@ -55,7 +55,7 @@ namespace Ombi.Core.Engine.Demo
{
continue;
}
retVal.Add(await ProcessResult(tvMazeSearch));
retVal.Add(await ProcessResult(tvMazeSearch, false));
}
return retVal;
}
@ -77,7 +77,7 @@ namespace Ombi.Core.Engine.Demo
}
var movieResult = await TvMazeApi.ShowLookup(tv);
responses.Add(await ProcessResult(movieResult));
responses.Add(await ProcessResult(movieResult, false));
}
return responses;

@ -9,7 +9,7 @@ namespace Ombi.Core.Engine.Interfaces
Task<IEnumerable<SearchTvShowViewModel>> Search(string searchTerm);
Task<SearchTvShowViewModel> GetShowInformation(int tvdbid);
Task<IEnumerable<SearchTvShowViewModel>> Popular();
Task<IEnumerable<SearchTvShowViewModel>> Popular(int currentlyLoaded, int amountToLoad);
Task<IEnumerable<SearchTvShowViewModel>> Popular(int currentlyLoaded, int amountToLoad, bool includeImages = false);
Task<IEnumerable<SearchTvShowViewModel>> Anticipated();
Task<IEnumerable<SearchTvShowViewModel>> Anticipated(int currentlyLoaded, int amountToLoad);
Task<IEnumerable<SearchTvShowViewModel>> Trending();

@ -27,11 +27,14 @@ namespace Ombi.Core.Engine
{
public class TvSearchEngine : BaseMediaEngine, ITvSearchEngine
{
private readonly IImageService _imageService;
public TvSearchEngine(IPrincipal identity, IRequestServiceMain service, ITvMazeApi tvMaze, IMapper mapper,
ITraktApi trakt, IRuleEvaluator r, OmbiUserManager um,
ICacheService memCache, ISettingsService<OmbiSettings> s, IRepository<RequestSubscription> sub)
ICacheService memCache, ISettingsService<OmbiSettings> s, IRepository<RequestSubscription> sub, IImageService imageService)
: base(identity, service, r, um, memCache, s, sub)
{
_imageService = imageService;
TvMazeApi = tvMaze;
Mapper = mapper;
TraktApi = trakt;
@ -54,7 +57,7 @@ namespace Ombi.Core.Engine
{
continue;
}
retVal.Add(await ProcessResult(tvMazeSearch));
retVal.Add(await ProcessResult(tvMazeSearch, false));
}
return retVal;
}
@ -66,7 +69,7 @@ namespace Ombi.Core.Engine
var show = await Cache.GetOrAdd(nameof(GetShowInformation) + tvdbid,
async () => await TvMazeApi.ShowLookupByTheTvDbId(tvdbid), DateTime.Now.AddHours(12));
if (show == null)
{
{
// We don't have enough information
return null;
}
@ -113,7 +116,7 @@ namespace Ombi.Core.Engine
});
}
}
return await ProcessResult(mapped);
return await ProcessResult(mapped, false);
}
public async Task<IEnumerable<SearchTvShowViewModel>> Popular()
@ -123,7 +126,7 @@ namespace Ombi.Core.Engine
return await processed;
}
public async Task<IEnumerable<SearchTvShowViewModel>> Popular(int currentlyLoaded, int amountToLoad)
public async Task<IEnumerable<SearchTvShowViewModel>> Popular(int currentlyLoaded, int amountToLoad, bool includeImages = false)
{
var pages = PaginationHelper.GetNextPages(currentlyLoaded, amountToLoad, ResultLimit);
var results = new List<TraktShow>();
@ -133,7 +136,8 @@ namespace Ombi.Core.Engine
async () => await TraktApi.GetPopularShows(pagesToLoad.Page, ResultLimit), DateTime.Now.AddHours(12));
results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take));
}
var processed = ProcessResults(results);
var processed = ProcessResults(results, includeImages);
return await processed;
}
@ -150,7 +154,7 @@ namespace Ombi.Core.Engine
var pages = PaginationHelper.GetNextPages(currentlyLoaded, amountToLoad, ResultLimit);
var results = new List<TraktShow>();
foreach (var pagesToLoad in pages)
{
{
var apiResult = await Cache.GetOrAdd(nameof(Anticipated) + pagesToLoad.Page,
async () => await TraktApi.GetAnticipatedShows(pagesToLoad.Page, ResultLimit), DateTime.Now.AddHours(12));
results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take));
@ -181,26 +185,35 @@ namespace Ombi.Core.Engine
return await processed;
}
protected async Task<IEnumerable<SearchTvShowViewModel>> ProcessResults<T>(IEnumerable<T> items)
protected async Task<IEnumerable<SearchTvShowViewModel>> ProcessResults<T>(IEnumerable<T> items, bool includeImages = false)
{
var retVal = new List<SearchTvShowViewModel>();
foreach (var tvMazeSearch in items)
{
retVal.Add(await ProcessResult(tvMazeSearch));
retVal.Add(await ProcessResult(tvMazeSearch, includeImages));
}
return retVal;
}
protected async Task<SearchTvShowViewModel> ProcessResult<T>(T tvMazeSearch)
protected async Task<SearchTvShowViewModel> ProcessResult<T>(T tvMazeSearch, bool includeImages)
{
var mapped = Mapper.Map<SearchTvShowViewModel>(tvMazeSearch);
return await ProcessResult(mapped);
return await ProcessResult(mapped, includeImages);
}
private async Task<SearchTvShowViewModel> ProcessResult(SearchTvShowViewModel item)
private async Task<SearchTvShowViewModel> ProcessResult(SearchTvShowViewModel item, bool includeImages)
{
item.TheTvDbId = item.Id.ToString();
if (includeImages)
{
if (item.TheTvDbId.HasValue())
{
item.BackdropPath = await _imageService.GetTvBackground(item.TheTvDbId);
}
}
await RunSearchRules(item);
return item;

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace Ombi.Core
{
public interface IImageService
{
Task<string> GetTvBackground(string tvdbId);
}
}

@ -0,0 +1,47 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Ombi.Api.FanartTv;
using Ombi.Helpers;
using Ombi.Store.Repository;
namespace Ombi.Core
{
public class ImageService : IImageService
{
private readonly IApplicationConfigRepository _configRepository;
private readonly IFanartTvApi _fanartTvApi;
private readonly ICacheService _cache;
public ImageService(IApplicationConfigRepository configRepository, IFanartTvApi fanartTvApi,
ICacheService cache)
{
_configRepository = configRepository;
_fanartTvApi = fanartTvApi;
_cache = cache;
}
public async Task<string> GetTvBackground(string tvdbId)
{
var key = await _cache.GetOrAdd(CacheKeys.FanartTv, async () => await _configRepository.GetAsync(Store.Entities.ConfigurationTypes.FanartTv), DateTime.Now.AddDays(1));
var images = await _cache.GetOrAdd($"{CacheKeys.FanartTv}tv{tvdbId}", async () => await _fanartTvApi.GetTvImages(int.Parse(tvdbId), key.Value), DateTime.Now.AddDays(1));
if (images == null)
{
return string.Empty;
}
if (images.showbackground?.Any() ?? false)
{
var enImage = images.showbackground.Where(x => x.lang == "en").OrderByDescending(x => x.likes).Select(x => x.url).FirstOrDefault();
if (enImage == null)
{
return images.showbackground.OrderByDescending(x => x.likes).Select(x => x.url).FirstOrDefault();
}
return enImage;
}
return string.Empty;
}
}
}

@ -57,5 +57,10 @@ namespace Ombi.Core.Models.Search
// We only have some episodes
public bool PartlyAvailable { get; set; }
public override RequestType Type => RequestType.TvShow;
/// <summary>
/// Only set on the images call
/// </summary>
public string BackdropPath { get; set; }
}
}

@ -22,6 +22,7 @@
<ProjectReference Include="..\Ombi.Api.CouchPotato\Ombi.Api.CouchPotato.csproj" />
<ProjectReference Include="..\Ombi.Api.DogNzb\Ombi.Api.DogNzb.csproj" />
<ProjectReference Include="..\Ombi.Api.Emby\Ombi.Api.Emby.csproj" />
<ProjectReference Include="..\Ombi.Api.FanartTv\Ombi.Api.FanartTv.csproj" />
<ProjectReference Include="..\Ombi.Api.Lidarr\Ombi.Api.Lidarr.csproj" />
<ProjectReference Include="..\Ombi.Api.MusicBrainz\Ombi.Api.MusicBrainz.csproj" />
<ProjectReference Include="..\Ombi.Api.Plex\Ombi.Api.Plex.csproj" />

@ -180,6 +180,7 @@ namespace Ombi.DependencyInjection
services.AddTransient<IEmailProvider, GenericEmailProvider>();
services.AddTransient<INotificationHelper, NotificationHelper>();
services.AddSingleton<ICacheService, CacheService>();
services.AddScoped<IImageService, ImageService>();
services.AddTransient<IDiscordNotification, DiscordNotification>();
services.AddTransient<IEmailNotification, EmailNotification>();

@ -70,6 +70,23 @@ namespace Ombi.Api.TheMovieDb.Models
public ExternalIds ExternalIds { get; set; }
[JsonProperty("keywords")]
public Keywords Keywords { get; set; }
//[JsonProperty("images")]
//public List<Images> Images { get; set; } // add images to append_to_response
}
public class Images
{
[JsonProperty("backdrops")]
public List<ImageContent> Backdrops { get; set; }
[JsonProperty("posters")]
public List<ImageContent> Posters { get; set; }
}
public class ImageContent
{
[JsonProperty("file_path")]
public string FilePath { get; set; }
}
public class Keywords

@ -19,8 +19,8 @@ import { trigger, transition, style, animate } from "@angular/animations";
export class DiscoverComponent implements OnInit {
public discoverResults: IDiscoverCardResult[] = [];
public movies: ISearchMovieResult[];
public tvShows: ISearchTvResult[];
public movies: ISearchMovieResult[] = [];
public tvShows: ISearchTvResult[] = [];
public defaultTvPoster: string;

@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Ombi.Api.FanartTv;
using Ombi.Config;
using Ombi.Core;
using Ombi.Helpers;
using Ombi.Store.Repository;
@ -16,18 +17,20 @@ namespace Ombi.Controllers.V1
public class ImagesController : ControllerBase
{
public ImagesController(IFanartTvApi fanartTvApi, IApplicationConfigRepository config,
IOptions<LandingPageBackground> options, ICacheService c)
IOptions<LandingPageBackground> options, ICacheService c, IImageService imageService)
{
FanartTvApi = fanartTvApi;
Config = config;
Options = options.Value;
_cache = c;
_imageService = imageService;
}
private IFanartTvApi FanartTvApi { get; }
private IApplicationConfigRepository Config { get; }
private LandingPageBackground Options { get; }
private readonly ICacheService _cache;
private readonly IImageService _imageService;
[HttpGet("tv/{tvdbid}")]
public async Task<string> GetTvBanner(int tvdbid)
@ -179,26 +182,8 @@ namespace Ombi.Controllers.V1
{
return string.Empty;
}
var key = await _cache.GetOrAdd(CacheKeys.FanartTv, async () => await Config.GetAsync(Store.Entities.ConfigurationTypes.FanartTv), DateTime.Now.AddDays(1));
var images = await _cache.GetOrAdd($"{CacheKeys.FanartTv}tv{tvdbid}", async () => await FanartTvApi.GetTvImages(tvdbid, key.Value), DateTime.Now.AddDays(1));
if (images == null)
{
return string.Empty;
}
if (images.showbackground?.Any() ?? false)
{
var enImage = images.showbackground.Where(x => x.lang == "en").OrderByDescending(x => x.likes).Select(x => x.url).FirstOrDefault();
if (enImage == null)
{
return images.showbackground.OrderByDescending(x => x.likes).Select(x => x.url).FirstOrDefault();
}
return enImage;
}
return string.Empty;
return await _imageService.GetTvBackground(tvdbid.ToString());
}
[HttpGet("background")]

@ -240,6 +240,19 @@ namespace Ombi.Controllers.V2
return await _tvSearchEngine.Popular(currentPosition, amountToLoad);
}
/// <summary>
/// Returns Popular Tv Shows
/// </summary>
/// <remarks>We use Trakt.tv as the Provider</remarks>
/// <returns></returns>
[HttpGet("tv/popular/{currentPosition}/{amountToLoad}/images")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesDefaultResponseType]
public async Task<IEnumerable<SearchTvShowViewModel>> PopularTvWithImages(int currentPosition, int amountToLoad)
{
return await _tvSearchEngine.Popular(currentPosition, amountToLoad, true);
}
/// <summary>
/// Returns most Anticipated tv shows.
/// </summary>

Loading…
Cancel
Save