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

101 lines
3.4 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;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Extensions;
using MediaBrowser.Model.Providers;
2020-05-30 23:23:09 -07:00
namespace MediaBrowser.Providers.Plugins.Tmdb.TV
{
public class TmdbSeasonImageProvider : IRemoteImageProvider, IHasOrder
{
2020-08-17 12:10:02 -07:00
private readonly IHttpClientFactory _httpClientFactory;
private readonly TmdbClientManager _tmdbClientManager;
public TmdbSeasonImageProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager)
{
2020-08-17 12:10:02 -07:00
_httpClientFactory = httpClientFactory;
_tmdbClientManager = tmdbClientManager;
}
public int Order => 1;
public string Name => TmdbUtils.ProviderName;
2020-08-17 12:10:02 -07:00
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
2020-08-31 10:05:21 -07:00
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
}
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
var season = (Season)item;
var series = season?.Series;
var seriesTmdbId = Convert.ToInt32(series?.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
2020-09-24 11:18:45 -07:00
if (seriesTmdbId <= 0 || season?.IndexNumber == null)
{
2019-12-19 10:59:55 -07:00
return Enumerable.Empty<RemoteImageInfo>();
}
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 seasonResult = await _tmdbClientManager
.GetSeasonAsync(seriesTmdbId, season.IndexNumber.Value, null, null, cancellationToken)
.ConfigureAwait(false);
2020-10-06 05:54:17 -07:00
var posters = seasonResult?.Images?.Posters;
if (posters == null)
{
return Enumerable.Empty<RemoteImageInfo>();
}
2020-10-06 05:54:17 -07:00
var remoteImages = new RemoteImageInfo[posters.Count];
for (var i = 0; i < posters.Count; i++)
{
2020-10-06 05:54:17 -07:00
var image = posters[i];
remoteImages[i] = new RemoteImageInfo
{
Url = _tmdbClientManager.GetPosterUrl(image.FilePath),
CommunityRating = image.VoteAverage,
VoteCount = image.VoteCount,
Width = image.Width,
Height = image.Height,
Language = TmdbUtils.AdjustImageLanguage(image.Iso_639_1, language),
ProviderName = Name,
Type = ImageType.Primary,
RatingType = RatingType.Score
2020-10-06 05:54:17 -07:00
};
}
return remoteImages.OrderByLanguageDescending(language);
}
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
{
return new List<ImageType>
{
ImageType.Primary
};
}
public bool Supports(BaseItem item)
{
return item is Season;
}
}
}