2020-06-19 11:24:13 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-30 15:28:43 -07:00
|
|
|
using System;
|
2019-02-07 15:56:57 -07:00
|
|
|
using System.Collections.Generic;
|
2019-02-08 11:48:18 -07:00
|
|
|
using System.Linq;
|
2019-02-12 11:50:19 -07:00
|
|
|
using System.Reflection;
|
2020-07-16 06:32:58 -07:00
|
|
|
using System.Runtime.CompilerServices;
|
2019-02-07 12:28:19 -07:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-02-08 11:48:18 -07:00
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2019-02-07 12:28:19 -07:00
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2019-01-30 15:28:43 -07:00
|
|
|
using TvDbSharper;
|
2019-02-07 12:28:19 -07:00
|
|
|
using TvDbSharper.Dto;
|
2019-01-30 15:28:43 -07:00
|
|
|
|
2020-03-09 07:53:07 -07:00
|
|
|
namespace MediaBrowser.Providers.Plugins.TheTvdb
|
2019-01-30 15:28:43 -07:00
|
|
|
{
|
2020-03-09 07:53:07 -07:00
|
|
|
public class TvdbClientManager
|
2019-01-30 15:28:43 -07:00
|
|
|
{
|
2019-09-25 08:19:48 -07:00
|
|
|
private const string DefaultLanguage = "en";
|
|
|
|
|
2019-02-07 12:57:42 -07:00
|
|
|
private readonly SemaphoreSlim _cacheWriteLock = new SemaphoreSlim(1, 1);
|
2019-02-15 12:11:27 -07:00
|
|
|
private readonly IMemoryCache _cache;
|
2019-02-20 03:03:04 -07:00
|
|
|
private readonly TvDbClient _tvDbClient;
|
|
|
|
private DateTime _tokenCreatedAt;
|
2019-01-30 15:28:43 -07:00
|
|
|
|
2020-03-09 07:53:07 -07:00
|
|
|
public TvdbClientManager(IMemoryCache memoryCache)
|
2019-01-30 15:28:43 -07:00
|
|
|
{
|
2019-02-15 12:11:27 -07:00
|
|
|
_cache = memoryCache;
|
2019-02-20 03:03:04 -07:00
|
|
|
_tvDbClient = new TvDbClient();
|
2019-01-30 15:28:43 -07:00
|
|
|
}
|
|
|
|
|
2019-06-21 10:08:04 -07:00
|
|
|
private TvDbClient TvDbClient
|
2019-01-30 15:28:43 -07:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2019-06-21 10:08:04 -07:00
|
|
|
if (string.IsNullOrEmpty(_tvDbClient.Authentication.Token))
|
|
|
|
{
|
|
|
|
_tvDbClient.Authentication.AuthenticateAsync(TvdbUtils.TvdbApiKey).GetAwaiter().GetResult();
|
|
|
|
_tokenCreatedAt = DateTime.Now;
|
|
|
|
}
|
|
|
|
|
2019-01-30 15:28:43 -07:00
|
|
|
// Refresh if necessary
|
2019-05-30 22:24:52 -07:00
|
|
|
if (_tokenCreatedAt < DateTime.Now.Subtract(TimeSpan.FromHours(20)))
|
2019-01-30 15:28:43 -07:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-06-21 10:08:04 -07:00
|
|
|
_tvDbClient.Authentication.RefreshTokenAsync().GetAwaiter().GetResult();
|
2019-01-30 15:28:43 -07:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2019-06-21 10:08:04 -07:00
|
|
|
_tvDbClient.Authentication.AuthenticateAsync(TvdbUtils.TvdbApiKey).GetAwaiter().GetResult();
|
2019-01-30 15:28:43 -07:00
|
|
|
}
|
|
|
|
|
2019-02-20 03:03:04 -07:00
|
|
|
_tokenCreatedAt = DateTime.Now;
|
2019-01-30 15:28:43 -07:00
|
|
|
}
|
2019-02-12 11:50:19 -07:00
|
|
|
|
2019-02-20 03:03:04 -07:00
|
|
|
return _tvDbClient;
|
2019-01-30 15:28:43 -07:00
|
|
|
}
|
|
|
|
}
|
2019-02-07 12:28:19 -07:00
|
|
|
|
2019-02-12 11:50:19 -07:00
|
|
|
public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByNameAsync(string name, string language,
|
|
|
|
CancellationToken cancellationToken)
|
2019-02-07 12:28:19 -07:00
|
|
|
{
|
2019-02-12 13:04:45 -07:00
|
|
|
var cacheKey = GenerateKey("series", name, language);
|
2020-03-24 08:12:06 -07:00
|
|
|
return TryGetValue(cacheKey, language, () => TvDbClient.Search.SearchSeriesByNameAsync(name, cancellationToken));
|
2019-02-07 12:28:19 -07:00
|
|
|
}
|
|
|
|
|
2019-02-12 11:50:19 -07:00
|
|
|
public Task<TvDbResponse<Series>> GetSeriesByIdAsync(int tvdbId, string language,
|
|
|
|
CancellationToken cancellationToken)
|
2019-02-07 12:28:19 -07:00
|
|
|
{
|
2019-02-12 13:04:45 -07:00
|
|
|
var cacheKey = GenerateKey("series", tvdbId, language);
|
2020-03-24 08:12:06 -07:00
|
|
|
return TryGetValue(cacheKey, language, () => TvDbClient.Series.GetAsync(tvdbId, cancellationToken));
|
2019-02-07 13:59:57 -07:00
|
|
|
}
|
|
|
|
|
2019-02-12 11:50:19 -07:00
|
|
|
public Task<TvDbResponse<EpisodeRecord>> GetEpisodesAsync(int episodeTvdbId, string language,
|
|
|
|
CancellationToken cancellationToken)
|
2019-02-07 13:59:57 -07:00
|
|
|
{
|
2019-02-12 13:04:45 -07:00
|
|
|
var cacheKey = GenerateKey("episode", episodeTvdbId, language);
|
2020-03-24 08:12:06 -07:00
|
|
|
return TryGetValue(cacheKey, language, () => TvDbClient.Episodes.GetAsync(episodeTvdbId, cancellationToken));
|
2019-02-07 13:59:57 -07:00
|
|
|
}
|
|
|
|
|
2019-02-12 11:50:19 -07:00
|
|
|
public async Task<List<EpisodeRecord>> GetAllEpisodesAsync(int tvdbId, string language,
|
|
|
|
CancellationToken cancellationToken)
|
2019-02-07 15:56:57 -07:00
|
|
|
{
|
|
|
|
// Traverse all episode pages and join them together
|
|
|
|
var episodes = new List<EpisodeRecord>();
|
2019-02-20 11:35:47 -07:00
|
|
|
var episodePage = await GetEpisodesPageAsync(tvdbId, new EpisodeQuery(), language, cancellationToken)
|
|
|
|
.ConfigureAwait(false);
|
2019-02-07 15:56:57 -07:00
|
|
|
episodes.AddRange(episodePage.Data);
|
2019-02-08 11:48:18 -07:00
|
|
|
if (!episodePage.Links.Next.HasValue || !episodePage.Links.Last.HasValue)
|
|
|
|
{
|
|
|
|
return episodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
int next = episodePage.Links.Next.Value;
|
|
|
|
int last = episodePage.Links.Last.Value;
|
2019-02-07 15:56:57 -07:00
|
|
|
|
|
|
|
for (var page = next; page <= last; ++page)
|
|
|
|
{
|
2019-02-20 11:35:47 -07:00
|
|
|
episodePage = await GetEpisodesPageAsync(tvdbId, page, new EpisodeQuery(), language, cancellationToken)
|
|
|
|
.ConfigureAwait(false);
|
2019-02-07 15:56:57 -07:00
|
|
|
episodes.AddRange(episodePage.Data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return episodes;
|
|
|
|
}
|
|
|
|
|
2019-09-25 08:19:48 -07:00
|
|
|
public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByImdbIdAsync(
|
|
|
|
string imdbId,
|
|
|
|
string language,
|
2019-02-12 11:50:19 -07:00
|
|
|
CancellationToken cancellationToken)
|
2019-02-07 13:59:57 -07:00
|
|
|
{
|
2019-02-12 13:04:45 -07:00
|
|
|
var cacheKey = GenerateKey("series", imdbId, language);
|
2020-03-24 08:12:06 -07:00
|
|
|
return TryGetValue(cacheKey, language, () => TvDbClient.Search.SearchSeriesByImdbIdAsync(imdbId, cancellationToken));
|
2019-02-07 13:59:57 -07:00
|
|
|
}
|
|
|
|
|
2019-09-25 08:19:48 -07:00
|
|
|
public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByZap2ItIdAsync(
|
|
|
|
string zap2ItId,
|
|
|
|
string language,
|
2019-02-12 11:50:19 -07:00
|
|
|
CancellationToken cancellationToken)
|
2019-02-07 13:59:57 -07:00
|
|
|
{
|
2019-02-12 13:04:45 -07:00
|
|
|
var cacheKey = GenerateKey("series", zap2ItId, language);
|
2019-09-25 08:19:48 -07:00
|
|
|
return TryGetValue(cacheKey, language, () => TvDbClient.Search.SearchSeriesByZap2ItIdAsync(zap2ItId, cancellationToken));
|
2019-02-07 13:59:57 -07:00
|
|
|
}
|
2020-05-30 23:15:34 -07:00
|
|
|
|
2019-09-25 08:19:48 -07:00
|
|
|
public Task<TvDbResponse<Actor[]>> GetActorsAsync(
|
|
|
|
int tvdbId,
|
|
|
|
string language,
|
2019-02-12 11:50:19 -07:00
|
|
|
CancellationToken cancellationToken)
|
2019-02-07 13:59:57 -07:00
|
|
|
{
|
2019-02-12 13:04:45 -07:00
|
|
|
var cacheKey = GenerateKey("actors", tvdbId, language);
|
2019-09-25 08:19:48 -07:00
|
|
|
return TryGetValue(cacheKey, language, () => TvDbClient.Series.GetActorsAsync(tvdbId, cancellationToken));
|
2019-02-07 13:59:57 -07:00
|
|
|
}
|
|
|
|
|
2019-09-25 08:19:48 -07:00
|
|
|
public Task<TvDbResponse<Image[]>> GetImagesAsync(
|
|
|
|
int tvdbId,
|
|
|
|
ImagesQuery imageQuery,
|
|
|
|
string language,
|
2019-02-12 11:50:19 -07:00
|
|
|
CancellationToken cancellationToken)
|
2019-02-07 13:59:57 -07:00
|
|
|
{
|
2019-02-12 13:04:45 -07:00
|
|
|
var cacheKey = GenerateKey("images", tvdbId, language, imageQuery);
|
2019-09-25 08:19:48 -07:00
|
|
|
return TryGetValue(cacheKey, language, () => TvDbClient.Series.GetImagesAsync(tvdbId, imageQuery, cancellationToken));
|
2019-02-07 14:42:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public Task<TvDbResponse<Language[]>> GetLanguagesAsync(CancellationToken cancellationToken)
|
|
|
|
{
|
2019-09-25 08:19:48 -07:00
|
|
|
return TryGetValue("languages", null, () => TvDbClient.Languages.GetAllAsync(cancellationToken));
|
2019-02-07 12:57:42 -07:00
|
|
|
}
|
|
|
|
|
2019-09-25 08:19:48 -07:00
|
|
|
public Task<TvDbResponse<EpisodesSummary>> GetSeriesEpisodeSummaryAsync(
|
|
|
|
int tvdbId,
|
|
|
|
string language,
|
2019-02-12 11:50:19 -07:00
|
|
|
CancellationToken cancellationToken)
|
2019-02-07 15:09:55 -07:00
|
|
|
{
|
2019-02-12 13:04:45 -07:00
|
|
|
var cacheKey = GenerateKey("seriesepisodesummary", tvdbId, language);
|
|
|
|
return TryGetValue(cacheKey, language,
|
2019-02-07 15:09:55 -07:00
|
|
|
() => TvDbClient.Series.GetEpisodesSummaryAsync(tvdbId, cancellationToken));
|
|
|
|
}
|
|
|
|
|
2019-09-25 08:19:48 -07:00
|
|
|
public Task<TvDbResponse<EpisodeRecord[]>> GetEpisodesPageAsync(
|
|
|
|
int tvdbId,
|
|
|
|
int page,
|
|
|
|
EpisodeQuery episodeQuery,
|
|
|
|
string language,
|
|
|
|
CancellationToken cancellationToken)
|
2019-02-07 15:56:57 -07:00
|
|
|
{
|
2019-02-12 13:04:45 -07:00
|
|
|
var cacheKey = GenerateKey(language, tvdbId, episodeQuery);
|
2019-02-12 11:50:19 -07:00
|
|
|
|
|
|
|
return TryGetValue(cacheKey, language,
|
2019-02-07 15:56:57 -07:00
|
|
|
() => TvDbClient.Series.GetEpisodesAsync(tvdbId, page, episodeQuery, cancellationToken));
|
|
|
|
}
|
|
|
|
|
2019-09-25 08:19:48 -07:00
|
|
|
public Task<string> GetEpisodeTvdbId(
|
|
|
|
EpisodeInfo searchInfo,
|
|
|
|
string language,
|
2019-02-12 11:50:19 -07:00
|
|
|
CancellationToken cancellationToken)
|
2019-02-08 11:48:18 -07:00
|
|
|
{
|
2020-06-06 12:17:49 -07:00
|
|
|
searchInfo.SeriesProviderIds.TryGetValue(MetadataProvider.Tvdb.ToString(),
|
2019-02-08 11:48:18 -07:00
|
|
|
out var seriesTvdbId);
|
|
|
|
|
2019-02-11 12:33:44 -07:00
|
|
|
var episodeQuery = new EpisodeQuery();
|
|
|
|
|
|
|
|
// Prefer SxE over premiere date as it is more robust
|
|
|
|
if (searchInfo.IndexNumber.HasValue && searchInfo.ParentIndexNumber.HasValue)
|
|
|
|
{
|
2019-06-22 14:11:47 -07:00
|
|
|
switch (searchInfo.SeriesDisplayOrder)
|
|
|
|
{
|
|
|
|
case "dvd":
|
|
|
|
episodeQuery.DvdEpisode = searchInfo.IndexNumber.Value;
|
|
|
|
episodeQuery.DvdSeason = searchInfo.ParentIndexNumber.Value;
|
|
|
|
break;
|
|
|
|
case "absolute":
|
|
|
|
episodeQuery.AbsoluteNumber = searchInfo.IndexNumber.Value;
|
|
|
|
break;
|
2019-08-07 02:24:56 -07:00
|
|
|
default:
|
2020-05-30 23:15:34 -07:00
|
|
|
// aired order
|
2019-06-22 14:11:47 -07:00
|
|
|
episodeQuery.AiredEpisode = searchInfo.IndexNumber.Value;
|
|
|
|
episodeQuery.AiredSeason = searchInfo.ParentIndexNumber.Value;
|
|
|
|
break;
|
|
|
|
}
|
2019-02-11 12:33:44 -07:00
|
|
|
}
|
|
|
|
else if (searchInfo.PremiereDate.HasValue)
|
|
|
|
{
|
|
|
|
// tvdb expects yyyy-mm-dd format
|
|
|
|
episodeQuery.FirstAired = searchInfo.PremiereDate.Value.ToString("yyyy-MM-dd");
|
|
|
|
}
|
|
|
|
|
2019-02-13 03:23:54 -07:00
|
|
|
return GetEpisodeTvdbId(Convert.ToInt32(seriesTvdbId), episodeQuery, language, cancellationToken);
|
2019-02-08 11:48:18 -07:00
|
|
|
}
|
|
|
|
|
2019-09-25 08:19:48 -07:00
|
|
|
public async Task<string> GetEpisodeTvdbId(
|
|
|
|
int seriesTvdbId,
|
|
|
|
EpisodeQuery episodeQuery,
|
2019-02-13 03:23:54 -07:00
|
|
|
string language,
|
|
|
|
CancellationToken cancellationToken)
|
2019-02-08 11:48:18 -07:00
|
|
|
{
|
2019-02-20 11:35:47 -07:00
|
|
|
var episodePage =
|
|
|
|
await GetEpisodesPageAsync(Convert.ToInt32(seriesTvdbId), episodeQuery, language, cancellationToken)
|
|
|
|
.ConfigureAwait(false);
|
2019-02-08 11:48:18 -07:00
|
|
|
return episodePage.Data.FirstOrDefault()?.Id.ToString();
|
|
|
|
}
|
|
|
|
|
2019-09-25 08:19:48 -07:00
|
|
|
public Task<TvDbResponse<EpisodeRecord[]>> GetEpisodesPageAsync(
|
|
|
|
int tvdbId,
|
|
|
|
EpisodeQuery episodeQuery,
|
|
|
|
string language,
|
|
|
|
CancellationToken cancellationToken)
|
2019-02-07 15:09:55 -07:00
|
|
|
{
|
2019-02-12 11:50:19 -07:00
|
|
|
return GetEpisodesPageAsync(tvdbId, 1, episodeQuery, language, cancellationToken);
|
2019-02-07 15:09:55 -07:00
|
|
|
}
|
|
|
|
|
2020-07-16 06:32:58 -07:00
|
|
|
public async IAsyncEnumerable<KeyType> GetImageKeyTypesForSeriesAsync(int tvdbId, string language, [EnumeratorCancellation] CancellationToken cancellationToken)
|
2020-07-15 07:45:14 -07:00
|
|
|
{
|
|
|
|
var cacheKey = GenerateKey(nameof(TvDbClient.Series.GetImagesSummaryAsync), tvdbId);
|
|
|
|
var imagesSummary = await TryGetValue(cacheKey, language, () => TvDbClient.Series.GetImagesSummaryAsync(tvdbId, cancellationToken)).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (imagesSummary.Data.Fanart > 0)
|
|
|
|
{
|
2020-07-15 08:14:39 -07:00
|
|
|
yield return KeyType.Fanart;
|
2020-07-15 07:45:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (imagesSummary.Data.Series > 0)
|
|
|
|
{
|
2020-07-15 08:14:39 -07:00
|
|
|
yield return KeyType.Series;
|
2020-07-15 07:45:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (imagesSummary.Data.Poster > 0)
|
|
|
|
{
|
2020-07-15 08:14:39 -07:00
|
|
|
yield return KeyType.Poster;
|
2020-07-15 07:45:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 06:32:58 -07:00
|
|
|
public async IAsyncEnumerable<KeyType> GetImageKeyTypesForSeasonAsync(int tvdbId, string language, [EnumeratorCancellation] CancellationToken cancellationToken)
|
2020-07-15 07:45:14 -07:00
|
|
|
{
|
|
|
|
var cacheKey = GenerateKey(nameof(TvDbClient.Series.GetImagesSummaryAsync), tvdbId);
|
|
|
|
var imagesSummary = await TryGetValue(cacheKey, language, () => TvDbClient.Series.GetImagesSummaryAsync(tvdbId, cancellationToken)).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (imagesSummary.Data.Season > 0)
|
|
|
|
{
|
2020-07-15 08:14:39 -07:00
|
|
|
yield return KeyType.Season;
|
2020-07-15 07:45:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (imagesSummary.Data.Fanart > 0)
|
|
|
|
{
|
2020-07-15 08:14:39 -07:00
|
|
|
yield return KeyType.Fanart;
|
2020-07-15 07:45:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO seasonwide is not supported in TvDbSharper
|
|
|
|
}
|
|
|
|
|
2019-02-12 11:50:19 -07:00
|
|
|
private async Task<T> TryGetValue<T>(string key, string language, Func<Task<T>> resultFactory)
|
2019-02-07 12:57:42 -07:00
|
|
|
{
|
|
|
|
if (_cache.TryGetValue(key, out T cachedValue))
|
2019-02-07 12:28:19 -07:00
|
|
|
{
|
2019-02-07 12:57:42 -07:00
|
|
|
return cachedValue;
|
|
|
|
}
|
2019-02-07 13:05:58 -07:00
|
|
|
|
|
|
|
await _cacheWriteLock.WaitAsync().ConfigureAwait(false);
|
|
|
|
try
|
2019-02-07 12:57:42 -07:00
|
|
|
{
|
|
|
|
if (_cache.TryGetValue(key, out cachedValue))
|
|
|
|
{
|
|
|
|
return cachedValue;
|
|
|
|
}
|
2019-02-07 13:05:58 -07:00
|
|
|
|
2019-02-20 03:03:04 -07:00
|
|
|
_tvDbClient.AcceptedLanguage = TvdbUtils.NormalizeLanguage(language) ?? DefaultLanguage;
|
2019-02-20 11:35:47 -07:00
|
|
|
var result = await resultFactory.Invoke().ConfigureAwait(false);
|
2019-02-07 13:05:58 -07:00
|
|
|
_cache.Set(key, result, TimeSpan.FromHours(1));
|
2019-02-07 12:57:42 -07:00
|
|
|
return result;
|
2019-02-07 12:28:19 -07:00
|
|
|
}
|
2019-02-07 13:05:58 -07:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
_cacheWriteLock.Release();
|
|
|
|
}
|
2019-02-07 12:28:19 -07:00
|
|
|
}
|
2019-02-12 11:50:19 -07:00
|
|
|
|
2019-02-12 13:04:45 -07:00
|
|
|
private static string GenerateKey(params object[] objects)
|
2019-02-12 11:50:19 -07:00
|
|
|
{
|
|
|
|
var key = string.Empty;
|
|
|
|
|
|
|
|
foreach (var obj in objects)
|
|
|
|
{
|
|
|
|
var objType = obj.GetType();
|
|
|
|
if (objType.IsPrimitive || objType == typeof(string))
|
|
|
|
{
|
2019-02-12 13:30:52 -07:00
|
|
|
key += obj + ";";
|
2019-02-12 11:50:19 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach (PropertyInfo propertyInfo in objType.GetProperties())
|
|
|
|
{
|
|
|
|
var currentValue = propertyInfo.GetValue(obj, null);
|
|
|
|
if (currentValue == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-02-13 03:23:54 -07:00
|
|
|
key += propertyInfo.Name + "=" + currentValue + ";";
|
2019-02-12 11:50:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
2019-01-30 15:28:43 -07:00
|
|
|
}
|
|
|
|
}
|