jellyfin-server/MediaBrowser.Providers/Plugins/TheTvdb/TvdbSeasonImageProvider.cs

156 lines
5.8 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
using System.Collections.Generic;
using System.Linq;
2020-08-17 19:10:02 +00:00
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
2020-08-31 17:05:21 +00:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Logging;
2019-01-30 20:23:23 +00:00
using TvDbSharper;
using TvDbSharper.Dto;
using RatingType = MediaBrowser.Model.Dto.RatingType;
namespace MediaBrowser.Providers.Plugins.TheTvdb
{
2016-05-25 00:42:12 +00:00
public class TvdbSeasonImageProvider : IRemoteImageProvider, IHasOrder
{
2020-08-17 19:10:02 +00:00
private readonly IHttpClientFactory _httpClientFactory;
2020-06-06 00:15:56 +00:00
private readonly ILogger<TvdbSeasonImageProvider> _logger;
private readonly TvdbClientManager _tvdbClientManager;
2020-08-17 19:10:02 +00:00
public TvdbSeasonImageProvider(IHttpClientFactory httpClientFactory, ILogger<TvdbSeasonImageProvider> logger, TvdbClientManager tvdbClientManager)
{
2020-08-17 19:10:02 +00:00
_httpClientFactory = httpClientFactory;
_logger = logger;
_tvdbClientManager = tvdbClientManager;
}
public string Name => ProviderName;
public static string ProviderName => "TheTVDB";
2018-09-12 17:26:21 +00:00
public bool Supports(BaseItem item)
{
return item is Season;
}
2018-09-12 17:26:21 +00:00
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
{
return new List<ImageType>
{
2019-01-07 23:27:46 +00:00
ImageType.Primary,
ImageType.Banner,
ImageType.Backdrop
};
}
2018-09-12 17:26:21 +00:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
var season = (Season)item;
2014-02-03 05:35:43 +00:00
var series = season.Series;
2019-01-30 20:23:23 +00:00
if (series == null || !season.IndexNumber.HasValue || !TvdbSeriesProvider.IsValidSeries(series.ProviderIds))
2015-09-19 21:25:19 +00:00
{
return Array.Empty<RemoteImageInfo>();
2019-01-30 20:23:23 +00:00
}
2020-06-06 19:17:49 +00:00
var tvdbId = Convert.ToInt32(series.GetProviderId(MetadataProvider.Tvdb));
2019-01-30 20:23:23 +00:00
var seasonNumber = season.IndexNumber.Value;
var language = item.GetPreferredMetadataLanguage();
var remoteImages = new List<RemoteImageInfo>();
2020-07-15 15:14:39 +00:00
var keyTypes = _tvdbClientManager.GetImageKeyTypesForSeasonAsync(tvdbId, language, cancellationToken).ConfigureAwait(false);
await foreach (var keyType in keyTypes)
2019-01-30 20:23:23 +00:00
{
var imageQuery = new ImagesQuery
2014-12-23 03:58:14 +00:00
{
2019-01-30 20:23:23 +00:00
KeyType = keyType,
SubKey = seasonNumber.ToString()
};
2019-01-30 22:28:43 +00:00
try
{
var imageResults = await _tvdbClientManager
2019-02-20 18:35:47 +00:00
.GetImagesAsync(tvdbId, imageQuery, language, cancellationToken).ConfigureAwait(false);
2019-01-30 22:28:43 +00:00
remoteImages.AddRange(GetImages(imageResults.Data, language));
}
2019-02-08 18:48:18 +00:00
catch (TvDbServerException)
2019-01-30 22:28:43 +00:00
{
2019-02-08 18:48:18 +00:00
_logger.LogDebug("No images of type {KeyType} found for series {TvdbId}", keyType, tvdbId);
2019-01-30 22:28:43 +00:00
}
}
2019-01-30 20:23:23 +00:00
return remoteImages;
}
2019-02-15 19:11:27 +00:00
private IEnumerable<RemoteImageInfo> GetImages(Image[] images, string preferredLanguage)
{
var list = new List<RemoteImageInfo>();
2020-06-05 01:30:02 +00:00
// any languages with null ids are ignored
2020-06-05 22:14:47 +00:00
var languages = _tvdbClientManager.GetLanguagesAsync(CancellationToken.None).Result.Data.Where(x => x.Id.HasValue);
2019-01-30 20:23:23 +00:00
foreach (Image image in images)
{
2019-01-30 20:23:23 +00:00
var imageInfo = new RemoteImageInfo
{
2019-01-30 20:23:23 +00:00
RatingType = RatingType.Score,
CommunityRating = (double?)image.RatingsInfo.Average,
VoteCount = image.RatingsInfo.Count,
Url = TvdbUtils.BannerUrl + image.FileName,
2019-01-30 20:23:23 +00:00
ProviderName = ProviderName,
Language = languages.FirstOrDefault(lang => lang.Id == image.LanguageId)?.Abbreviation,
ThumbnailUrl = TvdbUtils.BannerUrl + image.Thumbnail
2019-01-30 20:23:23 +00:00
};
2016-10-27 07:58:33 +00:00
2019-01-30 22:28:43 +00:00
var resolution = image.Resolution.Split('x');
if (resolution.Length == 2)
{
imageInfo.Width = Convert.ToInt32(resolution[0]);
imageInfo.Height = Convert.ToInt32(resolution[1]);
}
imageInfo.Type = TvdbUtils.GetImageTypeFromKeyType(image.KeyType);
2019-01-30 20:23:23 +00:00
list.Add(imageInfo);
}
var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase);
return list.OrderByDescending(i =>
{
if (string.Equals(preferredLanguage, i.Language, StringComparison.OrdinalIgnoreCase))
{
return 3;
}
2019-01-30 20:23:23 +00:00
if (!isLanguageEn)
{
if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
{
return 2;
}
}
2019-01-30 20:23:23 +00:00
if (string.IsNullOrEmpty(i.Language))
{
return isLanguageEn ? 3 : 2;
}
2019-01-30 20:23:23 +00:00
return 0;
})
.ThenByDescending(i => i.CommunityRating ?? 0)
.ThenByDescending(i => i.VoteCount ?? 0);
}
public int Order => 0;
2020-08-17 19:10:02 +00:00
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
2020-08-31 17:05:21 +00:00
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
}
}
}