TvDbClientManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using MediaBrowser.Controller.Library;
  5. using Microsoft.Extensions.Caching.Memory;
  6. using Microsoft.Extensions.Internal;
  7. using TvDbSharper;
  8. using TvDbSharper.Dto;
  9. namespace MediaBrowser.Providers.TV
  10. {
  11. // TODO add to DI once Bond's PR is merged
  12. public sealed class TvDbClientManager
  13. {
  14. private static volatile TvDbClientManager instance;
  15. // TODO add to DI once Bond's PR is merged
  16. private readonly SemaphoreSlim _cacheWriteLock = new SemaphoreSlim(1, 1);
  17. private static MemoryCache _cache;
  18. private static readonly object syncRoot = new object();
  19. private static TvDbClient tvDbClient;
  20. private static DateTime tokenCreatedAt;
  21. private TvDbClientManager()
  22. {
  23. tvDbClient = new TvDbClient();
  24. tvDbClient.Authentication.AuthenticateAsync(TVUtils.TvdbApiKey);
  25. tokenCreatedAt = DateTime.Now;
  26. }
  27. public static TvDbClientManager Instance
  28. {
  29. get
  30. {
  31. if (instance != null)
  32. {
  33. return instance;
  34. }
  35. lock (syncRoot)
  36. {
  37. if (instance == null)
  38. {
  39. instance = new TvDbClientManager();
  40. _cache = new MemoryCache(new MemoryCacheOptions());
  41. }
  42. }
  43. return instance;
  44. }
  45. }
  46. public TvDbClient TvDbClient
  47. {
  48. get
  49. {
  50. // Refresh if necessary
  51. if (tokenCreatedAt > DateTime.Now.Subtract(TimeSpan.FromHours(20)))
  52. {
  53. try
  54. {
  55. tvDbClient.Authentication.RefreshTokenAsync();
  56. }
  57. catch
  58. {
  59. tvDbClient.Authentication.AuthenticateAsync(TVUtils.TvdbApiKey);
  60. }
  61. tokenCreatedAt = DateTime.Now;
  62. }
  63. // Default to English
  64. tvDbClient.AcceptedLanguage = "en";
  65. return tvDbClient;
  66. }
  67. }
  68. public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByNameAsync(string name, CancellationToken cancellationToken)
  69. {
  70. return TryGetValue(name,() => TvDbClient.Search.SearchSeriesByNameAsync(name, cancellationToken));
  71. }
  72. public Task<TvDbResponse<Series>> GetSeriesByIdAsync(int tvdbId, CancellationToken cancellationToken)
  73. {
  74. return TryGetValue(tvdbId,() => TvDbClient.Series.GetAsync(tvdbId, cancellationToken));
  75. }
  76. public Task<TvDbResponse<EpisodeRecord>> GetEpisodesAsync(int tvdbId, CancellationToken cancellationToken)
  77. {
  78. return TryGetValue(tvdbId,() => TvDbClient.Episodes.GetAsync(tvdbId, cancellationToken));
  79. }
  80. public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByImdbIdAsync(string imdbId, CancellationToken cancellationToken)
  81. {
  82. return TryGetValue(imdbId,() => TvDbClient.Search.SearchSeriesByImdbIdAsync(imdbId, cancellationToken));
  83. }
  84. public Task<TvDbResponse<SeriesSearchResult[]>> GetSeriesByZap2ItIdAsync(string zap2ItId, CancellationToken cancellationToken)
  85. {
  86. return TryGetValue(zap2ItId,() => TvDbClient.Search.SearchSeriesByImdbIdAsync(zap2ItId, cancellationToken));
  87. }
  88. public Task<TvDbResponse<Actor[]>> GetActorsAsync(int tvdbId, CancellationToken cancellationToken)
  89. {
  90. return TryGetValue(tvdbId,() => TvDbClient.Series.GetActorsAsync(tvdbId, cancellationToken));
  91. }
  92. public Task<TvDbResponse<Image[]>> GetImagesAsync(int tvdbId, ImagesQuery imageQuery, CancellationToken cancellationToken)
  93. {
  94. return TryGetValue(tvdbId,() => TvDbClient.Series.GetImagesAsync(tvdbId, imageQuery, cancellationToken));
  95. }
  96. private async Task<T> TryGetValue<T>(object key, Func<Task<T>> resultFactory)
  97. {
  98. if (_cache.TryGetValue(key, out T cachedValue))
  99. {
  100. Console.WriteLine("Cache hit!!!");
  101. return cachedValue;
  102. }
  103. await _cacheWriteLock.WaitAsync().ConfigureAwait(false);
  104. try
  105. {
  106. if (_cache.TryGetValue(key, out cachedValue))
  107. {
  108. return cachedValue;
  109. }
  110. var result = await resultFactory.Invoke();
  111. _cache.Set(key, result, TimeSpan.FromHours(1));
  112. return result;
  113. }
  114. finally
  115. {
  116. _cacheWriteLock.Release();
  117. }
  118. }
  119. }
  120. }