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

86 lines
2.9 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
2020-09-28 14:56:55 -07:00
using System.Linq;
2020-08-17 12:10:02 -07:00
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
2020-08-31 10:05:21 -07:00
using MediaBrowser.Common.Net;
2014-02-08 23:08:10 -07:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
2020-05-30 23:23:09 -07:00
namespace MediaBrowser.Providers.Plugins.Tmdb.TV
2014-02-08 23:08:10 -07:00
{
public class TmdbSeriesImageProvider : IRemoteImageProvider, IHasOrder
2014-02-08 23:08:10 -07:00
{
2020-08-17 12:10:02 -07:00
private readonly IHttpClientFactory _httpClientFactory;
private readonly TmdbClientManager _tmdbClientManager;
2014-02-08 23:08:10 -07:00
public TmdbSeriesImageProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager)
2014-02-08 23:08:10 -07:00
{
2020-08-17 12:10:02 -07:00
_httpClientFactory = httpClientFactory;
_tmdbClientManager = tmdbClientManager;
2014-02-08 23:08:10 -07:00
}
public string Name => TmdbUtils.ProviderName;
2014-02-08 23:08:10 -07:00
2020-09-08 07:12:47 -07:00
// After tvdb and fanart
public int Order => 2;
2018-09-12 10:26:21 -07:00
public bool Supports(BaseItem item)
2014-02-08 23:08:10 -07:00
{
return item is Series;
}
2018-09-12 10:26:21 -07:00
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
2014-02-08 23:08:10 -07:00
{
return new List<ImageType>
{
2019-01-07 16:27:46 -07:00
ImageType.Primary,
2014-02-08 23:08:10 -07:00
ImageType.Backdrop
};
}
2018-09-12 10:26:21 -07:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
2014-02-08 23:08:10 -07:00
{
var tmdbId = item.GetProviderId(MetadataProvider.Tmdb);
2014-02-08 23:08:10 -07:00
if (string.IsNullOrEmpty(tmdbId))
2014-02-08 23:08:10 -07:00
{
return Enumerable.Empty<RemoteImageInfo>();
2014-02-08 23:08:10 -07: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 series = await _tmdbClientManager
.GetSeriesAsync(Convert.ToInt32(tmdbId, CultureInfo.InvariantCulture), null, null, cancellationToken)
.ConfigureAwait(false);
2014-02-08 23:08:10 -07:00
if (series?.Images == null)
2014-02-08 23:08:10 -07:00
{
return Enumerable.Empty<RemoteImageInfo>();
2014-02-08 23:08:10 -07:00
}
2020-10-06 05:54:17 -07:00
var posters = series.Images.Posters;
var backdrops = series.Images.Backdrops;
2021-10-21 15:35:14 -07:00
var remoteImages = new List<RemoteImageInfo>(posters.Count + backdrops.Count);
2014-02-08 23:08:10 -07:00
_tmdbClientManager.ConvertPostersToRemoteImageInfo(posters, language, remoteImages);
_tmdbClientManager.ConvertBackdropsToRemoteImageInfo(backdrops, language, remoteImages);
2014-02-08 23:08:10 -07:00
return remoteImages;
2014-02-08 23:08:10 -07:00
}
2020-05-30 23:28:01 -07:00
2020-08-17 12:10:02 -07:00
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
2014-02-08 23:08:10 -07:00
{
2020-08-31 10:05:21 -07:00
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
2014-02-08 23:08:10 -07:00
}
}
}