2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2013-11-04 19:04:23 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.TV;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Providers;
|
2019-02-07 18:54:34 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2019-01-30 20:23:23 +00:00
|
|
|
using TvDbSharper;
|
|
|
|
using TvDbSharper.Dto;
|
|
|
|
using RatingType = MediaBrowser.Model.Dto.RatingType;
|
2013-11-04 19:04:23 +00:00
|
|
|
|
2019-02-09 09:10:33 +00:00
|
|
|
namespace MediaBrowser.Providers.TV.TheTVDB
|
2013-11-04 19:04:23 +00:00
|
|
|
{
|
2016-05-25 00:42:12 +00:00
|
|
|
public class TvdbSeasonImageProvider : IRemoteImageProvider, IHasOrder
|
2013-11-04 19:04:23 +00:00
|
|
|
{
|
2014-03-27 02:14:06 +00:00
|
|
|
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
|
|
|
|
2014-01-28 18:37:01 +00:00
|
|
|
private readonly IHttpClient _httpClient;
|
2019-02-07 18:54:34 +00:00
|
|
|
private readonly ILogger _logger;
|
2019-01-30 22:28:43 +00:00
|
|
|
private readonly TvDbClientManager _tvDbClientManager;
|
2013-11-04 19:04:23 +00:00
|
|
|
|
2019-02-07 18:54:34 +00:00
|
|
|
public TvdbSeasonImageProvider(IHttpClient httpClient, ILogger logger)
|
2013-11-04 19:04:23 +00:00
|
|
|
{
|
2014-01-28 18:37:01 +00:00
|
|
|
_httpClient = httpClient;
|
2019-02-07 18:54:34 +00:00
|
|
|
_logger = logger;
|
2019-01-30 22:28:43 +00:00
|
|
|
_tvDbClientManager = TvDbClientManager.Instance;
|
2013-11-04 19:04:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public string Name => ProviderName;
|
2013-11-04 19:04:23 +00:00
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public static string ProviderName => "TheTVDB";
|
2013-11-04 19:04:23 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool Supports(BaseItem item)
|
2013-11-04 19:04:23 +00:00
|
|
|
{
|
|
|
|
return item is Season;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2014-01-28 18:37:01 +00:00
|
|
|
{
|
|
|
|
return new List<ImageType>
|
|
|
|
{
|
2019-01-07 23:27:46 +00:00
|
|
|
ImageType.Primary,
|
2014-01-28 18:37:01 +00:00
|
|
|
ImageType.Banner,
|
|
|
|
ImageType.Backdrop
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
|
2013-11-04 19:04:23 +00:00
|
|
|
{
|
|
|
|
var season = (Season)item;
|
2014-02-03 05:35:43 +00:00
|
|
|
var series = season.Series;
|
2013-11-04 19:04:23 +00:00
|
|
|
|
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
|
|
|
{
|
2019-01-30 20:23:23 +00:00
|
|
|
return new RemoteImageInfo[] { };
|
|
|
|
}
|
2013-11-04 19:04:23 +00:00
|
|
|
|
2019-02-07 18:54:34 +00:00
|
|
|
var tvdbId = Convert.ToInt32(series.GetProviderId(MetadataProviders.Tvdb));
|
2019-01-30 20:23:23 +00:00
|
|
|
var seasonNumber = season.IndexNumber.Value;
|
|
|
|
var language = item.GetPreferredMetadataLanguage();
|
2019-01-30 22:28:43 +00:00
|
|
|
_tvDbClientManager.TvDbClient.AcceptedLanguage = language;
|
2019-01-30 20:23:23 +00:00
|
|
|
var remoteImages = new List<RemoteImageInfo>();
|
2019-02-07 18:54:34 +00:00
|
|
|
|
2019-01-30 20:23:23 +00:00
|
|
|
var keyTypes = new[] {KeyType.Season, KeyType.Seasonwide, KeyType.Fanart};
|
2019-02-07 18:54:34 +00:00
|
|
|
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
|
|
|
|
{
|
2019-02-07 20:59:57 +00:00
|
|
|
var imageResults = await _tvDbClientManager.GetImagesAsync(tvdbId, imageQuery, cancellationToken);
|
2019-01-30 22:28:43 +00:00
|
|
|
remoteImages.AddRange(GetImages(imageResults.Data, language));
|
|
|
|
}
|
|
|
|
catch (TvDbServerException e)
|
|
|
|
{
|
2019-02-07 18:54:34 +00:00
|
|
|
_logger.LogInformation(e, "No images of type {KeyType} found for series {TvdbId}", keyType, tvdbId);
|
2019-01-30 22:28:43 +00:00
|
|
|
}
|
2013-11-04 19:04:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:23:23 +00:00
|
|
|
return remoteImages;
|
2013-11-04 19:04:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:23:23 +00:00
|
|
|
private static IEnumerable<RemoteImageInfo> GetImages(Image[] images, string preferredLanguage)
|
2013-11-04 19:04:23 +00:00
|
|
|
{
|
|
|
|
var list = new List<RemoteImageInfo>();
|
|
|
|
|
2019-01-30 20:23:23 +00:00
|
|
|
foreach (Image image in images)
|
2013-11-04 19:04:23 +00:00
|
|
|
{
|
2019-01-30 20:23:23 +00:00
|
|
|
var imageInfo = new RemoteImageInfo
|
2013-11-04 19:04:23 +00:00
|
|
|
{
|
2019-01-30 20:23:23 +00:00
|
|
|
RatingType = RatingType.Score,
|
|
|
|
CommunityRating = (double?)image.RatingsInfo.Average,
|
|
|
|
VoteCount = image.RatingsInfo.Count,
|
|
|
|
Url = TVUtils.BannerUrl + image.FileName,
|
|
|
|
ProviderName = ProviderName,
|
|
|
|
// TODO Language = image.LanguageId,
|
|
|
|
ThumbnailUrl = TVUtils.BannerUrl + image.Thumbnail
|
|
|
|
};
|
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]);
|
|
|
|
}
|
|
|
|
|
2019-01-30 20:23:23 +00:00
|
|
|
if (string.Equals(image.KeyType, "season", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
imageInfo.Type = ImageType.Primary;
|
|
|
|
}
|
|
|
|
else if (string.Equals(image.KeyType, "seasonwide", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
imageInfo.Type = ImageType.Banner;
|
|
|
|
}
|
|
|
|
else if (string.Equals(image.KeyType, "fanart", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
imageInfo.Type = ImageType.Backdrop;
|
2013-11-04 19:04:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-30 20:23:23 +00:00
|
|
|
list.Add(imageInfo);
|
|
|
|
}
|
2013-12-27 00:23:58 +00:00
|
|
|
var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase);
|
2013-11-04 19:04:23 +00:00
|
|
|
|
|
|
|
return list.OrderByDescending(i =>
|
|
|
|
{
|
2013-12-27 00:23:58 +00:00
|
|
|
if (string.Equals(preferredLanguage, i.Language, StringComparison.OrdinalIgnoreCase))
|
2013-11-04 19:04:23 +00:00
|
|
|
{
|
|
|
|
return 3;
|
|
|
|
}
|
2019-01-30 20:23:23 +00:00
|
|
|
|
2013-11-04 19:04:23 +00:00
|
|
|
if (!isLanguageEn)
|
|
|
|
{
|
|
|
|
if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 20:23:23 +00:00
|
|
|
|
2013-11-04 19:04:23 +00:00
|
|
|
if (string.IsNullOrEmpty(i.Language))
|
|
|
|
{
|
|
|
|
return isLanguageEn ? 3 : 2;
|
|
|
|
}
|
2019-01-30 20:23:23 +00:00
|
|
|
|
2013-11-04 19:04:23 +00:00
|
|
|
return 0;
|
|
|
|
})
|
|
|
|
.ThenByDescending(i => i.CommunityRating ?? 0)
|
2017-08-09 19:56:38 +00:00
|
|
|
.ThenByDescending(i => i.VoteCount ?? 0);
|
2013-11-04 19:04:23 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public int Order => 0;
|
2014-01-28 18:37:01 +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-01-28 18:37:01 +00:00
|
|
|
});
|
2013-11-04 19:04:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|