jellyfin/MediaBrowser.Providers/Plugins/Tmdb/TV/TmdbEpisodeImageProvider.cs

97 lines
3.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
2020-08-17 19:10:02 +00:00
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
2014-07-07 14:23:08 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
2020-05-31 06:23:09 +00:00
namespace MediaBrowser.Providers.Plugins.Tmdb.TV
2014-07-07 14:23:08 +00:00
{
/// <summary>
2022-04-27 11:08:54 +00:00
/// TV episode image provider powered by TheMovieDb.
/// </summary>
public class TmdbEpisodeImageProvider : IRemoteImageProvider, IHasOrder
2014-07-07 14:23:08 +00:00
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly TmdbClientManager _tmdbClientManager;
/// <summary>
/// Initializes a new instance of the <see cref="TmdbEpisodeImageProvider"/> class.
/// </summary>
/// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/>.</param>
/// <param name="tmdbClientManager">The <see cref="TmdbClientManager"/>.</param>
public TmdbEpisodeImageProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager)
2020-09-08 14:12:47 +00:00
{
_httpClientFactory = httpClientFactory;
_tmdbClientManager = tmdbClientManager;
2020-09-08 14:12:47 +00:00
}
/// <inheritdoc />
2020-09-08 14:12:47 +00:00
public int Order => 1;
2014-07-07 14:23:08 +00:00
/// <inheritdoc />
public string Name => TmdbUtils.ProviderName;
/// <inheritdoc />
public bool Supports(BaseItem item)
{
return item is Controller.Entities.TV.Episode;
}
/// <inheritdoc />
2018-09-12 17:26:21 +00:00
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
2014-07-07 14:23:08 +00:00
{
2023-02-28 23:44:57 +00:00
yield return ImageType.Primary;
2014-07-07 14:23:08 +00:00
}
/// <inheritdoc />
2018-09-12 17:26:21 +00:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
2014-07-07 14:23:08 +00:00
{
var episode = (Controller.Entities.TV.Episode)item;
var series = episode.Series;
var seriesTmdbId = Convert.ToInt32(series?.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
2014-07-07 14:23:08 +00:00
if (series is null || seriesTmdbId <= 0)
2014-07-07 14:23:08 +00:00
{
return Enumerable.Empty<RemoteImageInfo>();
2014-07-07 14:23:08 +00:00
}
var seasonNumber = episode.ParentIndexNumber;
var episodeNumber = episode.IndexNumber;
if (!seasonNumber.HasValue || !episodeNumber.HasValue)
{
return Enumerable.Empty<RemoteImageInfo>();
2014-07-07 14:23:08 +00:00
}
var language = item.GetPreferredMetadataLanguage();
// TODO use image languages if All Languages isn't toggled, but there's currently no way to get that value in here
var episodeResult = await _tmdbClientManager
.GetEpisodeAsync(seriesTmdbId, seasonNumber.Value, episodeNumber.Value, series.DisplayOrder, null, null, cancellationToken)
.ConfigureAwait(false);
2014-07-07 14:23:08 +00:00
2020-10-06 12:54:17 +00:00
var stills = episodeResult?.Images?.Stills;
2022-12-05 14:00:20 +00:00
if (stills is null)
2014-07-07 14:23:08 +00:00
{
return Enumerable.Empty<RemoteImageInfo>();
}
2020-05-31 06:28:01 +00:00
return _tmdbClientManager.ConvertStillsToRemoteImageInfo(stills, language);
2014-07-07 14:23:08 +00:00
}
/// <inheritdoc />
2020-08-17 19:10:02 +00:00
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
2014-07-07 14:23:08 +00:00
{
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
2014-07-07 14:23:08 +00:00
}
}
}