Episode provider is somewhat broken

pull/776/head
Claus Vium 6 years ago
parent 19b6808602
commit 2a26760911

@ -0,0 +1,64 @@
using System;
using MediaBrowser.Controller.Library;
using TvDbSharper;
namespace MediaBrowser.Providers.TV
{
public sealed class TvDbClientManager
{
private static volatile TvDbClientManager instance;
private static readonly object syncRoot = new object();
private static TvDbClient tvDbClient;
private static DateTime tokenCreatedAt;
private TvDbClientManager()
{
tvDbClient = new TvDbClient();
tvDbClient.Authentication.AuthenticateAsync(TVUtils.TvdbApiKey);
tokenCreatedAt = DateTime.Now;
}
public static TvDbClientManager Instance
{
get
{
if (instance != null)
{
return instance;
}
lock (syncRoot)
{
if (instance == null)
instance = new TvDbClientManager();
}
return instance;
}
}
public TvDbClient TvDbClient
{
get
{
// Refresh if necessary
if (tokenCreatedAt > DateTime.Now.Subtract(TimeSpan.FromHours(20)))
{
try
{
tvDbClient.Authentication.RefreshTokenAsync();
}
catch
{
tvDbClient.Authentication.AuthenticateAsync(TVUtils.TvdbApiKey);
}
tokenCreatedAt = DateTime.Now;
}
// Default to English
tvDbClient.AcceptedLanguage = "en";
return tvDbClient;
}
}
}
}

