You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
5.6 KiB
156 lines
5.6 KiB
6 years ago
|
using System;
|
||
11 years ago
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Threading;
|
||
|
using System.Threading.Tasks;
|
||
6 years ago
|
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;
|
||
6 years ago
|
using Microsoft.Extensions.Logging;
|
||
6 years ago
|
using TvDbSharper;
|
||
|
using TvDbSharper.Dto;
|
||
|
using RatingType = MediaBrowser.Model.Dto.RatingType;
|
||
11 years ago
|
|
||
5 years ago
|
namespace MediaBrowser.Providers.Plugins.TheTvdb
|
||
11 years ago
|
{
|
||
9 years ago
|
public class TvdbSeasonImageProvider : IRemoteImageProvider, IHasOrder
|
||
11 years ago
|
{
|
||
11 years ago
|
private readonly IHttpClient _httpClient;
|
||
6 years ago
|
private readonly ILogger _logger;
|
||
5 years ago
|
private readonly TvdbClientManager _tvdbClientManager;
|
||
11 years ago
|
|
||
5 years ago
|
public TvdbSeasonImageProvider(IHttpClient httpClient, ILogger<TvdbSeasonImageProvider> logger, TvdbClientManager tvdbClientManager)
|
||
11 years ago
|
{
|
||
11 years ago
|
_httpClient = httpClient;
|
||
6 years ago
|
_logger = logger;
|
||
5 years ago
|
_tvdbClientManager = tvdbClientManager;
|
||
11 years ago
|
}
|
||
|
|
||
6 years ago
|
public string Name => ProviderName;
|
||
11 years ago
|
|
||
6 years ago
|
public static string ProviderName => "TheTVDB";
|
||
11 years ago
|
|
||
6 years ago
|
public bool Supports(BaseItem item)
|
||
11 years ago
|
{
|
||
|
return item is Season;
|
||
|
}
|
||
|
|
||
6 years ago
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
||
11 years ago
|
{
|
||
|
return new List<ImageType>
|
||
|
{
|
||
6 years ago
|
ImageType.Primary,
|
||
11 years ago
|
ImageType.Banner,
|
||
|
ImageType.Backdrop
|
||
|
};
|
||
|
}
|
||
|
|
||
6 years ago
|
public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
|
||
11 years ago
|
{
|
||
|
var season = (Season)item;
|
||
11 years ago
|
var series = season.Series;
|
||
11 years ago
|
|
||
6 years ago
|
if (series == null || !season.IndexNumber.HasValue || !TvdbSeriesProvider.IsValidSeries(series.ProviderIds))
|
||
9 years ago
|
{
|
||
6 years ago
|
return new RemoteImageInfo[] { };
|
||
|
}
|
||
11 years ago
|
|
||
6 years ago
|
var tvdbId = Convert.ToInt32(series.GetProviderId(MetadataProviders.Tvdb));
|
||
6 years ago
|
var seasonNumber = season.IndexNumber.Value;
|
||
|
var language = item.GetPreferredMetadataLanguage();
|
||
|
var remoteImages = new List<RemoteImageInfo>();
|
||
6 years ago
|
|
||
6 years ago
|
var keyTypes = new[] { KeyType.Season, KeyType.Seasonwide, KeyType.Fanart };
|
||
6 years ago
|
foreach (var keyType in keyTypes)
|
||
6 years ago
|
{
|
||
|
var imageQuery = new ImagesQuery
|
||
10 years ago
|
{
|
||
6 years ago
|
KeyType = keyType,
|
||
|
SubKey = seasonNumber.ToString()
|
||
|
};
|
||
6 years ago
|
try
|
||
|
{
|
||
5 years ago
|
var imageResults = await _tvdbClientManager
|
||
6 years ago
|
.GetImagesAsync(tvdbId, imageQuery, language, cancellationToken).ConfigureAwait(false);
|
||
6 years ago
|
remoteImages.AddRange(GetImages(imageResults.Data, language));
|
||
|
}
|
||
6 years ago
|
catch (TvDbServerException)
|
||
6 years ago
|
{
|
||
6 years ago
|
_logger.LogDebug("No images of type {KeyType} found for series {TvdbId}", keyType, tvdbId);
|
||
6 years ago
|
}
|
||
11 years ago
|
}
|
||
|
|
||
6 years ago
|
return remoteImages;
|
||
11 years ago
|
}
|
||
|
|
||
6 years ago
|
private IEnumerable<RemoteImageInfo> GetImages(Image[] images, string preferredLanguage)
|
||
11 years ago
|
{
|
||
|
var list = new List<RemoteImageInfo>();
|
||
5 years ago
|
var languages = _tvdbClientManager.GetLanguagesAsync(CancellationToken.None).Result.Data;
|
||
6 years ago
|
foreach (Image image in images)
|
||
11 years ago
|
{
|
||
6 years ago
|
var imageInfo = new RemoteImageInfo
|
||
11 years ago
|
{
|
||
6 years ago
|
RatingType = RatingType.Score,
|
||
|
CommunityRating = (double?)image.RatingsInfo.Average,
|
||
|
VoteCount = image.RatingsInfo.Count,
|
||
6 years ago
|
Url = TvdbUtils.BannerUrl + image.FileName,
|
||
6 years ago
|
ProviderName = ProviderName,
|
||
6 years ago
|
Language = languages.FirstOrDefault(lang => lang.Id == image.LanguageId)?.Abbreviation,
|
||
6 years ago
|
ThumbnailUrl = TvdbUtils.BannerUrl + image.Thumbnail
|
||
6 years ago
|
};
|
||
8 years ago
|
|
||
6 years ago
|
var resolution = image.Resolution.Split('x');
|
||
|
if (resolution.Length == 2)
|
||
|
{
|
||
|
imageInfo.Width = Convert.ToInt32(resolution[0]);
|
||
|
imageInfo.Height = Convert.ToInt32(resolution[1]);
|
||
|
}
|
||
|
|
||
6 years ago
|
imageInfo.Type = TvdbUtils.GetImageTypeFromKeyType(image.KeyType);
|
||
6 years ago
|
list.Add(imageInfo);
|
||
|
}
|
||
11 years ago
|
var isLanguageEn = string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase);
|
||
11 years ago
|
|
||
|
return list.OrderByDescending(i =>
|
||
|
{
|
||
11 years ago
|
if (string.Equals(preferredLanguage, i.Language, StringComparison.OrdinalIgnoreCase))
|
||
11 years ago
|
{
|
||
|
return 3;
|
||
|
}
|
||
6 years ago
|
|
||
11 years ago
|
if (!isLanguageEn)
|
||
|
{
|
||
|
if (string.Equals("en", i.Language, StringComparison.OrdinalIgnoreCase))
|
||
|
{
|
||
|
return 2;
|
||
|
}
|
||
|
}
|
||
6 years ago
|
|
||
11 years ago
|
if (string.IsNullOrEmpty(i.Language))
|
||
|
{
|
||
|
return isLanguageEn ? 3 : 2;
|
||
|
}
|
||
6 years ago
|
|
||
11 years ago
|
return 0;
|
||
|
})
|
||
|
.ThenByDescending(i => i.CommunityRating ?? 0)
|
||
7 years ago
|
.ThenByDescending(i => i.VoteCount ?? 0);
|
||
11 years ago
|
}
|
||
|
|
||
6 years ago
|
public int Order => 0;
|
||
11 years ago
|
|
||
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
||
|
{
|
||
|
return _httpClient.GetResponse(new HttpRequestOptions
|
||
|
{
|
||
|
CancellationToken = cancellationToken,
|
||
8 years ago
|
Url = url
|
||
11 years ago
|
});
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|