jellyfin/MediaBrowser.Providers/TV/TheTVDB/TvDbClientManager.cs

182 lines
6.9 KiB
C#
Raw Normal View History

2019-01-30 22:28:43 +00:00
using System;
2019-02-07 22:56:57 +00:00
using System.Collections.Generic;
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;
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
{
2019-02-07 20:59:57 +00:00
// TODO add to DI once Bond's PR is merged
2019-01-30 22:28:43 +00:00
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
2019-02-07 20:59:57 +00:00
public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByNameAsync(string name, CancellationToken cancellationToken)
2019-02-07 19:28:19 +00:00
{
return TryGetValue("series" + name,() => TvDbClient.Search.SearchSeriesByNameAsync(name, cancellationToken));
2019-02-07 19:28:19 +00:00
}
2019-02-07 20:59:57 +00:00
public Task<TvDbResponse<Series>> GetSeriesByIdAsync(int tvdbId, CancellationToken cancellationToken)
2019-02-07 19:28:19 +00:00
{
return TryGetValue("series" + tvdbId,() => TvDbClient.Series.GetAsync(tvdbId, cancellationToken));
2019-02-07 20:59:57 +00:00
}
public Task<TvDbResponse<EpisodeRecord>> GetEpisodesAsync(int episodeTvdbId, CancellationToken cancellationToken)
2019-02-07 20:59:57 +00:00
{
return TryGetValue("episode" + episodeTvdbId,() => TvDbClient.Episodes.GetAsync(episodeTvdbId, cancellationToken));
2019-02-07 20:59:57 +00:00
}
2019-02-07 22:56:57 +00:00
public async Task<List<EpisodeRecord>> GetAllEpisodesAsync(int tvdbId, CancellationToken cancellationToken)
{
// Traverse all episode pages and join them together
var episodes = new List<EpisodeRecord>();
var episodePage = await GetEpisodesPageAsync(tvdbId, new EpisodeQuery(), cancellationToken);
episodes.AddRange(episodePage.Data);
int next = episodePage.Links.Next.GetValueOrDefault(0);
int last = episodePage.Links.Last.GetValueOrDefault(0);
for (var page = next; page <= last; ++page)
{
episodePage = await GetEpisodesPageAsync(tvdbId, page, new EpisodeQuery(), cancellationToken);
episodes.AddRange(episodePage.Data);
}
return episodes;
}
2019-02-07 20:59:57 +00:00
public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByImdbIdAsync(string imdbId, CancellationToken cancellationToken)
{
return TryGetValue("series" + imdbId,() => TvDbClient.Search.SearchSeriesByImdbIdAsync(imdbId, cancellationToken));
2019-02-07 20:59:57 +00:00
}
public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByZap2ItIdAsync(string zap2ItId, CancellationToken cancellationToken)
{
return TryGetValue("series" + zap2ItId,() => TvDbClient.Search.SearchSeriesByZap2ItIdAsync(zap2ItId, cancellationToken));
2019-02-07 20:59:57 +00:00
}
public Task<TvDbResponse<Actor[]>> GetActorsAsync(int tvdbId, CancellationToken cancellationToken)
{
return TryGetValue("actors" + tvdbId,() => TvDbClient.Series.GetActorsAsync(tvdbId, cancellationToken));
2019-02-07 20:59:57 +00:00
}
public Task<TvDbResponse<Image[]>> GetImagesAsync(int tvdbId, ImagesQuery imageQuery, CancellationToken cancellationToken)
{
return TryGetValue("images" + tvdbId,() => TvDbClient.Series.GetImagesAsync(tvdbId, imageQuery, cancellationToken));
}
public Task<TvDbResponse<Language[]>> GetLanguagesAsync(CancellationToken cancellationToken)
{
return TryGetValue("languages",() => TvDbClient.Languages.GetAllAsync(cancellationToken));
2019-02-07 19:57:42 +00:00
}
public Task<TvDbResponse<EpisodesSummary>> GetSeriesEpisodeSummaryAsync(int tvdbId, CancellationToken cancellationToken)
{
return TryGetValue("seriesepisodesummary" + tvdbId,
() => TvDbClient.Series.GetEpisodesSummaryAsync(tvdbId, cancellationToken));
}
2019-02-07 22:56:57 +00:00
public Task<TvDbResponse<EpisodeRecord[]>> GetEpisodesPageAsync(int tvdbId, int page, EpisodeQuery episodeQuery, CancellationToken cancellationToken)
{
// Not quite as dynamic as it could be
var cacheKey = "episodespage" + tvdbId + "page" + page;
if (episodeQuery.AiredSeason.HasValue)
{
cacheKey += "airedseason" + episodeQuery.AiredSeason.Value;
}
return TryGetValue(cacheKey,
() => TvDbClient.Series.GetEpisodesAsync(tvdbId, page, episodeQuery, cancellationToken));
}
public Task<TvDbResponse<EpisodeRecord[]>> GetEpisodesPageAsync(int tvdbId, EpisodeQuery episodeQuery, CancellationToken cancellationToken)
{
2019-02-07 22:56:57 +00:00
return GetEpisodesPageAsync(tvdbId, 1, episodeQuery, cancellationToken);
}
2019-02-07 19:57:42 +00:00
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;
}
await _cacheWriteLock.WaitAsync().ConfigureAwait(false);
try
2019-02-07 19:57:42 +00:00
{
if (_cache.TryGetValue(key, out cachedValue))
{
return cachedValue;
}
2019-02-07 19:57:42 +00:00
var result = await resultFactory.Invoke();
_cache.Set(key, result, TimeSpan.FromHours(1));
2019-02-07 19:57:42 +00:00
return result;
2019-02-07 19:28:19 +00:00
}
finally
{
_cacheWriteLock.Release();
}
2019-02-07 19:28:19 +00:00
}
2019-01-30 22:28:43 +00:00
}
}