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