2019-01-13 13:03:10 -07:00
|
|
|
using System;
|
2013-11-04 12:04:23 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 12:26:31 -07: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;
|
2019-02-07 11:54:34 -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;
|
2013-11-04 12:04:23 -07:00
|
|
|
|
2020-03-09 07:53:07 -07:00
|
|
|
namespace MediaBrowser.Providers.Plugins.TheTvdb
|
2013-11-04 12:04:23 -07:00
|
|
|
{
|
2016-05-24 17:42:12 -07:00
|
|
|
public class TvdbSeasonImageProvider : IRemoteImageProvider, IHasOrder
|
2013-11-04 12:04:23 -07:00
|
|
|
{
|
2014-01-28 11:37:01 -07:00
|
|
|
private readonly IHttpClient _httpClient;
|
2019-02-07 11:54:34 -07:00
|
|
|
private readonly ILogger _logger;
|
2020-03-09 07:53:07 -07:00
|
|
|
private readonly TvdbClientManager _tvdbClientManager;
|
2013-11-04 12:04:23 -07:00
|
|
|
|
2020-03-09 07:53:07 -07:00
|
|
|
public TvdbSeasonImageProvider(IHttpClient httpClient, ILogger<TvdbSeasonImageProvider> logger, TvdbClientManager tvdbClientManager)
|
2013-11-04 12:04:23 -07:00
|
|
|
{
|
2014-01-28 11:37:01 -07:00
|
|
|
_httpClient = httpClient;
|
2019-02-07 11:54:34 -07:00
|
|
|
_logger = logger;
|
2020-03-09 07:53:07 -07:00
|
|
|
_tvdbClientManager = tvdbClientManager;
|
2013-11-04 12:04:23 -07:00
|
|
|
}
|
|
|
|
|
2019-01-13 13:31:14 -07:00
|
|
|
public string Name => ProviderName;
|
2013-11-04 12:04:23 -07:00
|
|
|
|
2019-01-13 13:31:14 -07:00
|
|
|
public static string ProviderName => "TheTVDB";
|
2013-11-04 12:04:23 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
public bool Supports(BaseItem item)
|
2013-11-04 12:04:23 -07:00
|
|
|
{
|
|
|
|
return item is Season;
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
return new List<ImageType>
|
|
|
|
{
|
2019-01-07 16:27:46 -07:00
|
|
|
ImageType.Primary,
|
2014-01-28 11:37:01 -07:00
|
|
|
ImageType.Banner,
|
|
|
|
ImageType.Backdrop
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
|
2013-11-04 12:04:23 -07:00
|
|
|
{
|
|
|
|
var season = (Season)item;
|
2014-02-02 22:35:43 -07:00
|
|
|
var series = season.Series;
|
2013-11-04 12:04:23 -07:00
|
|
|
|
2019-01-30 13:23:23 -07:00
|
|
|
if (series == null || !season.IndexNumber.HasValue || !TvdbSeriesProvider.IsValidSeries(series.ProviderIds))
|
2015-09-19 14:25:19 -07:00
|
|
|
{
|
2019-01-30 13:23:23 -07:00
|
|
|
return new RemoteImageInfo[] { };
|
|
|
|
}
|
2013-11-04 12:04:23 -07:00
|
|
|
|
2019-02-07 11:54:34 -07:00
|
|
|
var tvdbId = Convert.ToInt32(series.GetProviderId(MetadataProviders.Tvdb));
|
2019-01-30 13:23:23 -07:00
|
|
|
var seasonNumber = season.IndexNumber.Value;
|
|
|
|
var language = item.GetPreferredMetadataLanguage();
|
|
|
|
var remoteImages = new List<RemoteImageInfo>();
|
2019-02-07 11:54:34 -07:00
|
|
|
|
2019-02-20 03:03:04 -07:00
|
|
|
var keyTypes = new[] { KeyType.Season, KeyType.Seasonwide, KeyType.Fanart };
|
2019-02-07 11:54:34 -07:00
|
|
|
foreach (var keyType in keyTypes)
|
2019-01-30 13:23:23 -07:00
|
|
|
{
|
|
|
|
var imageQuery = new ImagesQuery
|
2014-12-22 20:58:14 -07:00
|
|
|
{
|
2019-01-30 13:23:23 -07:00
|
|
|
KeyType = keyType,
|
|
|
|
SubKey = seasonNumber.ToString()
|
|
|
|
};
|
2019-01-30 15:28:43 -07:00
|
|
|
try
|
|
|
|
{
|
2020-03-09 07:53:07 -07:00
|
|
|
var imageResults = await _tvdbClientManager
|
2019-02-20 11:35:47 -07:00
|
|
|
.GetImagesAsync(tvdbId, imageQuery, language, cancellationToken).ConfigureAwait(false);
|
2019-01-30 15:28:43 -07:00
|
|
|
remoteImages.AddRange(GetImages(imageResults.Data, language));
|
|
|
|
}
|
2019-02-08 11:48:18 -07:00
|
|
|
catch (TvDbServerException)
|
2019-01-30 15:28:43 -07:00
|
|
|
{
|
2019-02-08 11:48:18 -07:00
|
|
|
_logger.LogDebug("No images of type {KeyType} found for series {TvdbId}", keyType, tvdbId);
|
2019-01-30 15:28:43 -07:00
|
|
|
}
|
2013-11-04 12:04:23 -07:00
|
|
|
}
|
|
|
|
|
2019-01-30 13:23:23 -07:00
|
|
|
return remoteImages;
|
2013-11-04 12:04:23 -07:00
|
|
|
}
|
|
|
|
|
2019-02-15 12:11:27 -07:00
|
|
|
private IEnumerable<RemoteImageInfo> GetImages(Image[] images, string preferredLanguage)
|
2013-11-04 12:04:23 -07:00
|
|
|
{
|
|
|
|
var list = new List<RemoteImageInfo>();
|
2020-03-09 07:53:07 -07:00
|
|
|
var languages = _tvdbClientManager.GetLanguagesAsync(CancellationToken.None).Result.Data;
|
2020-06-04 18:30:02 -07:00
|
|
|
// any languages with null ids are ignored
|
|
|
|
languages = Array.FindAll(languages, language => language.Id != null);
|
2019-01-30 13:23:23 -07:00
|
|
|
foreach (Image image in images)
|
2013-11-04 12:04:23 -07:00
|
|
|
{
|
2019-01-30 13:23:23 -07:00
|
|
|
var imageInfo = new RemoteImageInfo
|
2013-11-04 12:04:23 -07:00
|
|
|
{
|
2019-01-30 13:23:23 -07:00
|
|
|
RatingType = RatingType.Score,
|
|
|
|
CommunityRating = (double?)image.RatingsInfo.Average,
|
|
|
|
VoteCount = image.RatingsInfo.Count,
|
2019-02-13 03:21:56 -07:00
|
|
|
Url = TvdbUtils.BannerUrl + image.FileName,
|
2019-01-30 13:23:23 -07:00
|
|
|
ProviderName = ProviderName,
|
2019-02-11 10:47:40 -07:00
|
|
|
Language = languages.FirstOrDefault(lang => lang.Id == image.LanguageId)?.Abbreviation,
|
2019-02-13 03:21:56 -07:00
|
|
|
ThumbnailUrl = TvdbUtils.BannerUrl + image.Thumbnail
|
2019-01-30 13:23:23 -07:00
|
|
|
};
|
2016-10-27 00:58:33 -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-12-26 17:23:58 -07:00
|
|
|
var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase);
|
2013-11-04 12:04:23 -07:00
|
|
|
|
|
|
|
return list.OrderByDescending(i =>
|
|
|
|
{
|
2013-12-26 17:23:58 -07:00
|
|
|
if (string.Equals(preferredLanguage, i.Language, StringComparison.OrdinalIgnoreCase))
|
2013-11-04 12:04:23 -07:00
|
|
|
{
|
|
|
|
return 3;
|
|
|
|
}
|
2019-01-30 13:23:23 -07:00
|
|
|
|
2013-11-04 12:04:23 -07:00
|
|
|
if (!isLanguageEn)
|
|
|
|
{
|
|
|
|
if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
2019-01-30 13:23:23 -07:00
|
|
|
|
2013-11-04 12:04:23 -07:00
|
|
|
if (string.IsNullOrEmpty(i.Language))
|
|
|
|
{
|
|
|
|
return isLanguageEn ? 3 : 2;
|
|
|
|
}
|
2019-01-30 13:23:23 -07:00
|
|
|
|
2013-11-04 12:04:23 -07:00
|
|
|
return 0;
|
|
|
|
})
|
|
|
|
.ThenByDescending(i => i.CommunityRating ?? 0)
|
2017-08-09 12:56:38 -07:00
|
|
|
.ThenByDescending(i => i.VoteCount ?? 0);
|
2013-11-04 12:04:23 -07:00
|
|
|
}
|
|
|
|
|
2019-01-13 13:31:14 -07:00
|
|
|
public int Order => 0;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
{
|
|
|
|
CancellationToken = cancellationToken,
|
2017-04-29 19:37:51 -07:00
|
|
|
Url = url
|
2014-01-28 11:37:01 -07:00
|
|
|
});
|
2013-11-04 12:04:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|