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

132 lines
4.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2014-07-07 14:23:08 +00:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
2014-07-07 14:23:08 +00:00
using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Serialization;
2020-05-31 06:23:09 +00:00
using MediaBrowser.Providers.Plugins.Tmdb.Models.General;
using MediaBrowser.Providers.Plugins.Tmdb.Movies;
using Microsoft.Extensions.Logging;
2014-07-07 14:23:08 +00:00
2020-05-31 06:23:09 +00:00
namespace MediaBrowser.Providers.Plugins.Tmdb.TV
2014-07-07 14:23:08 +00:00
{
public class TmdbEpisodeImageProvider :
TmdbEpisodeProviderBase,
2019-01-07 23:27:46 +00:00
IRemoteImageProvider,
IHasOrder
2014-07-07 14:23:08 +00:00
{
public TmdbEpisodeImageProvider(IHttpClient httpClient, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IFileSystem fileSystem, ILocalizationManager localization, ILoggerFactory loggerFactory)
: base(httpClient, configurationManager, jsonSerializer, fileSystem, localization, loggerFactory)
{ }
2014-07-07 14:23:08 +00:00
2018-09-12 17:26:21 +00:00
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
2014-07-07 14:23:08 +00:00
{
return new List<ImageType>
{
ImageType.Primary
};
}
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 seriesId = series != null ? series.GetProviderId(MetadataProviders.Tmdb) : null;
var list = new List<RemoteImageInfo>();
if (string.IsNullOrEmpty(seriesId))
{
return list;
}
var seasonNumber = episode.ParentIndexNumber;
var episodeNumber = episode.IndexNumber;
if (!seasonNumber.HasValue || !episodeNumber.HasValue)
{
return list;
}
var language = item.GetPreferredMetadataLanguage();
2014-07-07 14:23:08 +00:00
var response = await GetEpisodeInfo(seriesId, seasonNumber.Value, episodeNumber.Value,
language, cancellationToken).ConfigureAwait(false);
2014-07-07 14:23:08 +00:00
var tmdbSettings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
2014-07-07 14:23:08 +00:00
2018-09-12 17:26:21 +00:00
var tmdbImageUrl = tmdbSettings.images.GetImageUrl("original");
2014-07-07 14:23:08 +00:00
2019-08-18 12:44:13 +00:00
list.AddRange(GetPosters(response.Images).Select(i => new RemoteImageInfo
2014-07-07 14:23:08 +00:00
{
2019-08-18 12:44:13 +00:00
Url = tmdbImageUrl + i.File_Path,
CommunityRating = i.Vote_Average,
VoteCount = i.Vote_Count,
Width = i.Width,
Height = i.Height,
Language = TmdbMovieProvider.AdjustImageLanguage(i.Iso_639_1, language),
2014-07-07 14:23:08 +00:00
ProviderName = Name,
Type = ImageType.Primary,
RatingType = RatingType.Score
}));
var isLanguageEn = string.Equals(language, "en", StringComparison.OrdinalIgnoreCase);
return list.OrderByDescending(i =>
{
if (string.Equals(language, i.Language, StringComparison.OrdinalIgnoreCase))
{
return 3;
}
2020-05-31 06:28:01 +00:00
2014-07-07 14:23:08 +00:00
if (!isLanguageEn)
{
if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
{
return 2;
}
}
2020-05-31 06:28:01 +00:00
2014-07-07 14:23:08 +00:00
if (string.IsNullOrEmpty(i.Language))
{
return isLanguageEn ? 3 : 2;
}
2020-05-31 06:28:01 +00:00
2014-07-07 14:23:08 +00:00
return 0;
})
.ThenByDescending(i => i.CommunityRating ?? 0)
.ThenByDescending(i => i.VoteCount ?? 0);
2014-07-07 14:23:08 +00:00
}
private IEnumerable<Still> GetPosters(StillImages images)
2014-07-07 14:23:08 +00:00
{
2019-08-18 12:44:13 +00:00
return images.Stills ?? new List<Still>();
2014-07-07 14:23:08 +00:00
}
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
return GetResponse(url, cancellationToken);
2014-07-07 14:23:08 +00:00
}
public string Name => TmdbUtils.ProviderName;
2014-07-07 14:23:08 +00:00
2018-09-12 17:26:21 +00:00
public bool Supports(BaseItem item)
2014-07-07 14:23:08 +00:00
{
return item is Controller.Entities.TV.Episode;
}
// After TheTvDb
public int Order => 1;
2014-07-07 14:23:08 +00:00
}
}