Use DI for TvDbClientManager

This commit is contained in:
Claus Vium 2019-02-15 20:11:27 +01:00
parent 5a054e5150
commit ce51025e7c
10 changed files with 54 additions and 52 deletions

View File

@ -102,8 +102,10 @@ using MediaBrowser.Model.Xml;
using MediaBrowser.Providers.Chapters; using MediaBrowser.Providers.Chapters;
using MediaBrowser.Providers.Manager; using MediaBrowser.Providers.Manager;
using MediaBrowser.Providers.Subtitles; using MediaBrowser.Providers.Subtitles;
using MediaBrowser.Providers.TV.TheTVDB;
using MediaBrowser.WebDashboard.Api; using MediaBrowser.WebDashboard.Api;
using MediaBrowser.XbmcMetadata.Providers; using MediaBrowser.XbmcMetadata.Providers;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using ServiceStack; using ServiceStack;
@ -625,12 +627,13 @@ namespace Emby.Server.Implementations
/// </summary> /// </summary>
protected async Task RegisterResources(IServiceCollection serviceCollection) protected async Task RegisterResources(IServiceCollection serviceCollection)
{ {
serviceCollection.AddMemoryCache(options => options.SizeLimit = 1024);
serviceCollection.AddSingleton(ConfigurationManager); serviceCollection.AddSingleton(ConfigurationManager);
serviceCollection.AddSingleton<IApplicationHost>(this); serviceCollection.AddSingleton<IApplicationHost>(this);
serviceCollection.AddSingleton<IApplicationPaths>(ApplicationPaths); serviceCollection.AddSingleton<IApplicationPaths>(ApplicationPaths);
serviceCollection.AddSingleton(JsonSerializer); serviceCollection.AddSingleton(JsonSerializer);
serviceCollection.AddSingleton(LoggerFactory); serviceCollection.AddSingleton(LoggerFactory);
@ -640,6 +643,7 @@ namespace Emby.Server.Implementations
serviceCollection.AddSingleton(EnvironmentInfo); serviceCollection.AddSingleton(EnvironmentInfo);
serviceCollection.AddSingleton(FileSystemManager); serviceCollection.AddSingleton(FileSystemManager);
serviceCollection.AddSingleton<TvDbClientManager>();
HttpClient = CreateHttpClient(); HttpClient = CreateHttpClient();
serviceCollection.AddSingleton(HttpClient); serviceCollection.AddSingleton(HttpClient);

View File

@ -24,12 +24,12 @@ namespace MediaBrowser.Providers.People
private readonly ILibraryManager _libraryManager; private readonly ILibraryManager _libraryManager;
private readonly TvDbClientManager _tvDbClientManager; private readonly TvDbClientManager _tvDbClientManager;
public TvdbPersonImageProvider(ILibraryManager libraryManager, IHttpClient httpClient, ILogger logger) public TvdbPersonImageProvider(ILibraryManager libraryManager, IHttpClient httpClient, ILogger logger, TvDbClientManager tvDbClientManager)
{ {
_libraryManager = libraryManager; _libraryManager = libraryManager;
_httpClient = httpClient; _httpClient = httpClient;
_logger = logger; _logger = logger;
_tvDbClientManager = TvDbClientManager.Instance; _tvDbClientManager = tvDbClientManager;
} }
public string Name => ProviderName; public string Name => ProviderName;

View File

@ -27,17 +27,25 @@ namespace MediaBrowser.Providers.TV
private readonly ILibraryManager _libraryManager; private readonly ILibraryManager _libraryManager;
private readonly ILocalizationManager _localization; private readonly ILocalizationManager _localization;
private readonly IFileSystem _fileSystem; private readonly IFileSystem _fileSystem;
private readonly TvDbClientManager _tvDbClientManager;
private readonly CultureInfo _usCulture = new CultureInfo("en-US"); private readonly CultureInfo _usCulture = new CultureInfo("en-US");
private const double UnairedEpisodeThresholdDays = 2; private const double UnairedEpisodeThresholdDays = 2;
public MissingEpisodeProvider(ILogger logger, IServerConfigurationManager config, ILibraryManager libraryManager, ILocalizationManager localization, IFileSystem fileSystem) public MissingEpisodeProvider(
ILogger logger,
IServerConfigurationManager config,
ILibraryManager libraryManager,
ILocalizationManager localization,
IFileSystem fileSystem,
TvDbClientManager tvDbClientManager)
{ {
_logger = logger; _logger = logger;
_config = config; _config = config;
_libraryManager = libraryManager; _libraryManager = libraryManager;
_localization = localization; _localization = localization;
_fileSystem = fileSystem; _fileSystem = fileSystem;
_tvDbClientManager = tvDbClientManager;
} }
public async Task<bool> Run(Series series, bool addNewItems, CancellationToken cancellationToken) public async Task<bool> Run(Series series, bool addNewItems, CancellationToken cancellationToken)
@ -48,7 +56,7 @@ namespace MediaBrowser.Providers.TV
return false; return false;
} }
var episodes = await TvDbClientManager.Instance.GetAllEpisodesAsync(Convert.ToInt32(tvdbId), series.GetPreferredMetadataLanguage(), cancellationToken); var episodes = await _tvDbClientManager.GetAllEpisodesAsync(Convert.ToInt32(tvdbId), series.GetPreferredMetadataLanguage(), cancellationToken);
var episodeLookup = episodes var episodeLookup = episodes
.Select(i => .Select(i =>

View File

@ -10,6 +10,7 @@ using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.IO; using MediaBrowser.Model.IO;
using MediaBrowser.Model.Xml; using MediaBrowser.Model.Xml;
using MediaBrowser.Providers.Manager; using MediaBrowser.Providers.Manager;
using MediaBrowser.Providers.TV.TheTVDB;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace MediaBrowser.Providers.TV namespace MediaBrowser.Providers.TV
@ -18,11 +19,24 @@ namespace MediaBrowser.Providers.TV
{ {
private readonly ILocalizationManager _localization; private readonly ILocalizationManager _localization;
private readonly IXmlReaderSettingsFactory _xmlSettings; private readonly IXmlReaderSettingsFactory _xmlSettings;
private readonly TvDbClientManager _tvDbClientManager;
public SeriesMetadataService(IServerConfigurationManager serverConfigurationManager, ILogger logger, IProviderManager providerManager, IFileSystem fileSystem, IUserDataManager userDataManager, ILibraryManager libraryManager, ILocalizationManager localization, IXmlReaderSettingsFactory xmlSettings) : base(serverConfigurationManager, logger, providerManager, fileSystem, userDataManager, libraryManager) public SeriesMetadataService(
IServerConfigurationManager serverConfigurationManager,
ILogger logger,
IProviderManager providerManager,
IFileSystem fileSystem,
IUserDataManager userDataManager,
ILibraryManager libraryManager,
ILocalizationManager localization,
IXmlReaderSettingsFactory xmlSettings,
TvDbClientManager tvDbClientManager
)
: base(serverConfigurationManager, logger, providerManager, fileSystem, userDataManager, libraryManager)
{ {
_localization = localization; _localization = localization;
_xmlSettings = xmlSettings; _xmlSettings = xmlSettings;
_tvDbClientManager = tvDbClientManager;
} }
protected override async Task AfterMetadataRefresh(Series item, MetadataRefreshOptions refreshOptions, CancellationToken cancellationToken) protected override async Task AfterMetadataRefresh(Series item, MetadataRefreshOptions refreshOptions, CancellationToken cancellationToken)
@ -32,11 +46,13 @@ namespace MediaBrowser.Providers.TV
var seasonProvider = new DummySeasonProvider(ServerConfigurationManager, Logger, _localization, LibraryManager, FileSystem); var seasonProvider = new DummySeasonProvider(ServerConfigurationManager, Logger, _localization, LibraryManager, FileSystem);
await seasonProvider.Run(item, cancellationToken).ConfigureAwait(false); await seasonProvider.Run(item, cancellationToken).ConfigureAwait(false);
// TODO why does it not register this itself omg
var provider = new MissingEpisodeProvider(Logger, var provider = new MissingEpisodeProvider(Logger,
ServerConfigurationManager, ServerConfigurationManager,
LibraryManager, LibraryManager,
_localization, _localization,
FileSystem); FileSystem,
_tvDbClientManager);
try try
{ {

View File

@ -4,7 +4,6 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
@ -13,47 +12,22 @@ using TvDbSharper.Dto;
namespace MediaBrowser.Providers.TV.TheTVDB namespace MediaBrowser.Providers.TV.TheTVDB
{ {
// TODO add to DI once Bond's PR is merged public class TvDbClientManager
public sealed class TvDbClientManager
{ {
private static volatile TvDbClientManager instance;
// TODO add to DI once Bond's PR is merged
private readonly SemaphoreSlim _cacheWriteLock = new SemaphoreSlim(1, 1); private readonly SemaphoreSlim _cacheWriteLock = new SemaphoreSlim(1, 1);
private static MemoryCache _cache; private readonly IMemoryCache _cache;
private static readonly object syncRoot = new object(); private readonly TvDbClient tvDbClient;
private static TvDbClient tvDbClient; private DateTime tokenCreatedAt;
private static DateTime tokenCreatedAt;
private const string DefaultLanguage = "en"; private const string DefaultLanguage = "en";
private TvDbClientManager() public TvDbClientManager(IMemoryCache memoryCache)
{ {
_cache = memoryCache;
tvDbClient = new TvDbClient(); tvDbClient = new TvDbClient();
tvDbClient.Authentication.AuthenticateAsync(TvdbUtils.TvdbApiKey); tvDbClient.Authentication.AuthenticateAsync(TvdbUtils.TvdbApiKey);
tokenCreatedAt = DateTime.Now; tokenCreatedAt = DateTime.Now;
} }
public static TvDbClientManager Instance
{
get
{
if (instance != null)
{
return instance;
}
lock (syncRoot)
{
if (instance == null)
{
instance = new TvDbClientManager();
_cache = new MemoryCache(new MemoryCacheOptions());
}
}
return instance;
}
}
public TvDbClient TvDbClient public TvDbClient TvDbClient
{ {
get get

View File

@ -20,11 +20,11 @@ namespace MediaBrowser.Providers.TV.TheTVDB
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly TvDbClientManager _tvDbClientManager; private readonly TvDbClientManager _tvDbClientManager;
public TvdbEpisodeImageProvider(IHttpClient httpClient, ILogger logger) public TvdbEpisodeImageProvider(IHttpClient httpClient, ILogger logger, TvDbClientManager tvDbClientManager)
{ {
_httpClient = httpClient; _httpClient = httpClient;
_logger = logger; _logger = logger;
_tvDbClientManager = TvDbClientManager.Instance; _tvDbClientManager = tvDbClientManager;
} }
public string Name => "TheTVDB"; public string Name => "TheTVDB";

View File

@ -24,11 +24,11 @@ namespace MediaBrowser.Providers.TV.TheTVDB
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly TvDbClientManager _tvDbClientManager; private readonly TvDbClientManager _tvDbClientManager;
public TvdbEpisodeProvider(IHttpClient httpClient, ILogger logger) public TvdbEpisodeProvider(IHttpClient httpClient, ILogger logger, TvDbClientManager tvDbClientManager)
{ {
_httpClient = httpClient; _httpClient = httpClient;
_logger = logger; _logger = logger;
_tvDbClientManager = TvDbClientManager.Instance; _tvDbClientManager = tvDbClientManager;
} }
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(EpisodeInfo searchInfo, CancellationToken cancellationToken) public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(EpisodeInfo searchInfo, CancellationToken cancellationToken)

View File

@ -25,11 +25,11 @@ namespace MediaBrowser.Providers.TV.TheTVDB
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly TvDbClientManager _tvDbClientManager; private readonly TvDbClientManager _tvDbClientManager;
public TvdbSeasonImageProvider(IHttpClient httpClient, ILogger logger) public TvdbSeasonImageProvider(IHttpClient httpClient, ILogger logger, TvDbClientManager tvDbClientManager)
{ {
_httpClient = httpClient; _httpClient = httpClient;
_logger = logger; _logger = logger;
_tvDbClientManager = TvDbClientManager.Instance; _tvDbClientManager = tvDbClientManager;
} }
public string Name => ProviderName; public string Name => ProviderName;
@ -88,10 +88,10 @@ namespace MediaBrowser.Providers.TV.TheTVDB
return remoteImages; return remoteImages;
} }
private static IEnumerable<RemoteImageInfo> GetImages(Image[] images, string preferredLanguage) private IEnumerable<RemoteImageInfo> GetImages(Image[] images, string preferredLanguage)
{ {
var list = new List<RemoteImageInfo>(); var list = new List<RemoteImageInfo>();
var languages = TvDbClientManager.Instance.GetLanguagesAsync(CancellationToken.None).Result.Data; var languages = _tvDbClientManager.GetLanguagesAsync(CancellationToken.None).Result.Data;
foreach (Image image in images) foreach (Image image in images)
{ {
var imageInfo = new RemoteImageInfo var imageInfo = new RemoteImageInfo

View File

@ -22,11 +22,11 @@ namespace MediaBrowser.Providers.TV.TheTVDB
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly TvDbClientManager _tvDbClientManager; private readonly TvDbClientManager _tvDbClientManager;
public TvdbSeriesImageProvider(IHttpClient httpClient, ILogger logger) public TvdbSeriesImageProvider(IHttpClient httpClient, ILogger logger, TvDbClientManager tvDbClientManager)
{ {
_httpClient = httpClient; _httpClient = httpClient;
_logger = logger; _logger = logger;
_tvDbClientManager = TvDbClientManager.Instance; _tvDbClientManager = tvDbClientManager;
} }
public string Name => ProviderName; public string Name => ProviderName;
@ -84,7 +84,7 @@ namespace MediaBrowser.Providers.TV.TheTVDB
private IEnumerable<RemoteImageInfo> GetImages(Image[] images, string preferredLanguage) private IEnumerable<RemoteImageInfo> GetImages(Image[] images, string preferredLanguage)
{ {
var list = new List<RemoteImageInfo>(); var list = new List<RemoteImageInfo>();
var languages = TvDbClientManager.Instance.GetLanguagesAsync(CancellationToken.None).Result.Data; var languages = _tvDbClientManager.GetLanguagesAsync(CancellationToken.None).Result.Data;
foreach (Image image in images) foreach (Image image in images)
{ {

View File

@ -29,14 +29,14 @@ namespace MediaBrowser.Providers.TV.TheTVDB
private readonly ILocalizationManager _localizationManager; private readonly ILocalizationManager _localizationManager;
private readonly TvDbClientManager _tvDbClientManager; private readonly TvDbClientManager _tvDbClientManager;
public TvdbSeriesProvider(IHttpClient httpClient, ILogger logger, ILibraryManager libraryManager, ILocalizationManager localizationManager) public TvdbSeriesProvider(IHttpClient httpClient, ILogger logger, ILibraryManager libraryManager, ILocalizationManager localizationManager, TvDbClientManager tvDbClientManager)
{ {
_httpClient = httpClient; _httpClient = httpClient;
_logger = logger; _logger = logger;
_libraryManager = libraryManager; _libraryManager = libraryManager;
_localizationManager = localizationManager; _localizationManager = localizationManager;
Current = this; Current = this;
_tvDbClientManager = TvDbClientManager.Instance; _tvDbClientManager = tvDbClientManager;
} }
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo searchInfo, CancellationToken cancellationToken) public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo searchInfo, CancellationToken cancellationToken)