TmdbClientManager.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. movie = await _tmDbClient.GetMovieAsync(
  57. tmdbId,
  58. TmdbUtils.NormalizeLanguage(language),
  59. imageLanguages,
  60. MovieMethods.Credits | MovieMethods.Releases | MovieMethods.Images | MovieMethods.Keywords | MovieMethods.Videos,
  61. cancellationToken).ConfigureAwait(false);
  62. if (movie != null)
  63. {
  64. _memoryCache.Set(key, movie, TimeSpan.FromHours(CacheDurationInHours));
  65. }
  66. return movie;
  67. }
  68. /// <summary>
  69. /// Gets a collection from the TMDb API based on its TMDb id.
  70. /// </summary>
  71. /// <param name="tmdbId">The collection's TMDb id.</param>
  72. /// <param name="language">The collection's language.</param>
  73. /// <param name="imageLanguages">A comma-separated list of image languages.</param>
  74. /// <param name="cancellationToken">The cancellation token.</param>
  75. /// <returns>The TMDb collection or null if not found.</returns>
  76. public async Task<Collection> GetCollectionAsync(int tmdbId, string language, string imageLanguages, CancellationToken cancellationToken)
  77. {
  78. var key = $"collection-{tmdbId.ToString(CultureInfo.InvariantCulture)}-{language}";
  79. if (_memoryCache.TryGetValue(key, out Collection collection))
  80. {
  81. return collection;
  82. }
  83. await EnsureClientConfigAsync().ConfigureAwait(false);
  84. collection = await _tmDbClient.GetCollectionAsync(
  85. tmdbId,
  86. TmdbUtils.NormalizeLanguage(language),
  87. imageLanguages,
  88. CollectionMethods.Images,
  89. cancellationToken).ConfigureAwait(false);
  90. if (collection != null)
  91. {
  92. _memoryCache.Set(key, collection, TimeSpan.FromHours(CacheDurationInHours));
  93. }
  94. return collection;
  95. }
  96. /// <summary>
  97. /// Gets a tv show from the TMDb API based on its TMDb id.
  98. /// </summary>
  99. /// <param name="tmdbId">The tv show's TMDb id.</param>
  100. /// <param name="language">The tv show's language.</param>
  101. /// <param name="imageLanguages">A comma-separated list of image languages.</param>
  102. /// <param name="cancellationToken">The cancellation token.</param>
  103. /// <returns>The TMDb tv show information or null if not found.</returns>
  104. public async Task<TvShow> GetSeriesAsync(int tmdbId, string language, string imageLanguages, CancellationToken cancellationToken)
  105. {
  106. var key = $"series-{tmdbId.ToString(CultureInfo.InvariantCulture)}-{language}";
  107. if (_memoryCache.TryGetValue(key, out TvShow series))
  108. {
  109. return series;
  110. }
  111. await EnsureClientConfigAsync().ConfigureAwait(false);
  112. series = await _tmDbClient.GetTvShowAsync(
  113. tmdbId,
  114. language: TmdbUtils.NormalizeLanguage(language),
  115. includeImageLanguage: imageLanguages,
  116. extraMethods: TvShowMethods.Credits | TvShowMethods.Images | TvShowMethods.Keywords | TvShowMethods.ExternalIds | TvShowMethods.Videos | TvShowMethods.ContentRatings | TvShowMethods.EpisodeGroups,
  117. cancellationToken: cancellationToken).ConfigureAwait(false);
  118. if (series != null)
  119. {
  120. _memoryCache.Set(key, series, TimeSpan.FromHours(CacheDurationInHours));
  121. }
  122. return series;
  123. }
  124. /// <summary>
  125. /// Gets a tv show episode group from the TMDb API based on the show id and the display order.
  126. /// </summary>
  127. /// <param name="tvShowId">The tv show's TMDb id.</param>
  128. /// <param name="displayOrder">The display order.</param>
  129. /// <param name="language">The tv show's language.</param>
  130. /// <param name="imageLanguages">A comma-separated list of image languages.</param>
  131. /// <param name="cancellationToken">The cancellation token.</param>
  132. /// <returns>The TMDb tv show episode group information or null if not found.</returns>
  133. private async Task<TvGroupCollection> GetSeriesGroupAsync(int tvShowId, string displayOrder, string language, string imageLanguages, CancellationToken cancellationToken)
  134. {
  135. TvGroupType? groupType =
  136. string.Equals(displayOrder, "absolute", StringComparison.Ordinal) ? TvGroupType.Absolute :
  137. string.Equals(displayOrder, "dvd", StringComparison.Ordinal) ? TvGroupType.DVD :
  138. null;
  139. if (groupType == null)
  140. {
  141. return null;
  142. }
  143. var key = $"group-{tvShowId.ToString(CultureInfo.InvariantCulture)}-{displayOrder}-{language}";
  144. if (_memoryCache.TryGetValue(key, out TvGroupCollection group))
  145. {
  146. return group;
  147. }
  148. await EnsureClientConfigAsync().ConfigureAwait(false);
  149. var series = await GetSeriesAsync(tvShowId, language, imageLanguages, cancellationToken).ConfigureAwait(false);
  150. var episodeGroupId = series?.EpisodeGroups.Results.Find(g => g.Type == groupType)?.Id;
  151. if (episodeGroupId == null)
  152. {
  153. return null;
  154. }
  155. group = await _tmDbClient.GetTvEpisodeGroupsAsync(
  156. episodeGroupId,
  157. language: TmdbUtils.NormalizeLanguage(language),
  158. cancellationToken: cancellationToken).ConfigureAwait(false);
  159. if (group != null)
  160. {
  161. _memoryCache.Set(key, group, TimeSpan.FromHours(CacheDurationInHours));
  162. }
  163. return group;
  164. }
  165. /// <summary>
  166. /// Gets a tv season from the TMDb API based on the tv show's TMDb id.
  167. /// </summary>
  168. /// <param name="tvShowId">The tv season's TMDb id.</param>
  169. /// <param name="seasonNumber">The season number.</param>
  170. /// <param name="language">The tv season's language.</param>
  171. /// <param name="imageLanguages">A comma-separated list of image languages.</param>
  172. /// <param name="cancellationToken">The cancellation token.</param>
  173. /// <returns>The TMDb tv season information or null if not found.</returns>
  174. public async Task<TvSeason> GetSeasonAsync(int tvShowId, int seasonNumber, string language, string imageLanguages, CancellationToken cancellationToken)
  175. {
  176. var key = $"season-{tvShowId.ToString(CultureInfo.InvariantCulture)}-s{seasonNumber.ToString(CultureInfo.InvariantCulture)}-{language}";
  177. if (_memoryCache.TryGetValue(key, out TvSeason season))
  178. {
  179. return season;
  180. }
  181. await EnsureClientConfigAsync().ConfigureAwait(false);
  182. season = await _tmDbClient.GetTvSeasonAsync(
  183. tvShowId,
  184. seasonNumber,
  185. language: TmdbUtils.NormalizeLanguage(language),
  186. includeImageLanguage: imageLanguages,
  187. extraMethods: TvSeasonMethods.Credits | TvSeasonMethods.Images | TvSeasonMethods.ExternalIds | TvSeasonMethods.Videos,
  188. cancellationToken: cancellationToken).ConfigureAwait(false);
  189. if (season != null)
  190. {
  191. _memoryCache.Set(key, season, TimeSpan.FromHours(CacheDurationInHours));
  192. }
  193. return season;
  194. }
  195. /// <summary>
  196. /// Gets a movie from the TMDb API based on the tv show's TMDb id.
  197. /// </summary>
  198. /// <param name="tvShowId">The tv show's TMDb id.</param>
  199. /// <param name="seasonNumber">The season number.</param>
  200. /// <param name="episodeNumber">The episode number.</param>
  201. /// <param name="displayOrder">The display order.</param>
  202. /// <param name="language">The episode's language.</param>
  203. /// <param name="imageLanguages">A comma-separated list of image languages.</param>
  204. /// <param name="cancellationToken">The cancellation token.</param>
  205. /// <returns>The TMDb tv episode information or null if not found.</returns>
  206. public async Task<TvEpisode> GetEpisodeAsync(int tvShowId, int seasonNumber, int episodeNumber, string displayOrder, string language, string imageLanguages, CancellationToken cancellationToken)
  207. {
  208. var key = $"episode-{tvShowId.ToString(CultureInfo.InvariantCulture)}-s{seasonNumber.ToString(CultureInfo.InvariantCulture)}e{episodeNumber.ToString(CultureInfo.InvariantCulture)}-{displayOrder}-{language}";
  209. if (_memoryCache.TryGetValue(key, out TvEpisode episode))
  210. {
  211. return episode;
  212. }
  213. await EnsureClientConfigAsync().ConfigureAwait(false);
  214. var group = await GetSeriesGroupAsync(tvShowId, displayOrder, language, imageLanguages, cancellationToken).ConfigureAwait(false);
  215. if (group != null)
  216. {
  217. var season = group.Groups.Find(s => s.Order == seasonNumber);
  218. // Episode order starts at 0
  219. var ep = season?.Episodes.Find(e => e.Order == episodeNumber - 1);
  220. if (ep != null)
  221. {
  222. seasonNumber = ep.SeasonNumber;
  223. episodeNumber = ep.EpisodeNumber;
  224. }
  225. }
  226. episode = await _tmDbClient.GetTvEpisodeAsync(
  227. tvShowId,
  228. seasonNumber,
  229. episodeNumber,
  230. language: TmdbUtils.NormalizeLanguage(language),
  231. includeImageLanguage: imageLanguages,
  232. extraMethods: TvEpisodeMethods.Credits | TvEpisodeMethods.Images | TvEpisodeMethods.ExternalIds | TvEpisodeMethods.Videos,
  233. cancellationToken: cancellationToken).ConfigureAwait(false);
  234. if (episode != null)
  235. {
  236. _memoryCache.Set(key, episode, TimeSpan.FromHours(CacheDurationInHours));
  237. }
  238. return episode;
  239. }
  240. /// <summary>
  241. /// Gets a person eg. cast or crew member from the TMDb API based on its TMDb id.
  242. /// </summary>
  243. /// <param name="personTmdbId">The person's TMDb id.</param>
  244. /// <param name="language">The episode's language.</param>
  245. /// <param name="cancellationToken">The cancellation token.</param>
  246. /// <returns>The TMDb person information or null if not found.</returns>
  247. public async Task<Person> GetPersonAsync(int personTmdbId, string language, CancellationToken cancellationToken)
  248. {
  249. var key = $"person-{personTmdbId.ToString(CultureInfo.InvariantCulture)}-{language}";
  250. if (_memoryCache.TryGetValue(key, out Person person))
  251. {
  252. return person;
  253. }
  254. await EnsureClientConfigAsync().ConfigureAwait(false);
  255. person = await _tmDbClient.GetPersonAsync(
  256. personTmdbId,
  257. TmdbUtils.NormalizeLanguage(language),
  258. PersonMethods.TvCredits | PersonMethods.MovieCredits | PersonMethods.Images | PersonMethods.ExternalIds,
  259. cancellationToken).ConfigureAwait(false);
  260. if (person != null)
  261. {
  262. _memoryCache.Set(key, person, TimeSpan.FromHours(CacheDurationInHours));
  263. }
  264. return person;
  265. }
  266. /// <summary>
  267. /// Gets an item from the TMDb API based on its id from an external service eg. IMDb id, TvDb id.
  268. /// </summary>
  269. /// <param name="externalId">The item's external id.</param>
  270. /// <param name="source">The source of the id eg. IMDb.</param>
  271. /// <param name="language">The item's language.</param>
  272. /// <param name="cancellationToken">The cancellation token.</param>
  273. /// <returns>The TMDb item or null if not found.</returns>
  274. public async Task<FindContainer> FindByExternalIdAsync(
  275. string externalId,
  276. FindExternalSource source,
  277. string language,
  278. CancellationToken cancellationToken)
  279. {
  280. var key = $"find-{source.ToString()}-{externalId.ToString(CultureInfo.InvariantCulture)}-{language}";
  281. if (_memoryCache.TryGetValue(key, out FindContainer result))
  282. {
  283. return result;
  284. }
  285. await EnsureClientConfigAsync().ConfigureAwait(false);
  286. result = await _tmDbClient.FindAsync(
  287. source,
  288. externalId,
  289. TmdbUtils.NormalizeLanguage(language),
  290. cancellationToken).ConfigureAwait(false);
  291. if (result != null)
  292. {
  293. _memoryCache.Set(key, result, TimeSpan.FromHours(CacheDurationInHours));
  294. }
  295. return result;
  296. }
  297. /// <summary>
  298. /// Searches for a tv show using the TMDb API based on its name.
  299. /// </summary>
  300. /// <param name="name">The name of the tv show.</param>
  301. /// <param name="language">The tv show's language.</param>
  302. /// <param name="year">The year the tv show first aired.</param>
  303. /// <param name="cancellationToken">The cancellation token.</param>
  304. /// <returns>The TMDb tv show information.</returns>
  305. public async Task<IReadOnlyList<SearchTv>> SearchSeriesAsync(string name, string language, int year = 0, CancellationToken cancellationToken = default)
  306. {
  307. var key = $"searchseries-{name}-{language}";
  308. if (_memoryCache.TryGetValue(key, out SearchContainer<SearchTv> series))
  309. {
  310. return series.Results;
  311. }
  312. await EnsureClientConfigAsync().ConfigureAwait(false);
  313. var searchResults = await _tmDbClient
  314. .SearchTvShowAsync(name, TmdbUtils.NormalizeLanguage(language), includeAdult: Plugin.Instance.Configuration.IncludeAdult, firstAirDateYear: year, cancellationToken: cancellationToken)
  315. .ConfigureAwait(false);
  316. if (searchResults.Results.Count > 0)
  317. {
  318. _memoryCache.Set(key, searchResults, TimeSpan.FromHours(CacheDurationInHours));
  319. }
  320. return searchResults.Results;
  321. }
  322. /// <summary>
  323. /// Searches for a person based on their name using the TMDb API.
  324. /// </summary>
  325. /// <param name="name">The name of the person.</param>
  326. /// <param name="cancellationToken">The cancellation token.</param>
  327. /// <returns>The TMDb person information.</returns>
  328. public async Task<IReadOnlyList<SearchPerson>> SearchPersonAsync(string name, CancellationToken cancellationToken)
  329. {
  330. var key = $"searchperson-{name}";
  331. if (_memoryCache.TryGetValue(key, out SearchContainer<SearchPerson> person))
  332. {
  333. return person.Results;
  334. }
  335. await EnsureClientConfigAsync().ConfigureAwait(false);
  336. var searchResults = await _tmDbClient
  337. .SearchPersonAsync(name, includeAdult: Plugin.Instance.Configuration.IncludeAdult, cancellationToken: cancellationToken)
  338. .ConfigureAwait(false);
  339. if (searchResults.Results.Count > 0)
  340. {
  341. _memoryCache.Set(key, searchResults, TimeSpan.FromHours(CacheDurationInHours));
  342. }
  343. return searchResults.Results;
  344. }
  345. /// <summary>
  346. /// Searches for a movie based on its name using the TMDb API.
  347. /// </summary>
  348. /// <param name="name">The name of the movie.</param>
  349. /// <param name="language">The movie's language.</param>
  350. /// <param name="cancellationToken">The cancellation token.</param>
  351. /// <returns>The TMDb movie information.</returns>
  352. public Task<IReadOnlyList<SearchMovie>> SearchMovieAsync(string name, string language, CancellationToken cancellationToken)
  353. {
  354. return SearchMovieAsync(name, 0, language, cancellationToken);
  355. }
  356. /// <summary>
  357. /// Searches for a movie based on its name using the TMDb API.
  358. /// </summary>
  359. /// <param name="name">The name of the movie.</param>
  360. /// <param name="year">The release year of the movie.</param>
  361. /// <param name="language">The movie's language.</param>
  362. /// <param name="cancellationToken">The cancellation token.</param>
  363. /// <returns>The TMDb movie information.</returns>
  364. public async Task<IReadOnlyList<SearchMovie>> SearchMovieAsync(string name, int year, string language, CancellationToken cancellationToken)
  365. {
  366. var key = $"moviesearch-{name}-{year.ToString(CultureInfo.InvariantCulture)}-{language}";
  367. if (_memoryCache.TryGetValue(key, out SearchContainer<SearchMovie> movies))
  368. {
  369. return movies.Results;
  370. }
  371. await EnsureClientConfigAsync().ConfigureAwait(false);
  372. var searchResults = await _tmDbClient
  373. .SearchMovieAsync(name, TmdbUtils.NormalizeLanguage(language), includeAdult: Plugin.Instance.Configuration.IncludeAdult, year: year, cancellationToken: cancellationToken)
  374. .ConfigureAwait(false);
  375. if (searchResults.Results.Count > 0)
  376. {
  377. _memoryCache.Set(key, searchResults, TimeSpan.FromHours(CacheDurationInHours));
  378. }
  379. return searchResults.Results;
  380. }
  381. /// <summary>
  382. /// Searches for a collection based on its name using the TMDb API.
  383. /// </summary>
  384. /// <param name="name">The name of the collection.</param>
  385. /// <param name="language">The collection's language.</param>
  386. /// <param name="cancellationToken">The cancellation token.</param>
  387. /// <returns>The TMDb collection information.</returns>
  388. public async Task<IReadOnlyList<SearchCollection>> SearchCollectionAsync(string name, string language, CancellationToken cancellationToken)
  389. {
  390. var key = $"collectionsearch-{name}-{language}";
  391. if (_memoryCache.TryGetValue(key, out SearchContainer<SearchCollection> collections))
  392. {
  393. return collections.Results;
  394. }
  395. await EnsureClientConfigAsync().ConfigureAwait(false);
  396. var searchResults = await _tmDbClient
  397. .SearchCollectionAsync(name, TmdbUtils.NormalizeLanguage(language), cancellationToken: cancellationToken)
  398. .ConfigureAwait(false);
  399. if (searchResults.Results.Count > 0)
  400. {
  401. _memoryCache.Set(key, searchResults, TimeSpan.FromHours(CacheDurationInHours));
  402. }
  403. return searchResults.Results;
  404. }
  405. /// <summary>
  406. /// Handles bad path checking and builds the absolute url.
  407. /// </summary>
  408. /// <param name="size">The image size to fetch.</param>
  409. /// <param name="path">The relative URL of the image.</param>
  410. /// <returns>The absolute URL.</returns>
  411. private string GetUrl(string size, string path)
  412. {
  413. if (string.IsNullOrEmpty(path))
  414. {
  415. return null;
  416. }
  417. return _tmDbClient.GetImageUrl(size, path).ToString();
  418. }
  419. /// <summary>
  420. /// Gets the absolute URL of the poster.
  421. /// </summary>
  422. /// <param name="posterPath">The relative URL of the poster.</param>
  423. /// <returns>The absolute URL.</returns>
  424. public string GetPosterUrl(string posterPath)
  425. {
  426. return GetUrl(_tmDbClient.Config.Images.PosterSizes[^1], posterPath);
  427. }
  428. /// <summary>
  429. /// Gets the absolute URL of the profile image.
  430. /// </summary>
  431. /// <param name="actorProfilePath">The relative URL of the profile image.</param>
  432. /// <returns>The absolute URL.</returns>
  433. public string GetProfileUrl(string actorProfilePath)
  434. {
  435. return GetUrl(_tmDbClient.Config.Images.ProfileSizes[^1], actorProfilePath);
  436. }
  437. /// <summary>
  438. /// Converts poster <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
  439. /// </summary>
  440. /// <param name="images">The input images.</param>
  441. /// <param name="requestLanguage">The requested language.</param>
  442. /// <param name="results">The collection to add the remote images into.</param>
  443. public void ConvertPostersToRemoteImageInfo(List<ImageData> images, string requestLanguage, List<RemoteImageInfo> results)
  444. {
  445. ConvertToRemoteImageInfo(images, _tmDbClient.Config.Images.PosterSizes[^1], ImageType.Primary, requestLanguage, results);
  446. }
  447. /// <summary>
  448. /// Converts backdrop <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 ConvertBackdropsToRemoteImageInfo(List<ImageData> images, string requestLanguage, List<RemoteImageInfo> results)
  454. {
  455. ConvertToRemoteImageInfo(images, _tmDbClient.Config.Images.BackdropSizes[^1], ImageType.Backdrop, requestLanguage, results);
  456. }
  457. /// <summary>
  458. /// Converts profile <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 ConvertProfilesToRemoteImageInfo(List<ImageData> images, string requestLanguage, List<RemoteImageInfo> results)
  464. {
  465. ConvertToRemoteImageInfo(images, _tmDbClient.Config.Images.ProfileSizes[^1], ImageType.Primary, requestLanguage, results);
  466. }
  467. /// <summary>
  468. /// Converts still <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 ConvertStillsToRemoteImageInfo(List<ImageData> images, string requestLanguage, List<RemoteImageInfo> results)
  474. {
  475. ConvertToRemoteImageInfo(images, _tmDbClient.Config.Images.StillSizes[^1], ImageType.Primary, requestLanguage, results);
  476. }
  477. /// <summary>
  478. /// Converts <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
  479. /// </summary>
  480. /// <param name="images">The input images.</param>
  481. /// <param name="size">The size of the image to fetch.</param>
  482. /// <param name="type">The type of the image.</param>
  483. /// <param name="requestLanguage">The requested language.</param>
  484. /// <param name="results">The collection to add the remote images into.</param>
  485. private void ConvertToRemoteImageInfo(List<ImageData> images, string size, ImageType type, string requestLanguage, List<RemoteImageInfo> results)
  486. {
  487. for (var i = 0; i < images.Count; i++)
  488. {
  489. var image = images[i];
  490. results.Add(new RemoteImageInfo
  491. {
  492. Url = GetUrl(size, image.FilePath),
  493. CommunityRating = image.VoteAverage,
  494. VoteCount = image.VoteCount,
  495. Width = image.Width,
  496. Height = image.Height,
  497. Language = TmdbUtils.AdjustImageLanguage(image.Iso_639_1, requestLanguage),
  498. ProviderName = TmdbUtils.ProviderName,
  499. Type = type,
  500. RatingType = RatingType.Score
  501. });
  502. }
  503. }
  504. private Task EnsureClientConfigAsync()
  505. {
  506. return !_tmDbClient.HasConfig ? _tmDbClient.GetConfigAsync() : Task.CompletedTask;
  507. }
  508. /// <inheritdoc />
  509. public void Dispose()
  510. {
  511. Dispose(true);
  512. GC.SuppressFinalize(this);
  513. }
  514. /// <summary>
  515. /// Releases unmanaged and - optionally - managed resources.
  516. /// </summary>
  517. /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  518. protected virtual void Dispose(bool disposing)
  519. {
  520. if (disposing)
  521. {
  522. _memoryCache?.Dispose();
  523. _tmDbClient?.Dispose();
  524. }
  525. }
  526. }
  527. }