@ -20,13 +20,12 @@ namespace MediaBrowser.Providers.TV.TheTVDB
{
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly IHttpClient _httpClient;
private readonly TvDbClient _tvDbClient;
private readonly TvDbClientManager _tvDbClientManager;
public TvdbEpisodeImageProvider(IHttpClient httpClient)
{
_httpClient = httpClient;
_tvDbClient = new TvDbClient();
_tvDbClient.Authentication.AuthenticateAsync(TVUtils.TvdbApiKey);
_tvDbClientManager = TvDbClientManager.Instance;
}
public string Name => "TheTVDB";
@ -53,7 +52,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
{
var tvdbId = episode.GetProviderId(MetadataProviders.Tvdb);
// Process images
var episodeResult = await _tvDbClient.Episodes.GetAsync(Convert.ToInt32(tvdbId), cancellationToken);
var episodeResult = await _tvDbClientManager.TvDbClient.Episodes.GetAsync(Convert.ToInt32(tvdbId), cancellationToken);
var image = GetImageInfo(episodeResult.Data);
return new List<RemoteImageInfo>

File diff suppressed because it is too large Load Diff

@ -22,13 +22,12 @@ namespace MediaBrowser.Providers.TV.TheTVDB
private static readonly CultureInfo UsCulture = new CultureInfo("en-US");
private readonly IHttpClient _httpClient;
private readonly TvDbClient _tvDbClient;
private readonly TvDbClientManager _tvDbClientManager;
public TvdbSeasonImageProvider(IHttpClient httpClient)
{
_httpClient = httpClient;
_tvDbClient = new TvDbClient();
_tvDbClient.Authentication.AuthenticateAsync(TVUtils.TvdbApiKey);
_tvDbClientManager = TvDbClientManager.Instance;
}
public string Name => ProviderName;
@ -62,7 +61,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
var seasonNumber = season.IndexNumber.Value;
var language = item.GetPreferredMetadataLanguage();
_tvDbClient.AcceptedLanguage = language;
_tvDbClientManager.TvDbClient.AcceptedLanguage = language;
var remoteImages = new List<RemoteImageInfo>();
var keyTypes = new[] {KeyType.Season, KeyType.Seasonwide, KeyType.Fanart};
// TODO error handling
@ -73,10 +72,18 @@ namespace MediaBrowser.Providers.TV.TheTVDB
KeyType = keyType,
SubKey = seasonNumber.ToString()
};
var imageResults =
await _tvDbClient.Series.GetImagesAsync(Convert.ToInt32(series.GetProviderId(MetadataProviders.Tvdb)), imageQuery, cancellationToken);
remoteImages.AddRange(GetImages(imageResults.Data, language));
try
{
var imageResults =
await _tvDbClientManager.TvDbClient.Series.GetImagesAsync(
Convert.ToInt32(series.GetProviderId(MetadataProviders.Tvdb)), imageQuery,
cancellationToken);
remoteImages.AddRange(GetImages(imageResults.Data, language));
}
catch (TvDbServerException e)
{
// TODO log
}
}
return remoteImages;
@ -88,7 +95,6 @@ namespace MediaBrowser.Providers.TV.TheTVDB
foreach (Image image in images)
{
var resolution = image.Resolution.Split('x');
var imageInfo = new RemoteImageInfo
{
RatingType = RatingType.Score,
@ -97,11 +103,16 @@ namespace MediaBrowser.Providers.TV.TheTVDB
Url = TVUtils.BannerUrl + image.FileName,
ProviderName = ProviderName,
// TODO Language = image.LanguageId,
Width = Convert.ToInt32(resolution[0]),
Height = Convert.ToInt32(resolution[1]),
ThumbnailUrl = TVUtils.BannerUrl + image.Thumbnail
};
var resolution = image.Resolution.Split('x');
if (resolution.Length == 2)
{
imageInfo.Width = Convert.ToInt32(resolution[0]);
imageInfo.Height = Convert.ToInt32(resolution[1]);
}
if (string.Equals(image.KeyType, "season", StringComparison.OrdinalIgnoreCase))
{
imageInfo.Type = ImageType.Primary;

@ -21,12 +21,12 @@ namespace MediaBrowser.Providers.TV.TheTVDB
{
private readonly IHttpClient _httpClient;
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private readonly TvDbClient _tvDbClient = new TvDbClient();
private readonly TvDbClientManager _tvDbClientManager;
public TvdbSeriesImageProvider(IHttpClient httpClient)
{
_httpClient = httpClient;
_tvDbClient.Authentication.AuthenticateAsync(TVUtils.TvdbApiKey);
_tvDbClientManager = TvDbClientManager.Instance;
}
public string Name => ProviderName;
@ -56,7 +56,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
}
var language = item.GetPreferredMetadataLanguage();
_tvDbClient.AcceptedLanguage = language;
_tvDbClientManager.TvDbClient.AcceptedLanguage = language;
var remoteImages = new List<RemoteImageInfo>();
var keyTypes = new[] {KeyType.Poster, KeyType.Series, KeyType.Fanart};
// TODO error handling
@ -67,7 +67,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
KeyType = keyType
};
var imageResults =
await _tvDbClient.Series.GetImagesAsync(Convert.ToInt32(item.GetProviderId(MetadataProviders.Tvdb)), imageQuery, cancellationToken);
await _tvDbClientManager.TvDbClient.Series.GetImagesAsync(Convert.ToInt32(item.GetProviderId(MetadataProviders.Tvdb)), imageQuery, cancellationToken);
remoteImages.AddRange(GetImages(imageResults.Data, language));
}
@ -80,7 +80,6 @@ namespace MediaBrowser.Providers.TV.TheTVDB
foreach (Image image in images)
{
var resolution = image.Resolution.Split('x');
var imageInfo = new RemoteImageInfo
{
RatingType = RatingType.Score,
@ -89,11 +88,16 @@ namespace MediaBrowser.Providers.TV.TheTVDB
Url = TVUtils.BannerUrl + image.FileName,
ProviderName = Name,
// TODO Language = image.LanguageId,
Width = Convert.ToInt32(resolution[0]),
Height = Convert.ToInt32(resolution[1]),
ThumbnailUrl = TVUtils.BannerUrl + image.Thumbnail
};
var resolution = image.Resolution.Split('x');
if (resolution.Length == 2)
{
imageInfo.Width = Convert.ToInt32(resolution[0]);
imageInfo.Height = Convert.ToInt32(resolution[1]);
}
if (string.Equals(image.KeyType, "poster", StringComparison.OrdinalIgnoreCase))
{

@ -1,25 +1,17 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Xml;
using Microsoft.Extensions.Logging;
using TvDbSharper;
using TvDbSharper.Dto;
@ -35,7 +27,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
private readonly ILogger _logger;
private readonly ILibraryManager _libraryManager;
private readonly ILocalizationManager _localizationManager;
private readonly TvDbClient _tvDbClient;
private readonly TvDbClientManager _tvDbClientManager;
public TvdbSeriesProvider(IHttpClient httpClient, ILogger logger, ILibraryManager libraryManager, ILocalizationManager localizationManager)
{
@ -44,8 +36,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
_libraryManager = libraryManager;
_localizationManager = localizationManager;
Current = this;
_tvDbClient = new TvDbClient();
_tvDbClient.Authentication.AuthenticateAsync(TVUtils.TvdbApiKey);
_tvDbClientManager = TvDbClientManager.Instance;
}
private string NormalizeLanguage(string language)
@ -112,7 +103,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
private async Task FetchSeriesData(MetadataResult<Series> result, string metadataLanguage, Dictionary<string, string> seriesProviderIds, CancellationToken cancellationToken)
{
_tvDbClient.AcceptedLanguage = NormalizeLanguage(metadataLanguage);
_tvDbClientManager.TvDbClient.AcceptedLanguage = NormalizeLanguage(metadataLanguage);
var series = result.Item;
if (seriesProviderIds.TryGetValue(MetadataProviders.Tvdb.ToString(), out var tvdbId) && !string.IsNullOrEmpty(tvdbId))
@ -133,7 +124,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
}
// TODO call this function elsewhere?
var seriesResult = await _tvDbClient.Series.GetAsync(Convert.ToInt32(tvdbId), cancellationToken);
var seriesResult = await _tvDbClientManager.TvDbClient.Series.GetAsync(Convert.ToInt32(tvdbId), cancellationToken);
// TODO error handling
MapSeriesToResult(result, seriesResult.Data, cancellationToken);
@ -142,22 +133,22 @@ namespace MediaBrowser.Providers.TV.TheTVDB
result.ResetPeople();
var actorsResult = await _tvDbClient.Series.GetActorsAsync(Convert.ToInt32(tvdbId), cancellationToken);
var actorsResult = await _tvDbClientManager.TvDbClient.Series.GetActorsAsync(Convert.ToInt32(tvdbId), cancellationToken);
MapActorsToResult(result, actorsResult.Data);
}
private async Task<string> GetSeriesByRemoteId(string id, string idType, string language, CancellationToken cancellationToken)
{
_tvDbClient.AcceptedLanguage = NormalizeLanguage(language);
_tvDbClientManager.TvDbClient.AcceptedLanguage = NormalizeLanguage(language);
TvDbResponse<SeriesSearchResult[]> result;
if (string.Equals(idType, MetadataProviders.Zap2It.ToString(), StringComparison.OrdinalIgnoreCase))
{
result = await _tvDbClient.Search.SearchSeriesByZap2ItIdAsync(id, cancellationToken);
result = await _tvDbClientManager.TvDbClient.Search.SearchSeriesByZap2ItIdAsync(id, cancellationToken);
}
else
{
result = await _tvDbClient.Search.SearchSeriesByImdbIdAsync(id, cancellationToken);
result = await _tvDbClientManager.TvDbClient.Search.SearchSeriesByImdbIdAsync(id, cancellationToken);
}
return result.Data.First().Id.ToString();
@ -252,10 +243,10 @@ namespace MediaBrowser.Providers.TV.TheTVDB
private async Task<List<RemoteSearchResult>> FindSeriesInternal(string name, string language, CancellationToken cancellationToken)
{
_tvDbClient.AcceptedLanguage = NormalizeLanguage(language);
_tvDbClientManager.TvDbClient.AcceptedLanguage = NormalizeLanguage(language);
var comparableName = GetComparableName(name);
var list = new List<Tuple<List<string>, RemoteSearchResult>>();
TvDbResponse<SeriesSearchResult[]> result = await _tvDbClient.Search.SearchSeriesByNameAsync(comparableName, cancellationToken);
TvDbResponse<SeriesSearchResult[]> result = await _tvDbClientManager.TvDbClient.Search.SearchSeriesByNameAsync(comparableName, cancellationToken);
foreach (var seriesSearchResult in result.Data)
{

Loading…
Cancel
Save