2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2013-09-15 17:34:48 +00:00
|
|
|
using System.Collections.Generic;
|
2014-02-03 20:51:28 +00:00
|
|
|
using System.Globalization;
|
2013-10-26 04:12:20 +00:00
|
|
|
using System.Linq;
|
2013-05-24 15:29:10 +00:00
|
|
|
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.Library;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Providers;
|
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;
|
2013-05-24 15:29:10 +00:00
|
|
|
|
2019-02-09 09:10:33 +00:00
|
|
|
namespace MediaBrowser.Providers.TV.TheTVDB
|
2013-05-24 15:29:10 +00:00
|
|
|
{
|
2016-05-25 00:42:12 +00:00
|
|
|
public class TvdbSeriesImageProvider : IRemoteImageProvider, IHasOrder
|
2013-05-24 15:29:10 +00:00
|
|
|
{
|
2014-02-03 20:51:28 +00:00
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
2019-01-30 20:23:23 +00:00
|
|
|
private readonly TvDbClient _tvDbClient = new TvDbClient();
|
2013-05-24 15:29:10 +00:00
|
|
|
|
2019-01-30 20:23:23 +00:00
|
|
|
public TvdbSeriesImageProvider(IHttpClient httpClient)
|
2013-05-24 15:29:10 +00:00
|
|
|
{
|
2014-02-03 20:51:28 +00:00
|
|
|
_httpClient = httpClient;
|
2019-01-30 20:23:23 +00:00
|
|
|
_tvDbClient.Authentication.AuthenticateAsync(TVUtils.TvdbApiKey);
|
2013-05-24 15:29:10 +00:00
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public string Name => ProviderName;
|
2013-05-24 15:29:10 +00:00
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public static string ProviderName => "TheTVDB";
|
2013-05-24 15:29:10 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public bool Supports(BaseItem item)
|
2013-05-24 15:29:10 +00:00
|
|
|
{
|
2014-02-03 20:51:28 +00:00
|
|
|
return item is Series;
|
2013-05-24 15:29:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2013-06-25 01:22:21 +00:00
|
|
|
{
|
2014-02-03 20:51:28 +00:00
|
|
|
return new List<ImageType>
|
2013-06-25 01:22:21 +00:00
|
|
|
{
|
2019-01-07 23:27:46 +00:00
|
|
|
ImageType.Primary,
|
2014-02-03 20:51:28 +00:00
|
|
|
ImageType.Banner,
|
|
|
|
ImageType.Backdrop
|
|
|
|
};
|
2013-06-25 01:22:21 +00:00
|
|
|
}
|
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)
|
2013-05-24 15:29:10 +00:00
|
|
|
{
|
2019-01-30 20:23:23 +00:00
|
|
|
if (!TvdbSeriesProvider.IsValidSeries(item.ProviderIds))
|
2013-05-24 15:29:10 +00:00
|
|
|
{
|
2019-01-30 20:23:23 +00:00
|
|
|
return new RemoteImageInfo[] { };
|
2013-05-24 15:29:10 +00:00
|
|
|
}
|
2013-09-15 17:34:48 +00:00
|
|
|
|
2019-01-30 20:23:23 +00:00
|
|
|
var language = item.GetPreferredMetadataLanguage();
|
|
|
|
_tvDbClient.AcceptedLanguage = language;
|
|
|
|
var remoteImages = new List<RemoteImageInfo>();
|
|
|
|
var keyTypes = new[] {KeyType.Poster, KeyType.Series, KeyType.Fanart};
|
|
|
|
// TODO error handling
|
|
|
|
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
|
|
|
|
};
|
|
|
|
var imageResults =
|
|
|
|
await _tvDbClient.Series.GetImagesAsync(Convert.ToInt32(item.GetProviderId(MetadataProviders.Tvdb)), imageQuery, cancellationToken);
|
2016-10-27 07:58:33 +00:00
|
|
|
|
2019-01-30 20:23:23 +00:00
|
|
|
remoteImages.AddRange(GetImages(imageResults.Data, language));
|
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>();
|
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
|
|
|
{
|
2019-01-30 20:23:23 +00:00
|
|
|
var resolution = image.Resolution.Split('x');
|
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 = TVUtils.BannerUrl + image.FileName,
|
2014-02-03 20:51:28 +00:00
|
|
|
ProviderName = Name,
|
2019-01-30 20:23:23 +00:00
|
|
|
// TODO Language = image.LanguageId,
|
|
|
|
Width = Convert.ToInt32(resolution[0]),
|
|
|
|
Height = Convert.ToInt32(resolution[1]),
|
|
|
|
ThumbnailUrl = TVUtils.BannerUrl + image.Thumbnail
|
2014-02-03 20:51:28 +00:00
|
|
|
};
|
2013-10-22 19:03:21 +00:00
|
|
|
|
2013-09-15 17:34:48 +00:00
|
|
|
|
2019-01-30 20:23:23 +00:00
|
|
|
if (string.Equals(image.KeyType, "poster", StringComparison.OrdinalIgnoreCase))
|
2014-02-03 20:51:28 +00:00
|
|
|
{
|
|
|
|
imageInfo.Type = ImageType.Primary;
|
|
|
|
}
|
2019-01-30 20:23:23 +00:00
|
|
|
else if (string.Equals(image.KeyType, "series", StringComparison.OrdinalIgnoreCase))
|
2014-02-03 20:51:28 +00:00
|
|
|
{
|
|
|
|
imageInfo.Type = ImageType.Banner;
|
|
|
|
}
|
2019-01-30 20:23:23 +00:00
|
|
|
else if (string.Equals(image.KeyType, "fanart", StringComparison.OrdinalIgnoreCase))
|
2014-02-03 20:51:28 +00:00
|
|
|
{
|
|
|
|
imageInfo.Type = ImageType.Backdrop;
|
2013-05-24 15:29:10 +00:00
|
|
|
}
|
2019-01-30 20:23:23 +00:00
|
|
|
|
|
|
|
list.Add(imageInfo);
|
2013-05-24 15:29:10 +00:00
|
|
|
}
|
2019-01-30 20:23:23 +00:00
|
|
|
var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase);
|
2014-02-03 20:51:28 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +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
|
|
|
}
|
2013-05-24 15:29:10 +00:00
|
|
|
}
|