2019-01-30 22:28:43 +00:00
|
|
|
using System;
|
2019-02-07 19:28:19 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-30 22:28:43 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
2019-02-07 19:28:19 +00:00
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
using Microsoft.Extensions.Internal;
|
2019-01-30 22:28:43 +00:00
|
|
|
using TvDbSharper;
|
2019-02-07 19:28:19 +00:00
|
|
|
using TvDbSharper.Dto;
|
2019-01-30 22:28:43 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.TV
|
|
|
|
{
|
|
|
|
public sealed class TvDbClientManager
|
|
|
|
{
|
|
|
|
private static volatile TvDbClientManager instance;
|
2019-02-07 19:28:19 +00:00
|
|
|
// TODO add to DI once Bond's PR is merged
|
2019-02-07 19:57:42 +00:00
|
|
|
private readonly SemaphoreSlim _cacheWriteLock = new SemaphoreSlim(1, 1);
|
2019-02-07 19:28:19 +00:00
|
|
|
private static MemoryCache _cache;
|
2019-01-30 22:28:43 +00:00
|
|
|
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)
|
2019-02-07 19:28:19 +00:00
|
|
|
{
|
2019-01-30 22:28:43 +00:00
|
|
|
instance = new TvDbClientManager();
|
2019-02-07 19:28:19 +00:00
|
|
|
_cache = new MemoryCache(new MemoryCacheOptions());
|
|
|
|
}
|
2019-01-30 22:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 19:28:19 +00:00
|
|
|
|
|
|
|
public async Task<SeriesSearchResult[]> GetSeriesByName(string name, CancellationToken cancellationToken)
|
|
|
|
{
|
2019-02-07 19:57:42 +00:00
|
|
|
return await TryGetValue(name,
|
|
|
|
async () => (await TvDbClient.Search.SearchSeriesByNameAsync(name, cancellationToken)).Data);
|
2019-02-07 19:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<Series> GetSeriesById(int tvdbId, CancellationToken cancellationToken)
|
|
|
|
{
|
2019-02-07 19:57:42 +00:00
|
|
|
return await TryGetValue(tvdbId,
|
|
|
|
async () => (await TvDbClient.Series.GetAsync(tvdbId, cancellationToken)).Data);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<T> TryGetValue<T>(object key, Func<Task<T>> resultFactory)
|
|
|
|
{
|
|
|
|
if (_cache.TryGetValue(key, out T cachedValue))
|
2019-02-07 19:28:19 +00:00
|
|
|
{
|
2019-02-07 19:57:42 +00:00
|
|
|
return cachedValue;
|
|
|
|
}
|
|
|
|
using (_cacheWriteLock)
|
|
|
|
{
|
|
|
|
if (_cache.TryGetValue(key, out cachedValue))
|
|
|
|
{
|
|
|
|
return cachedValue;
|
|
|
|
}
|
|
|
|
var result = await resultFactory.Invoke();
|
|
|
|
_cache.Set(key, result, DateTimeOffset.UtcNow.AddHours(1));
|
|
|
|
return result;
|
2019-02-07 19:28:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-30 22:28:43 +00:00
|
|
|
}
|
|
|
|
}
|