TmdbClientManager.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. #nullable disable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Model.Dto;
  8. using MediaBrowser.Model.Entities;
  9. using MediaBrowser.Model.Providers;
  10. using Microsoft.Extensions.Caching.Memory;
  11. using TMDbLib.Client;
  12. using TMDbLib.Objects.Collections;
  13. using TMDbLib.Objects.Find;
  14. using TMDbLib.Objects.General;
  15. using TMDbLib.Objects.Movies;
  16. using TMDbLib.Objects.People;
  17. using TMDbLib.Objects.Search;
  18. using TMDbLib.Objects.TvShows;
  19. namespace MediaBrowser.Providers.Plugins.Tmdb
  20. {
  21. /// <summary>
  22. /// Manager class for abstracting the TMDb API client library.
  23. /// </summary>
  24. public class TmdbClientManager : IDisposable
  25. {
  26. private const int CacheDurationInHours = 1;
  27. private readonly IMemoryCache _memoryCache;
  28. private readonly TMDbClient _tmDbClient;
  29. /// <summary>
  30. /// Initializes a new instance of the <see cref="TmdbClientManager"/> class.
  31. /// </summary>
  32. /// <param name="memoryCache">An instance of <see cref="IMemoryCache"/>.</param>
  33. public TmdbClientManager(IMemoryCache memoryCache)
  34. {
  35. _memoryCache = memoryCache;
  36. _tmDbClient = new TMDbClient(TmdbUtils.ApiKey);
  37. // Not really interested in NotFoundException
  38. _tmDbClient.ThrowApiExceptions = false;
  39. }
  40. /// <summary>
  41. /// Gets a movie from the TMDb API based on its TMDb id.
  42. /// </summary>
  43. /// <param name="tmdbId">The movie's TMDb id.</param>
  44. /// <param name="language">The movie's language.</param>
  45. /// <param name="imageLanguages">A comma-separated list of image languages.</param>
  46. /// <param name="cancellationToken">The cancellation token.</param>
  47. /// <returns>The TMDb movie or null if not found.</returns>
  48. public async Task<Movie> GetMovieAsync(int tmdbId, string language, string imageLanguages, CancellationToken cancellationToken)
  49. {
  50. var key = $"movie-{tmdbId.ToString(CultureInfo.InvariantCulture)}-{language}";
  51. if (_memoryCache.TryGetValue(key, out Movie movie))
  52. {
  53. return movie;
  54. }
  55. await EnsureClientConfigAsync().ConfigureAwait(false);
  56. var extraMethods = MovieMethods.Credits | MovieMethods.Releases | MovieMethods.Images | MovieMethods.Videos;
  57. if (!(Plugin.Instance?.Configuration.ExcludeTagsMovies).GetValueOrDefault())
  58. {
  59. extraMethods |= MovieMethods.Keywords;
  60. }
  61. movie = await _tmDbClient.GetMovieAsync(
  62. tmdbId,
  63. TmdbUtils.NormalizeLanguage(language),
  64. imageLanguages,
  65. extraMethods,
  66. cancellationToken).ConfigureAwait(false);
  67. if (movie != null)
  68. {
  69. _memoryCache.Set(key, movie, TimeSpan.FromHours(CacheDurationInHours));
  70. }
  71. return movie;
  72. }
  73. /// <summary>
  74. /// Gets a collection from the TMDb API based on its TMDb id.
  75. /// </summary>
  76. /// <param name="tmdbId">The collection's TMDb id.</param>
  77. /// <param name="language">The collection's language.</param>
  78. /// <param name="imageLanguages">A comma-separated list of image languages.</param>
  79. /// <param name="cancellationToken">The cancellation token.</param>
  80. /// <returns>The TMDb collection or null if not found.</returns>
  81. public async Task<Collection> GetCollectionAsync(int tmdbId, string language, string imageLanguages, CancellationToken cancellationToken)
  82. {
  83. var key = $"collection-{tmdbId.ToString(CultureInfo.InvariantCulture)}-{language}";
  84. if (_memoryCache.TryGetValue(key, out Collection collection))
  85. {
  86. return collection;
  87. }
  88. await EnsureClientConfigAsync().ConfigureAwait(false);
  89. collection = await _tmDbClient.GetCollectionAsync(
  90. tmdbId,
  91. TmdbUtils.NormalizeLanguage(language),
  92. imageLanguages,
  93. CollectionMethods.Images,
  94. cancellationToken).ConfigureAwait(false);
  95. if (collection != null)
  96. {
  97. _memoryCache.Set(key, collection, TimeSpan.FromHours(CacheDurationInHours));
  98. }
  99. return collection;
  100. }
  101. /// <summary>
  102. /// Gets a tv show from the TMDb API based on its TMDb id.
  103. /// </summary>
  104. /// <param name="tmdbId">The tv show's TMDb id.</param>
  105. /// <param name="language">The tv show's language.</param>
  106. /// <param name="imageLanguages">A comma-separated list of image languages.</param>
  107. /// <param name="cancellationToken">The cancellation token.</param>
  108. /// <returns>The TMDb tv show information or null if not found.</returns>
  109. public async Task<TvShow> GetSeriesAsync(int tmdbId, string language, string imageLanguages, CancellationToken cancellationToken)
  110. {
  111. var key = $"series-{tmdbId.ToString(CultureInfo.InvariantCulture)}-{language}";
  112. if (_memoryCache.TryGetValue(key, out TvShow series))
  113. {
  114. return series;
  115. }
  116. await EnsureClientConfigAsync().ConfigureAwait(false);
  117. var extraMethods = TvShowMethods.Credits | TvShowMethods.Images | TvShowMethods.ExternalIds | TvShowMethods.Videos | TvShowMethods.ContentRatings | TvShowMethods.EpisodeGroups;
  118. if (!(Plugin.Instance?.Configuration.ExcludeTagsSeries).GetValueOrDefault())
  119. {
  120. extraMethods |= TvShowMethods.Keywords;
  121. }
  122. series = await _tmDbClient.GetTvShowAsync(
  123. tmdbId,
  124. language: TmdbUtils.NormalizeLanguage(language),
  125. includeImageLanguage: imageLanguages,
  126. extraMethods: extraMethods,
  127. cancellationToken: cancellationToken).ConfigureAwait(false);
  128. if (series != null)
  129. {
  130. _memoryCache.Set(key, series, TimeSpan.FromHours(CacheDurationInHours));
  131. }
  132. return series;
  133. }
  134. /// <summary>
  135. /// Gets a tv show episode group from the TMDb API based on the show id and the display order.
  136. /// </summary>
  137. /// <param name="tvShowId">The tv show's TMDb id.</param>
  138. /// <param name="displayOrder">The display order.</param>
  139. /// <param name="language">The tv show's language.</param>
  140. /// <param name="imageLanguages">A comma-separated list of image languages.</param>
  141. /// <param name="cancellationToken">The cancellation token.</param>
  142. /// <returns>The TMDb tv show episode group information or null if not found.</returns>
  143. private async Task<TvGroupCollection> GetSeriesGroupAsync(int tvShowId, string displayOrder, string language, string imageLanguages, CancellationToken cancellationToken)
  144. {
  145. TvGroupType? groupType =
  146. string.Equals(displayOrder, "absolute", StringComparison.Ordinal) ? TvGroupType.Absolute :
  147. string.Equals(displayOrder, "dvd", StringComparison.Ordinal) ? TvGroupType.DVD :
  148. null;
  149. if (groupType == null)
  150. {
  151. return null;
  152. }
  153. var key = $"group-{tvShowId.ToString(CultureInfo.InvariantCulture)}-{displayOrder}-{language}";
  154. if (_memoryCache.TryGetValue(key, out TvGroupCollection group))
  155. {
  156. return group;
  157. }
  158. await EnsureClientConfigAsync().ConfigureAwait(false);
  159. var series = await GetSeriesAsync(tvShowId, language, imageLanguages, cancellationToken).ConfigureAwait(false);
  160. var episodeGroupId = series?.EpisodeGroups.Results.Find(g => g.Type == groupType)?.Id;
  161. if (episodeGroupId == null)
  162. {
  163. return null;
  164. }
  165. group = await _tmDbClient.GetTvEpisodeGroupsAsync(
  166. episodeGroupId,
  167. language: TmdbUtils.NormalizeLanguage(language),
  168. cancellationToken: cancellationToken).ConfigureAwait(false);
  169. if (group != null)
  170. {
  171. _memoryCache.Set(key, group, TimeSpan.FromHours(CacheDurationInHours));
  172. }
  173. return group;
  174. }
  175. /// <summary>
  176. /// Gets a tv season from the TMDb API based on the tv show's TMDb id.
  177. /// </summary>
  178. /// <param name="tvShowId">The tv season's TMDb id.</param>
  179. /// <param name="seasonNumber">The season number.</param>
  180. /// <param name="language">The tv season's language.</param>
  181. /// <param name="imageLanguages">A comma-separated list of image languages.</param>
  182. /// <param name="cancellationToken">The cancellation token.</param>
  183. /// <returns>The TMDb tv season information or null if not found.</returns>
  184. public async Task<TvSeason> GetSeasonAsync(int tvShowId, int seasonNumber, string language, string imageLanguages, CancellationToken cancellationToken)
  185. {
  186. var key = $"season-{tvShowId.ToString(CultureInfo.InvariantCulture)}-s{seasonNumber.ToString(CultureInfo.InvariantCulture)}-{language}";
  187. if (_memoryCache.TryGetValue(key, out TvSeason season))
  188. {
  189. return season;
  190. }
  191. await EnsureClientConfigAsync().ConfigureAwait(false);
  192. season = await _tmDbClient.GetTvSeasonAsync(
  193. tvShowId,
  194. seasonNumber,
  195. language: TmdbUtils.NormalizeLanguage(language),
  196. includeImageLanguage: imageLanguages,
  197. extraMethods: TvSeasonMethods.Credits | TvSeasonMethods.Images | TvSeasonMethods.ExternalIds | TvSeasonMethods.Videos,
  198. cancellationToken: cancellationToken).ConfigureAwait(false);
  199. if (season != null)
  200. {
  201. _memoryCache.Set(key, season, TimeSpan.FromHours(CacheDurationInHours));
  202. }
  203. return season;
  204. }
  205. /// <summary>
  206. /// Gets a movie from the TMDb API based on the tv show's TMDb id.
  207. /// </summary>
  208. /// <param name="tvShowId">The tv show's TMDb id.</param>
  209. /// <param name="seasonNumber">The season number.</param>
  210. /// <param name="episodeNumber">The episode number.</param>
  211. /// <param name="displayOrder">The display order.</param>
  212. /// <param name="language">The episode's language.</param>
  213. /// <param name="imageLanguages">A comma-separated list of image languages.</param>
  214. /// <param name="cancellationToken">The cancellation token.</param>
  215. /// <returns>The TMDb tv episode information or null if not found.</returns>
  216. public async Task<TvEpisode> GetEpisodeAsync(int tvShowId, int seasonNumber, int episodeNumber, string displayOrder, string language, string imageLanguages, CancellationToken cancellationToken)
  217. {
  218. var key = $"episode-{tvShowId.ToString(CultureInfo.InvariantCulture)}-s{seasonNumber.ToString(CultureInfo.InvariantCulture)}e{episodeNumber.ToString(CultureInfo.InvariantCulture)}-{displayOrder}-{language}";
  219. if (_memoryCache.TryGetValue(key, out TvEpisode episode))
  220. {
  221. return episode;
  222. }
  223. await EnsureClientConfigAsync().ConfigureAwait(false);
  224. var group = await GetSeriesGroupAsync(tvShowId, displayOrder, language, imageLanguages, cancellationToken).ConfigureAwait(false);
  225. if (group != null)
  226. {
  227. var season = group.Groups.Find(s => s.Order == seasonNumber);
  228. // Episode order starts at 0
  229. var ep = season?.Episodes.Find(e => e.Order == episodeNumber - 1);
  230. if (ep != null)
  231. {
  232. seasonNumber = ep.SeasonNumber;
  233. episodeNumber = ep.EpisodeNumber;
  234. }
  235. }
  236. episode = await _tmDbClient.GetTvEpisodeAsync(
  237. tvShowId,
  238. seasonNumber,
  239. episodeNumber,
  240. language: TmdbUtils.NormalizeLanguage(language),
  241. includeImageLanguage: imageLanguages,
  242. extraMethods: TvEpisodeMethods.Credits | TvEpisodeMethods.Images | TvEpisodeMethods.ExternalIds | TvEpisodeMethods.Videos,
  243. cancellationToken: cancellationToken).ConfigureAwait(false);
  244. if (episode != null)
  245. {
  246. _memoryCache.Set(key, episode, TimeSpan.FromHours(CacheDurationInHours));
  247. }
  248. return episode;
  249. }
  250. /// <summary>
  251. /// Gets a person eg. cast or crew member from the TMDb API based on its TMDb id.
  252. /// </summary>
  253. /// <param name="personTmdbId">The person's TMDb id.</param>
  254. /// <param name="language">The episode's language.</param>
  255. /// <param name="cancellationToken">The cancellation token.</param>
  256. /// <returns>The TMDb person information or null if not found.</returns>
  257. public async Task<Person> GetPersonAsync(int personTmdbId, string language, CancellationToken cancellationToken)
  258. {
  259. var key = $"person-{personTmdbId.ToString(CultureInfo.InvariantCulture)}-{language}";
  260. if (_memoryCache.TryGetValue(key, out Person person))
  261. {
  262. return person;
  263. }
  264. await EnsureClientConfigAsync().ConfigureAwait(false);
  265. person = await _tmDbClient.GetPersonAsync(
  266. personTmdbId,
  267. TmdbUtils.NormalizeLanguage(language),
  268. PersonMethods.TvCredits | PersonMethods.MovieCredits | PersonMethods.Images | PersonMethods.ExternalIds,
  269. cancellationToken).ConfigureAwait(false);
  270. if (person != null)
  271. {
  272. _memoryCache.Set(key, person, TimeSpan.FromHours(CacheDurationInHours));
  273. }
  274. return person;
  275. }
  276. /// <summary>
  277. /// Gets an item from the TMDb API based on its id from an external service eg. IMDb id, TvDb id.
  278. /// </summary>
  279. /// <param name="externalId">The item's external id.</param>
  280. /// <param name="source">The source of the id eg. IMDb.</param>
  281. /// <param name="language">The item's language.</param>
  282. /// <param name="cancellationToken">The cancellation token.</param>
  283. /// <returns>The TMDb item or null if not found.</returns>
  284. public async Task<FindContainer> FindByExternalIdAsync(
  285. string externalId,
  286. FindExternalSource source,
  287. string language,
  288. CancellationToken cancellationToken)
  289. {
  290. var key = $"find-{source.ToString()}-{externalId.ToString(CultureInfo.InvariantCulture)}-{language}";
  291. if (_memoryCache.TryGetValue(key, out FindContainer result))
  292. {
  293. return result;
  294. }
  295. await EnsureClientConfigAsync().ConfigureAwait(false);
  296. result = await _tmDbClient.FindAsync(
  297. source,
  298. externalId,
  299. TmdbUtils.NormalizeLanguage(language),
  300. cancellationToken).ConfigureAwait(false);
  301. if (result != null)
  302. {
  303. _memoryCache.Set(key, result, TimeSpan.FromHours(CacheDurationInHours));
  304. }
  305. return result;
  306. }
  307. /// <summary>
  308. /// Searches for a tv show using the TMDb API based on its name.
  309. /// </summary>
  310. /// <param name="name">The name of the tv show.</param>
  311. /// <param name="language">The tv show's language.</param>
  312. /// <param name="year">The year the tv show first aired.</param>
  313. /// <param name="cancellationToken">The cancellation token.</param>
  314. /// <returns>The TMDb tv show information.</returns>
  315. public async Task<IReadOnlyList<SearchTv>> SearchSeriesAsync(string name, string language, int year = 0, CancellationToken cancellationToken = default)
  316. {
  317. var key = $"searchseries-{name}-{language}";
  318. if (_memoryCache.TryGetValue(key, out SearchContainer<SearchTv> series))
  319. {
  320. return series.Results;
  321. }
  322. await EnsureClientConfigAsync().ConfigureAwait(false);
  323. var searchResults = await _tmDbClient
  324. .SearchTvShowAsync(name, TmdbUtils.NormalizeLanguage(language), includeAdult: Plugin.Instance.Configuration.IncludeAdult, firstAirDateYear: year, cancellationToken: cancellationToken)
  325. .ConfigureAwait(false);
  326. if (searchResults.Results.Count > 0)
  327. {
  328. _memoryCache.Set(key, searchResults, TimeSpan.FromHours(CacheDurationInHours));
  329. }
  330. return searchResults.Results;
  331. }
  332. /// <summary>
  333. /// Searches for a person based on their name using the TMDb API.
  334. /// </summary>
  335. /// <param name="name">The name of the person.</param>
  336. /// <param name="cancellationToken">The cancellation token.</param>
  337. /// <returns>The TMDb person information.</returns>
  338. public async Task<IReadOnlyList<SearchPerson>> SearchPersonAsync(string name, CancellationToken cancellationToken)
  339. {
  340. var key = $"searchperson-{name}";
  341. if (_memoryCache.TryGetValue(key, out SearchContainer<SearchPerson> person))
  342. {
  343. return person.Results;
  344. }
  345. await EnsureClientConfigAsync().ConfigureAwait(false);
  346. var searchResults = await _tmDbClient
  347. .SearchPersonAsync(name, includeAdult: Plugin.Instance.Configuration.IncludeAdult, cancellationToken: cancellationToken)
  348. .ConfigureAwait(false);
  349. if (searchResults.Results.Count > 0)
  350. {
  351. _memoryCache.Set(key, searchResults, TimeSpan.FromHours(CacheDurationInHours));
  352. }
  353. return searchResults.Results;
  354. }
  355. /// <summary>
  356. /// Searches for a movie based on its name using the TMDb API.
  357. /// </summary>
  358. /// <param name="name">The name of the movie.</param>
  359. /// <param name="language">The movie's language.</param>
  360. /// <param name="cancellationToken">The cancellation token.</param>
  361. /// <returns>The TMDb movie information.</returns>
  362. public Task<IReadOnlyList<SearchMovie>> SearchMovieAsync(string name, string language, CancellationToken cancellationToken)
  363. {
  364. return SearchMovieAsync(name, 0, language, cancellationToken);
  365. }
  366. /// <summary>
  367. /// Searches for a movie based on its name using the TMDb API.
  368. /// </summary>
  369. /// <param name="name">The name of the movie.</param>
  370. /// <param name="year">The release year of the movie.</param>
  371. /// <param name="language">The movie's language.</param>
  372. /// <param name="cancellationToken">The cancellation token.</param>
  373. /// <returns>The TMDb movie information.</returns>
  374. public async Task<IReadOnlyList<SearchMovie>> SearchMovieAsync(string name, int year, string language, CancellationToken cancellationToken)
  375. {
  376. var key = $"moviesearch-{name}-{year.ToString(CultureInfo.InvariantCulture)}-{language}";
  377. if (_memoryCache.TryGetValue(key, out SearchContainer<SearchMovie> movies))
  378. {
  379. return movies.Results;
  380. }
  381. await EnsureClientConfigAsync().ConfigureAwait(false);
  382. var searchResults = await _tmDbClient
  383. .SearchMovieAsync(name, TmdbUtils.NormalizeLanguage(language), includeAdult: Plugin.Instance.Configuration.IncludeAdult, year: year, cancellationToken: cancellationToken)
  384. .ConfigureAwait(false);
  385. if (searchResults.Results.Count > 0)
  386. {
  387. _memoryCache.Set(key, searchResults, TimeSpan.FromHours(CacheDurationInHours));
  388. }
  389. return searchResults.Results;
  390. }
  391. /// <summary>
  392. /// Searches for a collection based on its name using the TMDb API.
  393. /// </summary>
  394. /// <param name="name">The name of the collection.</param>
  395. /// <param name="language">The collection's language.</param>
  396. /// <param name="cancellationToken">The cancellation token.</param>
  397. /// <returns>The TMDb collection information.</returns>
  398. public async Task<IReadOnlyList<SearchCollection>> SearchCollectionAsync(string name, string language, CancellationToken cancellationToken)
  399. {
  400. var key = $"collectionsearch-{name}-{language}";
  401. if (_memoryCache.TryGetValue(key, out SearchContainer<SearchCollection> collections))
  402. {
  403. return collections.Results;
  404. }
  405. await EnsureClientConfigAsync().ConfigureAwait(false);
  406. var searchResults = await _tmDbClient
  407. .SearchCollectionAsync(name, TmdbUtils.NormalizeLanguage(language), cancellationToken: cancellationToken)
  408. .ConfigureAwait(false);
  409. if (searchResults.Results.Count > 0)
  410. {
  411. _memoryCache.Set(key, searchResults, TimeSpan.FromHours(CacheDurationInHours));
  412. }
  413. return searchResults.Results;
  414. }
  415. /// <summary>
  416. /// Handles bad path checking and builds the absolute url.
  417. /// </summary>
  418. /// <param name="size">The image size to fetch.</param>
  419. /// <param name="path">The relative URL of the image.</param>
  420. /// <returns>The absolute URL.</returns>
  421. private string GetUrl(string size, string path)
  422. {
  423. if (string.IsNullOrEmpty(path))
  424. {
  425. return null;
  426. }
  427. return _tmDbClient.GetImageUrl(size, path, true).ToString();
  428. }
  429. /// <summary>
  430. /// Gets the absolute URL of the poster.
  431. /// </summary>
  432. /// <param name="posterPath">The relative URL of the poster.</param>
  433. /// <returns>The absolute URL.</returns>
  434. public string GetPosterUrl(string posterPath)
  435. {
  436. return GetUrl(Plugin.Instance.Configuration.PosterSize, posterPath);
  437. }
  438. /// <summary>
  439. /// Gets the absolute URL of the profile image.
  440. /// </summary>
  441. /// <param name="actorProfilePath">The relative URL of the profile image.</param>
  442. /// <returns>The absolute URL.</returns>
  443. public string GetProfileUrl(string actorProfilePath)
  444. {
  445. return GetUrl(Plugin.Instance.Configuration.ProfileSize, actorProfilePath);
  446. }
  447. /// <summary>
  448. /// Converts poster <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
  449. /// </summary>
  450. /// <param name="images">The input images.</param>
  451. /// <param name="requestLanguage">The requested language.</param>
  452. /// <param name="results">The collection to add the remote images into.</param>
  453. public void ConvertPostersToRemoteImageInfo(List<ImageData> images, string requestLanguage, List<RemoteImageInfo> results)
  454. {
  455. ConvertToRemoteImageInfo(images, Plugin.Instance.Configuration.PosterSize, ImageType.Primary, requestLanguage, results);
  456. }
  457. /// <summary>
  458. /// Converts backdrop <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
  459. /// </summary>
  460. /// <param name="images">The input images.</param>
  461. /// <param name="requestLanguage">The requested language.</param>
  462. /// <param name="results">The collection to add the remote images into.</param>
  463. public void ConvertBackdropsToRemoteImageInfo(List<ImageData> images, string requestLanguage, List<RemoteImageInfo> results)
  464. {
  465. ConvertToRemoteImageInfo(images, Plugin.Instance.Configuration.BackdropSize, ImageType.Backdrop, requestLanguage, results);
  466. }
  467. /// <summary>
  468. /// Converts profile <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
  469. /// </summary>
  470. /// <param name="images">The input images.</param>
  471. /// <param name="requestLanguage">The requested language.</param>
  472. /// <param name="results">The collection to add the remote images into.</param>
  473. public void ConvertProfilesToRemoteImageInfo(List<ImageData> images, string requestLanguage, List<RemoteImageInfo> results)
  474. {
  475. ConvertToRemoteImageInfo(images, Plugin.Instance.Configuration.ProfileSize, ImageType.Primary, requestLanguage, results);
  476. }
  477. /// <summary>
  478. /// Converts still <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
  479. /// </summary>
  480. /// <param name="images">The input images.</param>
  481. /// <param name="requestLanguage">The requested language.</param>
  482. /// <param name="results">The collection to add the remote images into.</param>
  483. public void ConvertStillsToRemoteImageInfo(List<ImageData> images, string requestLanguage, List<RemoteImageInfo> results)
  484. {
  485. ConvertToRemoteImageInfo(images, Plugin.Instance.Configuration.StillSize, ImageType.Primary, requestLanguage, results);
  486. }
  487. /// <summary>
  488. /// Converts <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
  489. /// </summary>
  490. /// <param name="images">The input images.</param>
  491. /// <param name="size">The size of the image to fetch.</param>
  492. /// <param name="type">The type of the image.</param>
  493. /// <param name="requestLanguage">The requested language.</param>
  494. /// <param name="results">The collection to add the remote images into.</param>
  495. private void ConvertToRemoteImageInfo(List<ImageData> images, string size, ImageType type, string requestLanguage, List<RemoteImageInfo> results)
  496. {
  497. // sizes provided are for original resolution, don't store them when downloading scaled images
  498. var scaleImage = !string.Equals(size, "original", StringComparison.OrdinalIgnoreCase);
  499. for (var i = 0; i < images.Count; i++)
  500. {
  501. var image = images[i];
  502. results.Add(new RemoteImageInfo
  503. {
  504. Url = GetUrl(size, image.FilePath),
  505. CommunityRating = image.VoteAverage,
  506. VoteCount = image.VoteCount,
  507. Width = scaleImage ? null : image.Width,
  508. Height = scaleImage ? null : image.Height,
  509. Language = TmdbUtils.AdjustImageLanguage(image.Iso_639_1, requestLanguage),
  510. ProviderName = TmdbUtils.ProviderName,
  511. Type = type,
  512. RatingType = RatingType.Score
  513. });
  514. }
  515. }
  516. private async Task EnsureClientConfigAsync()
  517. {
  518. if (!_tmDbClient.HasConfig)
  519. {
  520. var config = await _tmDbClient.GetConfigAsync().ConfigureAwait(false);
  521. ValidatePreferences(config);
  522. }
  523. }
  524. private static void ValidatePreferences(TMDbConfig config)
  525. {
  526. var imageConfig = config.Images;
  527. var pluginConfig = Plugin.Instance.Configuration;
  528. if (!imageConfig.PosterSizes.Contains(pluginConfig.PosterSize))
  529. {
  530. pluginConfig.PosterSize = imageConfig.PosterSizes[^1];
  531. }
  532. if (!imageConfig.BackdropSizes.Contains(pluginConfig.BackdropSize))
  533. {
  534. pluginConfig.BackdropSize = imageConfig.BackdropSizes[^1];
  535. }
  536. if (!imageConfig.ProfileSizes.Contains(pluginConfig.ProfileSize))
  537. {
  538. pluginConfig.ProfileSize = imageConfig.ProfileSizes[^1];
  539. }
  540. if (!imageConfig.StillSizes.Contains(pluginConfig.StillSize))
  541. {
  542. pluginConfig.StillSize = imageConfig.StillSizes[^1];
  543. }
  544. }
  545. /// <summary>
  546. /// Gets the <see cref="TMDbClient"/> configuration.
  547. /// </summary>
  548. /// <returns>The configuration.</returns>
  549. public async Task<TMDbConfig> GetClientConfiguration()
  550. {
  551. await EnsureClientConfigAsync().ConfigureAwait(false);
  552. return _tmDbClient.Config;
  553. }
  554. /// <inheritdoc />
  555. public void Dispose()
  556. {
  557. Dispose(true);
  558. GC.SuppressFinalize(this);
  559. }
  560. /// <summary>
  561. /// Releases unmanaged and - optionally - managed resources.
  562. /// </summary>
  563. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  564. protected virtual void Dispose(bool disposing)
  565. {
  566. if (disposing)
  567. {
  568. _memoryCache?.Dispose();
  569. _tmDbClient?.Dispose();
  570. }
  571. }
  572. }
  573. }