Added another API

This commit is contained in:
tidusjar 2019-12-06 00:32:08 +00:00
parent cdcea0ca8b
commit 92195a1598
12 changed files with 131 additions and 40 deletions

View file

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

View file

@ -9,7 +9,7 @@ namespace Ombi.Core.Engine.Interfaces
Task<IEnumerable<SearchTvShowViewModel>> Search(string searchTerm); Task<IEnumerable<SearchTvShowViewModel>> Search(string searchTerm);
Task<SearchTvShowViewModel> GetShowInformation(int tvdbid); Task<SearchTvShowViewModel> GetShowInformation(int tvdbid);
Task<IEnumerable<SearchTvShowViewModel>> Popular(); 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();
Task<IEnumerable<SearchTvShowViewModel>> Anticipated(int currentlyLoaded, int amountToLoad); Task<IEnumerable<SearchTvShowViewModel>> Anticipated(int currentlyLoaded, int amountToLoad);
Task<IEnumerable<SearchTvShowViewModel>> Trending(); Task<IEnumerable<SearchTvShowViewModel>> Trending();

View file

@ -27,11 +27,14 @@ namespace Ombi.Core.Engine
{ {
public class TvSearchEngine : BaseMediaEngine, ITvSearchEngine public class TvSearchEngine : BaseMediaEngine, ITvSearchEngine
{ {
private readonly IImageService _imageService;
public TvSearchEngine(IPrincipal identity, IRequestServiceMain service, ITvMazeApi tvMaze, IMapper mapper, public TvSearchEngine(IPrincipal identity, IRequestServiceMain service, ITvMazeApi tvMaze, IMapper mapper,
ITraktApi trakt, IRuleEvaluator r, OmbiUserManager um, 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) : base(identity, service, r, um, memCache, s, sub)
{ {
_imageService = imageService;
TvMazeApi = tvMaze; TvMazeApi = tvMaze;
Mapper = mapper; Mapper = mapper;
TraktApi = trakt; TraktApi = trakt;
@ -54,7 +57,7 @@ namespace Ombi.Core.Engine
{ {
continue; continue;
} }
retVal.Add(await ProcessResult(tvMazeSearch)); retVal.Add(await ProcessResult(tvMazeSearch, false));
} }
return retVal; return retVal;
} }
@ -113,7 +116,7 @@ namespace Ombi.Core.Engine
}); });
} }
} }
return await ProcessResult(mapped); return await ProcessResult(mapped, false);
} }
public async Task<IEnumerable<SearchTvShowViewModel>> Popular() public async Task<IEnumerable<SearchTvShowViewModel>> Popular()
@ -123,7 +126,7 @@ namespace Ombi.Core.Engine
return await processed; 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 pages = PaginationHelper.GetNextPages(currentlyLoaded, amountToLoad, ResultLimit);
var results = new List<TraktShow>(); var results = new List<TraktShow>();
@ -133,7 +136,8 @@ namespace Ombi.Core.Engine
async () => await TraktApi.GetPopularShows(pagesToLoad.Page, ResultLimit), DateTime.Now.AddHours(12)); async () => await TraktApi.GetPopularShows(pagesToLoad.Page, ResultLimit), DateTime.Now.AddHours(12));
results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take)); results.AddRange(apiResult.Skip(pagesToLoad.Skip).Take(pagesToLoad.Take));
} }
var processed = ProcessResults(results);
var processed = ProcessResults(results, includeImages);
return await processed; return await processed;
} }
@ -181,25 +185,34 @@ namespace Ombi.Core.Engine
return await processed; 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>(); var retVal = new List<SearchTvShowViewModel>();
foreach (var tvMazeSearch in items) foreach (var tvMazeSearch in items)
{ {
retVal.Add(await ProcessResult(tvMazeSearch)); retVal.Add(await ProcessResult(tvMazeSearch, includeImages));
} }
return retVal; 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); 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(); item.TheTvDbId = item.Id.ToString();
if (includeImages)
{
if (item.TheTvDbId.HasValue())
{
item.BackdropPath = await _imageService.GetTvBackground(item.TheTvDbId);
}
}
await RunSearchRules(item); await RunSearchRules(item);

View file

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

View file

@ -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;
}
}
}

View file

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

View file

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

View file

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

View file

@ -70,6 +70,23 @@ namespace Ombi.Api.TheMovieDb.Models
public ExternalIds ExternalIds { get; set; } public ExternalIds ExternalIds { get; set; }
[JsonProperty("keywords")] [JsonProperty("keywords")]
public Keywords Keywords { get; set; } 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 public class Keywords

View file

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

View file

@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Ombi.Api.FanartTv; using Ombi.Api.FanartTv;
using Ombi.Config; using Ombi.Config;
using Ombi.Core;
using Ombi.Helpers; using Ombi.Helpers;
using Ombi.Store.Repository; using Ombi.Store.Repository;
@ -16,18 +17,20 @@ namespace Ombi.Controllers.V1
public class ImagesController : ControllerBase public class ImagesController : ControllerBase
{ {
public ImagesController(IFanartTvApi fanartTvApi, IApplicationConfigRepository config, public ImagesController(IFanartTvApi fanartTvApi, IApplicationConfigRepository config,
IOptions<LandingPageBackground> options, ICacheService c) IOptions<LandingPageBackground> options, ICacheService c, IImageService imageService)
{ {
FanartTvApi = fanartTvApi; FanartTvApi = fanartTvApi;
Config = config; Config = config;
Options = options.Value; Options = options.Value;
_cache = c; _cache = c;
_imageService = imageService;
} }
private IFanartTvApi FanartTvApi { get; } private IFanartTvApi FanartTvApi { get; }
private IApplicationConfigRepository Config { get; } private IApplicationConfigRepository Config { get; }
private LandingPageBackground Options { get; } private LandingPageBackground Options { get; }
private readonly ICacheService _cache; private readonly ICacheService _cache;
private readonly IImageService _imageService;
[HttpGet("tv/{tvdbid}")] [HttpGet("tv/{tvdbid}")]
public async Task<string> GetTvBanner(int tvdbid) public async Task<string> GetTvBanner(int tvdbid)
@ -179,26 +182,8 @@ namespace Ombi.Controllers.V1
{ {
return string.Empty; 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)); return await _imageService.GetTvBackground(tvdbid.ToString());
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;
} }
[HttpGet("background")] [HttpGet("background")]

View file

@ -240,6 +240,19 @@ namespace Ombi.Controllers.V2
return await _tvSearchEngine.Popular(currentPosition, amountToLoad); 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> /// <summary>
/// Returns most Anticipated tv shows. /// Returns most Anticipated tv shows.
/// </summary> /// </summary>