jellyfin/MediaBrowser.Providers/Plugins/TheTvdb/TvdbSeriesImageProvider.cs

156 lines
5.4 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
using System;
2013-09-15 17:34:48 +00:00
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities;
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;
using Series = MediaBrowser.Controller.Entities.TV.Series;
namespace MediaBrowser.Providers.Plugins.TheTvdb
{
2016-05-25 00:42:12 +00:00
public class TvdbSeriesImageProvider : IRemoteImageProvider, IHasOrder
{
2014-02-03 20:51:28 +00:00
private readonly IHttpClient _httpClient;
2020-06-06 00:15:56 +00:00
private readonly ILogger<TvdbSeriesImageProvider> _logger;
private readonly TvdbClientManager _tvdbClientManager;
public TvdbSeriesImageProvider(IHttpClient httpClient, ILogger<TvdbSeriesImageProvider> logger, TvdbClientManager tvdbClientManager)
{
2014-02-03 20:51:28 +00:00
_httpClient = httpClient;
_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)
{
2014-02-03 20:51:28 +00:00
return item is Series;
}
2018-09-12 17:26:21 +00:00
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
{
2014-02-03 20:51:28 +00:00
return new List<ImageType>
{
2019-01-07 23:27:46 +00:00
ImageType.Primary,
2014-02-03 20:51:28 +00:00
ImageType.Banner,
ImageType.Backdrop
};
}
2013-09-15 17:34:48 +00:00
2018-09-12 17:26:21 +00:00
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
2019-01-30 20:23:23 +00:00
if (!TvdbSeriesProvider.IsValidSeries(item.ProviderIds))
{
return Array.Empty<RemoteImageInfo>();
}
2013-09-15 17:34:48 +00:00
2019-01-30 20:23:23 +00:00
var language = item.GetPreferredMetadataLanguage();
var remoteImages = new List<RemoteImageInfo>();
2019-02-20 10:03:04 +00:00
var keyTypes = new[] { KeyType.Poster, KeyType.Series, KeyType.Fanart };
2020-06-06 19:17:49 +00:00
var tvdbId = Convert.ToInt32(item.GetProviderId(MetadataProvider.Tvdb));
2019-01-30 20:23:23 +00:00
foreach (KeyType keyType in keyTypes)
2014-02-03 20:51:28 +00:00
{
2019-01-30 20:23:23 +00:00
var imageQuery = new ImagesQuery
2014-02-03 20:51:28 +00:00
{
2019-01-30 20:23:23 +00:00
KeyType = keyType
};
try
{
var imageResults =
await _tvdbClientManager.GetImagesAsync(tvdbId, imageQuery, language, cancellationToken)
2019-02-20 18:35:47 +00:00
.ConfigureAwait(false);
2016-10-27 07:58:33 +00:00
remoteImages.AddRange(GetImages(imageResults.Data, language));
}
2019-02-08 18:48:18 +00:00
catch (TvDbServerException)
{
2019-02-08 18:48:18 +00:00
_logger.LogDebug("No images of type {KeyType} exist for series {TvDbId}", keyType,
tvdbId);
}
2014-02-03 20:51:28 +00:00
}
2019-01-30 20:23:23 +00:00
return remoteImages;
2014-02-03 20:51:28 +00:00
}
2019-01-30 20:23:23 +00:00
private IEnumerable<RemoteImageInfo> GetImages(Image[] images, string preferredLanguage)
2014-02-03 20:51:28 +00:00
{
2019-01-30 20:23:23 +00:00
var list = new List<RemoteImageInfo>();
var languages = _tvdbClientManager.GetLanguagesAsync(CancellationToken.None).Result.Data;
2013-09-15 17:34:48 +00:00
2019-01-30 20:23:23 +00:00
foreach (Image image in images)
2013-09-15 17:34:48 +00:00
{
2014-02-03 20:51:28 +00:00
var imageInfo = new RemoteImageInfo
2013-09-15 17:34:48 +00:00
{
2014-02-03 20:51:28 +00:00
RatingType = RatingType.Score,
2019-01-30 20:23:23 +00:00
CommunityRating = (double?)image.RatingsInfo.Average,
VoteCount = image.RatingsInfo.Count,
Url = TvdbUtils.BannerUrl + image.FileName,
2014-02-03 20:51:28 +00:00
ProviderName = Name,
2019-02-11 18:07:53 +00:00
Language = languages.FirstOrDefault(lang => lang.Id == image.LanguageId)?.Abbreviation,
ThumbnailUrl = TvdbUtils.BannerUrl + image.Thumbnail
2014-02-03 20:51:28 +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);
}
2014-02-03 20:51:28 +00:00
var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase);
2019-01-30 20:23:23 +00: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 20:51:28 +00:00
}
public int Order => 0;
2014-02-03 20:51:28 +00:00
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
return _httpClient.GetResponse(new HttpRequestOptions
{
CancellationToken = cancellationToken,
2017-04-30 02:37:51 +00:00
Url = url
2014-02-03 20:51:28 +00:00
});
}
2013-09-15 17:34:48 +00:00
}
}