jellyfin/MediaBrowser.Providers/Plugins/Tmdb/BoxSets/TmdbBoxSetImageProvider.cs

112 lines
3.9 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Globalization;
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-01-30 14:47:13 -07:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
2020-05-30 23:23:09 -07:00
namespace MediaBrowser.Providers.Plugins.Tmdb.BoxSets
2014-01-30 14:47:13 -07:00
{
public class TmdbBoxSetImageProvider : IRemoteImageProvider, IHasOrder
2014-01-30 14:47:13 -07:00
{
2020-08-17 12:10:02 -07:00
private readonly IHttpClientFactory _httpClientFactory;
private readonly TmdbClientManager _tmdbClientManager;
2014-01-30 14:47:13 -07:00
public TmdbBoxSetImageProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager)
2014-01-30 14:47:13 -07:00
{
2020-08-17 12:10:02 -07:00
_httpClientFactory = httpClientFactory;
_tmdbClientManager = tmdbClientManager;
2014-01-30 14:47:13 -07:00
}
public string Name => TmdbUtils.ProviderName;
2014-01-30 14:47:13 -07:00
public int Order => 0;
2014-01-30 14:47:13 -07:00
2018-09-12 10:26:21 -07:00
public bool Supports(BaseItem item)
2014-01-30 14:47:13 -07:00
{
return item is BoxSet;
}
2018-09-12 10:26:21 -07:00
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
2014-01-30 14:47:13 -07:00
{
return new List<ImageType>
{
2019-01-07 16:27:46 -07:00
ImageType.Primary,
2014-01-30 14:47:13 -07:00
ImageType.Backdrop
};
}
2018-09-12 10:26:21 -07:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
2014-01-30 14:47:13 -07:00
{
var tmdbId = Convert.ToInt32(item.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
2014-01-30 14:47:13 -07:00
if (tmdbId <= 0)
2014-01-30 14:47:13 -07:00
{
return Enumerable.Empty<RemoteImageInfo>();
}
2014-01-30 14:47:13 -07:00
var language = item.GetPreferredMetadataLanguage();
2014-01-30 14:47:13 -07:00
// TODO use image languages if All Languages isn't toggled, but there's currently no way to get that value in here
var collection = await _tmdbClientManager.GetCollectionAsync(tmdbId, null, null, cancellationToken).ConfigureAwait(false);
2014-01-30 14:47:13 -07:00
if (collection?.Images == null)
{
return Enumerable.Empty<RemoteImageInfo>();
2014-01-30 14:47:13 -07:00
}
var remoteImages = new List<RemoteImageInfo>();
2014-01-30 14:47:13 -07:00
for (var i = 0; i < collection.Images.Posters.Count; i++)
2014-01-30 14:47:13 -07:00
{
var poster = collection.Images.Posters[i];
remoteImages.Add(new RemoteImageInfo
2014-01-30 14:47:13 -07:00
{
Url = _tmdbClientManager.GetPosterUrl(poster.FilePath),
CommunityRating = poster.VoteAverage,
VoteCount = poster.VoteCount,
Width = poster.Width,
Height = poster.Height,
Language = TmdbUtils.AdjustImageLanguage(poster.Iso_639_1, language),
ProviderName = Name,
Type = ImageType.Primary,
RatingType = RatingType.Score
});
}
2020-05-30 23:28:01 -07:00
for (var i = 0; i < collection.Images.Backdrops.Count; i++)
{
var backdrop = collection.Images.Backdrops[i];
remoteImages.Add(new RemoteImageInfo
2014-01-30 14:47:13 -07:00
{
Url = _tmdbClientManager.GetBackdropUrl(backdrop.FilePath),
CommunityRating = backdrop.VoteAverage,
VoteCount = backdrop.VoteCount,
Width = backdrop.Width,
Height = backdrop.Height,
ProviderName = Name,
Type = ImageType.Backdrop,
RatingType = RatingType.Score
});
}
2014-01-30 14:47:13 -07:00
return remoteImages;
2014-01-30 14:47:13 -07:00
}
2020-08-17 12:10:02 -07:00
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
2014-01-30 14:47:13 -07:00
{
2020-08-31 10:05:21 -07:00
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
2014-01-30 14:47:13 -07:00
}
}
}