< Summary - Jellyfin

Information
Class: MediaBrowser.Providers.Plugins.Tmdb.TmdbClientManager
Assembly: MediaBrowser.Providers
File(s): /srv/git/jellyfin/MediaBrowser.Providers/Plugins/Tmdb/TmdbClientManager.cs
Line coverage
4%
Covered lines: 13
Uncovered lines: 302
Coverable lines: 315
Total lines: 842
Line coverage: 4.1%
Branch coverage
1%
Covered branches: 3
Total branches: 210
Branch coverage: 1.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 5/27/2026 - 12:15:38 AM Line coverage: 4.7% (13/276) Branch coverage: 2% (4/192) Total lines: 7668/3/2026 - 12:16:46 AM Line coverage: 4.6% (13/277) Branch coverage: 2% (4/192) Total lines: 77610/29/2026 - 12:17:11 AM Line coverage: 4.1% (13/315) Branch coverage: 1.4% (3/210) Total lines: 842 5/27/2026 - 12:15:38 AM Line coverage: 4.7% (13/276) Branch coverage: 2% (4/192) Total lines: 7668/3/2026 - 12:16:46 AM Line coverage: 4.6% (13/277) Branch coverage: 2% (4/192) Total lines: 77610/29/2026 - 12:17:11 AM Line coverage: 4.1% (13/315) Branch coverage: 1.4% (3/210) Total lines: 842

Coverage delta

Coverage delta 1 -1

Metrics

File(s)

/srv/git/jellyfin/MediaBrowser.Providers/Plugins/Tmdb/TmdbClientManager.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Globalization;
 4using System.Threading;
 5using System.Threading.Tasks;
 6using MediaBrowser.Model.Dto;
 7using MediaBrowser.Model.Entities;
 8using MediaBrowser.Model.Providers;
 9using Microsoft.Extensions.Caching.Memory;
 10using TMDbLib.Client;
 11using TMDbLib.Objects.Collections;
 12using TMDbLib.Objects.Find;
 13using TMDbLib.Objects.General;
 14using TMDbLib.Objects.Movies;
 15using TMDbLib.Objects.People;
 16using TMDbLib.Objects.Search;
 17using TMDbLib.Objects.TvShows;
 18
 19namespace 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
 28        // Sized in TMDb records - see EstimateSize - rather than in responses, because the responses
 29        // differ in weight by orders of magnitude.
 30        private const int CacheSizeLimit = 100_000;
 31
 032        private static readonly Dictionary<string, string> ThumbnailSizes = new Dictionary<string, string>
 033        {
 034            { "Primary", "w500" },
 035            { "Backdrop", "w780" },
 036            { "Thumb", "w780" },
 037            { "Logo", "w500" },
 038        };
 39
 40        private readonly MemoryCache _memoryCache;
 41        private readonly TMDbClient _tmDbClient;
 42
 43        /// <summary>
 44        /// Initializes a new instance of the <see cref="TmdbClientManager"/> class.
 45        /// </summary>
 46        public TmdbClientManager()
 47        {
 2748            _memoryCache = new MemoryCache(new MemoryCacheOptions { SizeLimit = CacheSizeLimit });
 49
 2750            var apiKey = Plugin.Instance.Configuration.TmdbApiKey;
 2751            apiKey = string.IsNullOrEmpty(apiKey) ? TmdbUtils.ApiKey : apiKey;
 2752            _tmDbClient = new TMDbClient(apiKey);
 53
 54            // Not really interested in NotFoundException
 2755            _tmDbClient.ThrowApiExceptions = false;
 2756        }
 57
 58        /// <summary>
 59        /// Gets a movie from the TMDb API based on its TMDb id.
 60        /// </summary>
 61        /// <param name="tmdbId">The movie's TMDb id.</param>
 62        /// <param name="language">The movie's language.</param>
 63        /// <param name="imageLanguages">A comma-separated list of image languages.</param>
 64        /// <param name="countryCode">The country code, ISO 3166-1.</param>
 65        /// <param name="cancellationToken">The cancellation token.</param>
 66        /// <returns>The TMDb movie or null if not found.</returns>
 67        public async Task<Movie?> GetMovieAsync(int tmdbId, string? language, string? imageLanguages, string? countryCod
 68        {
 069            var key = $"movie-{tmdbId.ToString(CultureInfo.InvariantCulture)}-{language}";
 070            if (_memoryCache.TryGetValue(key, out Movie? movie))
 71            {
 072                return movie;
 73            }
 74
 075            await EnsureClientConfigAsync().ConfigureAwait(false);
 76
 077            var extraMethods = MovieMethods.Credits | MovieMethods.Releases | MovieMethods.Images | MovieMethods.Videos;
 078            if (!(Plugin.Instance?.Configuration.ExcludeTagsMovies).GetValueOrDefault())
 79            {
 080                extraMethods |= MovieMethods.Keywords;
 81            }
 82
 083            movie = await _tmDbClient.GetMovieAsync(
 084                tmdbId,
 085                TmdbUtils.NormalizeLanguage(language, countryCode),
 086                imageLanguages,
 087                extraMethods,
 088                cancellationToken).ConfigureAwait(false);
 89
 090            if (movie is not null)
 91            {
 092                Cache(key, movie);
 93            }
 94
 095            return movie;
 096        }
 97
 98        /// <summary>
 99        /// Gets a collection from the TMDb API based on its TMDb id.
 100        /// </summary>
 101        /// <param name="tmdbId">The collection's TMDb id.</param>
 102        /// <param name="language">The collection's language.</param>
 103        /// <param name="imageLanguages">A comma-separated list of image languages.</param>
 104        /// <param name="countryCode">The country code, ISO 3166-1.</param>
 105        /// <param name="cancellationToken">The cancellation token.</param>
 106        /// <returns>The TMDb collection or null if not found.</returns>
 107        public async Task<Collection?> GetCollectionAsync(int tmdbId, string? language, string? imageLanguages, string? 
 108        {
 0109            var key = $"collection-{tmdbId.ToString(CultureInfo.InvariantCulture)}-{language}";
 0110            if (_memoryCache.TryGetValue(key, out Collection? collection))
 111            {
 0112                return collection;
 113            }
 114
 0115            await EnsureClientConfigAsync().ConfigureAwait(false);
 116
 0117            collection = await _tmDbClient.GetCollectionAsync(
 0118                tmdbId,
 0119                TmdbUtils.NormalizeLanguage(language, countryCode),
 0120                imageLanguages,
 0121                CollectionMethods.Images,
 0122                cancellationToken).ConfigureAwait(false);
 123
 0124            if (collection is not null)
 125            {
 0126                Cache(key, collection);
 127            }
 128
 0129            return collection;
 0130        }
 131
 132        /// <summary>
 133        /// Gets a tv show from the TMDb API based on its TMDb id.
 134        /// </summary>
 135        /// <param name="tmdbId">The tv show's TMDb id.</param>
 136        /// <param name="language">The tv show's language.</param>
 137        /// <param name="imageLanguages">A comma-separated list of image languages.</param>
 138        /// <param name="countryCode">The country code, ISO 3166-1.</param>
 139        /// <param name="cancellationToken">The cancellation token.</param>
 140        /// <returns>The TMDb tv show information or null if not found.</returns>
 141        public async Task<TvShow?> GetSeriesAsync(int tmdbId, string? language, string? imageLanguages, string? countryC
 142        {
 0143            var key = $"series-{tmdbId.ToString(CultureInfo.InvariantCulture)}-{language}";
 0144            if (_memoryCache.TryGetValue(key, out TvShow? series))
 145            {
 0146                return series;
 147            }
 148
 0149            await EnsureClientConfigAsync().ConfigureAwait(false);
 150
 0151            var extraMethods = TvShowMethods.Credits | TvShowMethods.CreditsAggregate | TvShowMethods.Images | TvShowMet
 0152            if (!(Plugin.Instance?.Configuration.ExcludeTagsSeries).GetValueOrDefault())
 153            {
 0154                extraMethods |= TvShowMethods.Keywords;
 155            }
 156
 0157            series = await _tmDbClient.GetTvShowAsync(
 0158                tmdbId,
 0159                language: TmdbUtils.NormalizeLanguage(language, countryCode),
 0160                includeImageLanguage: imageLanguages,
 0161                extraMethods: extraMethods,
 0162                cancellationToken: cancellationToken).ConfigureAwait(false);
 163
 0164            if (series is not null)
 165            {
 0166                Cache(key, series);
 167            }
 168
 0169            return series;
 0170        }
 171
 172        /// <summary>
 173        /// Gets a tv show episode group from the TMDb API based on the episode group.
 174        /// </summary>
 175        /// <param name="episodeGroupId">The Tmdb episode group.</param>
 176        /// <param name="seasonNumber">The season number of the episode group.</param>
 177        /// <param name="language">The tv show's language.</param>
 178        /// <param name="imageLanguages">A comma-separated list of image languages.</param>
 179        /// <param name="countryCode">The country code, ISO 3166-1.</param>
 180        /// <param name="cancellationToken">The cancellation token.</param>
 181        /// <returns>The TMDb tv show episode group information or null if not found.</returns>
 182        public async Task<TvGroup?> GetSeriesGroupAsync(string episodeGroupId, int seasonNumber, string? language, strin
 183        {
 0184            var key = $"group-{episodeGroupId}-{seasonNumber.ToString(CultureInfo.InvariantCulture)}-{language}";
 0185            if (_memoryCache.TryGetValue(key, out TvGroup? group))
 186            {
 0187                return group;
 188            }
 189
 0190            await EnsureClientConfigAsync().ConfigureAwait(false);
 191
 0192            var groups = await _tmDbClient.GetTvEpisodeGroupsAsync(
 0193                episodeGroupId,
 0194                language: TmdbUtils.NormalizeLanguage(language, countryCode),
 0195                cancellationToken: cancellationToken).ConfigureAwait(false);
 196
 0197            group = groups?.Groups?.Find(s => s.Order == seasonNumber);
 0198            if (group is not null)
 199            {
 0200                Cache(key, group);
 201            }
 202
 0203            return group;
 0204        }
 205
 206        /// <summary>
 207        /// Gets a tv season from the TMDb API based on the tv show's TMDb id.
 208        /// </summary>
 209        /// <param name="tvShowId">The tv season's TMDb id.</param>
 210        /// <param name="seasonNumber">The season number.</param>
 211        /// <param name="language">The tv season's language.</param>
 212        /// <param name="imageLanguages">A comma-separated list of image languages.</param>
 213        /// <param name="countryCode">The country code, ISO 3166-1.</param>
 214        /// <param name="cancellationToken">The cancellation token.</param>
 215        /// <returns>The TMDb tv season information or null if not found.</returns>
 216        public async Task<TvSeason?> GetSeasonAsync(int tvShowId, int seasonNumber, string? language, string? imageLangu
 217        {
 0218            var key = $"season-{tvShowId.ToString(CultureInfo.InvariantCulture)}-s{seasonNumber.ToString(CultureInfo.Inv
 0219            if (_memoryCache.TryGetValue(key, out TvSeason? season))
 220            {
 0221                return season;
 222            }
 223
 0224            await EnsureClientConfigAsync().ConfigureAwait(false);
 225
 0226            season = await _tmDbClient.GetTvSeasonAsync(
 0227                tvShowId,
 0228                seasonNumber,
 0229                language: TmdbUtils.NormalizeLanguage(language, countryCode),
 0230                includeImageLanguage: imageLanguages,
 0231                extraMethods: TvSeasonMethods.Credits | TvSeasonMethods.Images | TvSeasonMethods.ExternalIds | TvSeasonM
 0232                cancellationToken: cancellationToken).ConfigureAwait(false);
 233
 0234            if (season is not null)
 235            {
 0236                Cache(key, season);
 237            }
 238
 0239            return season;
 0240        }
 241
 242        /// <summary>
 243        /// Gets an episode from the TMDb API based on the tv show's TMDb id.
 244        /// </summary>
 245        /// <param name="tvShowId">The tv show's TMDb id.</param>
 246        /// <param name="seasonNumber">The season number.</param>
 247        /// <param name="episodeNumber">The episode number.</param>
 248        /// <param name="episodeGroup">The containing episode group.</param>
 249        /// <param name="language">The episode's language.</param>
 250        /// <param name="imageLanguages">A comma-separated list of image languages.</param>
 251        /// <param name="countryCode">The country code, ISO 3166-1.</param>
 252        /// <param name="cancellationToken">The cancellation token.</param>
 253        /// <returns>The TMDb tv episode information or null if not found.</returns>
 254        public async Task<TvEpisode?> GetEpisodeAsync(int tvShowId, int seasonNumber, long episodeNumber, string? episod
 255        {
 0256            var key = $"episode-{tvShowId.ToString(CultureInfo.InvariantCulture)}-s{seasonNumber.ToString(CultureInfo.In
 0257            if (_memoryCache.TryGetValue(key, out TvEpisode? episode))
 258            {
 0259                return episode;
 260            }
 261
 0262            await EnsureClientConfigAsync().ConfigureAwait(false);
 263
 0264            if (!string.IsNullOrWhiteSpace(episodeGroup))
 265            {
 0266                var group = await GetSeriesGroupAsync(episodeGroup, seasonNumber, language, imageLanguages, countryCode,
 0267                var ep = group?.Episodes?.Find(e => e.Order == episodeNumber - 1);
 0268                if (ep is not null)
 269                {
 0270                    seasonNumber = ep.SeasonNumber;
 0271                    episodeNumber = ep.EpisodeNumber;
 272                }
 273            }
 274
 0275            episode = await _tmDbClient.GetTvEpisodeAsync(
 0276                tvShowId,
 0277                seasonNumber,
 0278                episodeNumber,
 0279                language: TmdbUtils.NormalizeLanguage(language, countryCode),
 0280                includeImageLanguage: imageLanguages,
 0281                extraMethods: TvEpisodeMethods.Credits | TvEpisodeMethods.Images | TvEpisodeMethods.ExternalIds | TvEpis
 0282                cancellationToken: cancellationToken).ConfigureAwait(false);
 283
 0284            if (episode is not null)
 285            {
 0286                Cache(key, episode);
 287            }
 288
 0289            return episode;
 0290        }
 291
 292        /// <summary>
 293        /// Gets a person eg. cast or crew member from the TMDb API based on its TMDb id.
 294        /// </summary>
 295        /// <param name="personTmdbId">The person's TMDb id.</param>
 296        /// <param name="language">The person's language.</param>
 297        /// <param name="countryCode">The country code, ISO 3166-1.</param>
 298        /// <param name="cancellationToken">The cancellation token.</param>
 299        /// <returns>The TMDb person information or null if not found.</returns>
 300        public async Task<Person?> GetPersonAsync(int personTmdbId, string language, string? countryCode, CancellationTo
 301        {
 0302            var key = $"person-{personTmdbId.ToString(CultureInfo.InvariantCulture)}-{language}";
 0303            if (_memoryCache.TryGetValue(key, out Person? person))
 304            {
 0305                return person;
 306            }
 307
 0308            await EnsureClientConfigAsync().ConfigureAwait(false);
 309
 0310            person = await _tmDbClient.GetPersonAsync(
 0311                personTmdbId,
 0312                TmdbUtils.NormalizeLanguage(language, countryCode),
 0313                PersonMethods.Images | PersonMethods.ExternalIds,
 0314                cancellationToken).ConfigureAwait(false);
 315
 0316            if (person is not null)
 317            {
 0318                Cache(key, person);
 319            }
 320
 0321            return person;
 0322        }
 323
 324        /// <summary>
 325        /// Gets an item from the TMDb API based on its id from an external service eg. IMDb id, TvDb id.
 326        /// </summary>
 327        /// <param name="externalId">The item's external id.</param>
 328        /// <param name="source">The source of the id eg. IMDb.</param>
 329        /// <param name="language">The item's language.</param>
 330        /// <param name="countryCode">The country code, ISO 3166-1.</param>
 331        /// <param name="cancellationToken">The cancellation token.</param>
 332        /// <returns>The TMDb item or null if not found.</returns>
 333        public async Task<FindContainer?> FindByExternalIdAsync(
 334            string externalId,
 335            FindExternalSource source,
 336            string language,
 337            string? countryCode,
 338            CancellationToken cancellationToken)
 339        {
 0340            var key = $"find-{source.ToString()}-{externalId.ToString(CultureInfo.InvariantCulture)}-{language}";
 0341            if (_memoryCache.TryGetValue(key, out FindContainer? result))
 342            {
 0343                return result;
 344            }
 345
 0346            await EnsureClientConfigAsync().ConfigureAwait(false);
 347
 0348            result = await _tmDbClient.FindAsync(
 0349                source,
 0350                externalId,
 0351                TmdbUtils.NormalizeLanguage(language, countryCode),
 0352                cancellationToken).ConfigureAwait(false);
 353
 0354            if (result is not null)
 355            {
 0356                Cache(key, result);
 357            }
 358
 0359            return result;
 0360        }
 361
 362        /// <summary>
 363        /// Searches for a tv show using the TMDb API based on its name.
 364        /// </summary>
 365        /// <param name="name">The name of the tv show.</param>
 366        /// <param name="language">The tv show's language.</param>
 367        /// <param name="countryCode">The country code, ISO 3166-1.</param>
 368        /// <param name="year">The year the tv show first aired.</param>
 369        /// <param name="cancellationToken">The cancellation token.</param>
 370        /// <returns>The TMDb tv show information.</returns>
 371        public async Task<IReadOnlyList<SearchTv>?> SearchSeriesAsync(string name, string language, string? countryCode,
 372        {
 0373            var key = $"searchseries-{name}-{year.ToString(CultureInfo.InvariantCulture)}-{language}";
 0374            if (_memoryCache.TryGetValue(key, out SearchContainer<SearchTv>? series) && series is not null)
 375            {
 0376                return series.Results;
 377            }
 378
 0379            await EnsureClientConfigAsync().ConfigureAwait(false);
 380
 0381            var searchResults = await _tmDbClient
 0382                .SearchTvShowAsync(name, TmdbUtils.NormalizeLanguage(language, countryCode), includeAdult: Plugin.Instan
 0383                .ConfigureAwait(false);
 384
 0385            if (searchResults?.Results?.Count > 0)
 386            {
 0387                CacheSearch(key, searchResults);
 388            }
 389
 0390            return searchResults?.Results;
 0391        }
 392
 393        /// <summary>
 394        /// Searches for a person based on their name using the TMDb API.
 395        /// </summary>
 396        /// <param name="name">The name of the person.</param>
 397        /// <param name="cancellationToken">The cancellation token.</param>
 398        /// <returns>The TMDb person information.</returns>
 399        public async Task<IReadOnlyList<SearchPerson>?> SearchPersonAsync(string name, CancellationToken cancellationTok
 400        {
 0401            var key = $"searchperson-{name}";
 0402            if (_memoryCache.TryGetValue(key, out SearchContainer<SearchPerson>? person) && person is not null)
 403            {
 0404                return person.Results;
 405            }
 406
 0407            await EnsureClientConfigAsync().ConfigureAwait(false);
 408
 0409            var searchResults = await _tmDbClient
 0410                .SearchPersonAsync(name, includeAdult: Plugin.Instance.Configuration.IncludeAdult, cancellationToken: ca
 0411                .ConfigureAwait(false);
 412
 0413            if (searchResults?.Results?.Count > 0)
 414            {
 0415                CacheSearch(key, searchResults);
 416            }
 417
 0418            return searchResults?.Results;
 0419        }
 420
 421        /// <summary>
 422        /// Searches for a movie based on its name using the TMDb API.
 423        /// </summary>
 424        /// <param name="name">The name of the movie.</param>
 425        /// <param name="language">The movie's language.</param>
 426        /// <param name="cancellationToken">The cancellation token.</param>
 427        /// <returns>The TMDb movie information.</returns>
 428        public Task<IReadOnlyList<SearchMovie>?> SearchMovieAsync(string name, string language, CancellationToken cancel
 429        {
 0430            return SearchMovieAsync(name, 0, language, null, cancellationToken);
 431        }
 432
 433        /// <summary>
 434        /// Searches for a movie based on its name using the TMDb API.
 435        /// </summary>
 436        /// <param name="name">The name of the movie.</param>
 437        /// <param name="year">The release year of the movie.</param>
 438        /// <param name="language">The movie's language.</param>
 439        /// <param name="countryCode">The country code, ISO 3166-1.</param>
 440        /// <param name="cancellationToken">The cancellation token.</param>
 441        /// <returns>The TMDb movie information.</returns>
 442        public async Task<IReadOnlyList<SearchMovie>?> SearchMovieAsync(string name, int year, string language, string? 
 443        {
 0444            var key = $"moviesearch-{name}-{year.ToString(CultureInfo.InvariantCulture)}-{language}";
 0445            if (_memoryCache.TryGetValue(key, out SearchContainer<SearchMovie>? movies) && movies is not null)
 446            {
 0447                return movies.Results;
 448            }
 449
 0450            await EnsureClientConfigAsync().ConfigureAwait(false);
 451
 0452            var searchResults = await _tmDbClient
 0453                .SearchMovieAsync(name, TmdbUtils.NormalizeLanguage(language, countryCode), includeAdult: Plugin.Instanc
 0454                .ConfigureAwait(false);
 455
 0456            if (searchResults?.Results?.Count > 0)
 457            {
 0458                CacheSearch(key, searchResults);
 459            }
 460
 0461            return searchResults?.Results;
 0462        }
 463
 464        /// <summary>
 465        /// Searches for a collection based on its name using the TMDb API.
 466        /// </summary>
 467        /// <param name="name">The name of the collection.</param>
 468        /// <param name="language">The collection's language.</param>
 469        /// <param name="countryCode">The country code, ISO 3166-1.</param>
 470        /// <param name="cancellationToken">The cancellation token.</param>
 471        /// <returns>The TMDb collection information.</returns>
 472        public async Task<IReadOnlyList<SearchCollection>?> SearchCollectionAsync(string name, string language, string? 
 473        {
 0474            var key = $"collectionsearch-{name}-{language}";
 0475            if (_memoryCache.TryGetValue(key, out SearchContainer<SearchCollection>? collections) && collections is not 
 476            {
 0477                return collections.Results;
 478            }
 479
 0480            await EnsureClientConfigAsync().ConfigureAwait(false);
 481
 0482            var searchResults = await _tmDbClient
 0483                .SearchCollectionAsync(name, TmdbUtils.NormalizeLanguage(language, countryCode), cancellationToken: canc
 0484                .ConfigureAwait(false);
 485
 0486            if (searchResults?.Results?.Count > 0)
 487            {
 0488                CacheSearch(key, searchResults);
 489            }
 490
 0491            return searchResults?.Results;
 0492        }
 493
 494        /// <summary>
 495        /// Gets a single page of recommended movies for a movie from the TMDb API.
 496        /// </summary>
 497        /// <param name="tmdbId">The TMDb id of the movie.</param>
 498        /// <param name="page">The page number to fetch (1-based).</param>
 499        /// <param name="language">The language for results.</param>
 500        /// <param name="cancellationToken">The cancellation token.</param>
 501        /// <returns>A tuple containing the list of recommended movies and the total number of pages available.</returns
 502        public async Task<(IReadOnlyList<SearchMovie> Results, int TotalPages)> GetMovieRecommendationsPageAsync(int tmd
 503        {
 0504            await EnsureClientConfigAsync().ConfigureAwait(false);
 505
 0506            var searchResults = await _tmDbClient
 0507                .GetMovieRecommendationsAsync(tmdbId, language, page, cancellationToken)
 0508                .ConfigureAwait(false);
 509
 0510            if (searchResults?.Results is null || searchResults.Results.Count == 0)
 511            {
 0512                return ([], 0);
 513            }
 514
 0515            return (searchResults.Results, searchResults.TotalPages);
 0516        }
 517
 518        /// <summary>
 519        /// Gets a single page of recommended TV shows for a series from the TMDb API.
 520        /// </summary>
 521        /// <param name="tmdbId">The TMDb id of the TV show.</param>
 522        /// <param name="page">The page number to fetch (1-based).</param>
 523        /// <param name="language">The language for results.</param>
 524        /// <param name="cancellationToken">The cancellation token.</param>
 525        /// <returns>A tuple containing the list of recommended TV shows and the total number of pages available.</retur
 526        public async Task<(IReadOnlyList<SearchTv> Results, int TotalPages)> GetSeriesRecommendationsPageAsync(int tmdbI
 527        {
 0528            await EnsureClientConfigAsync().ConfigureAwait(false);
 529
 0530            var searchResults = await _tmDbClient
 0531                .GetTvShowRecommendationsAsync(tmdbId, language, page, cancellationToken)
 0532                .ConfigureAwait(false);
 533
 0534            if (searchResults?.Results is null || searchResults.Results.Count == 0)
 535            {
 0536                return ([], 0);
 537            }
 538
 0539            return (searchResults.Results, searchResults.TotalPages);
 0540        }
 541
 542        /// <summary>
 543        /// Handles bad path checking and builds the absolute url.
 544        /// </summary>
 545        /// <param name="size">The image size to fetch.</param>
 546        /// <param name="path">The relative URL of the image.</param>
 547        /// <returns>The absolute URL.</returns>
 548        private string? GetUrl(string? size, string? path)
 549        {
 0550            if (string.IsNullOrEmpty(path))
 551            {
 0552                return null;
 553            }
 554
 555            // Use the original size as default if size is null or empty to prevent malformed URLs
 0556            var imageSize = string.IsNullOrEmpty(size) ? TmdbUtils.OriginalImageSize : size;
 557
 0558            return _tmDbClient.GetImageUrl(imageSize, path, true).ToString();
 559        }
 560
 561        /// <summary>
 562        /// Gets the absolute URL of the poster.
 563        /// </summary>
 564        /// <param name="posterPath">The relative URL of the poster.</param>
 565        /// <returns>The absolute URL.</returns>
 566        public string? GetPosterUrl(string? posterPath)
 567        {
 0568            return GetUrl(Plugin.Instance.Configuration.PosterSize, posterPath);
 569        }
 570
 571        /// <summary>
 572        /// Gets the absolute URL of the profile image.
 573        /// </summary>
 574        /// <param name="actorProfilePath">The relative URL of the profile image.</param>
 575        /// <returns>The absolute URL.</returns>
 576        public string? GetProfileUrl(string? actorProfilePath)
 577        {
 0578            return GetUrl(Plugin.Instance.Configuration.ProfileSize, actorProfilePath);
 579        }
 580
 581        /// <summary>
 582        /// Gets the absolute URL of an episode still.
 583        /// </summary>
 584        /// <param name="stillPath">The relative URL of the still.</param>
 585        /// <returns>The absolute URL.</returns>
 586        public string? GetStillUrl(string? stillPath)
 587        {
 0588            return GetUrl(Plugin.Instance.Configuration.StillSize, stillPath);
 589        }
 590
 591        /// <summary>
 592        /// Converts poster <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
 593        /// </summary>
 594        /// <param name="images">The input images.</param>
 595        /// <param name="requestLanguage">The requested language.</param>
 596        /// <returns>The remote images.</returns>
 597        public IEnumerable<RemoteImageInfo> ConvertPostersToRemoteImageInfo(IReadOnlyList<ImageData> images, string requ
 0598            => ConvertToRemoteImageInfo(images, Plugin.Instance.Configuration.PosterSize, ImageType.Primary, requestLang
 599
 600        /// <summary>
 601        /// Converts backdrop <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
 602        /// </summary>
 603        /// <param name="images">The input images.</param>
 604        /// <param name="requestLanguage">The requested language.</param>
 605        /// <returns>The remote images.</returns>
 606        public IEnumerable<RemoteImageInfo> ConvertBackdropsToRemoteImageInfo(IReadOnlyList<ImageData> images, string re
 0607            => ConvertToRemoteImageInfo(images, Plugin.Instance.Configuration.BackdropSize, ImageType.Backdrop, requestL
 608
 609        /// <summary>
 610        /// Converts logo <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
 611        /// </summary>
 612        /// <param name="images">The input images.</param>
 613        /// <param name="requestLanguage">The requested language.</param>
 614        /// <returns>The remote images.</returns>
 615        public IEnumerable<RemoteImageInfo> ConvertLogosToRemoteImageInfo(IReadOnlyList<ImageData> images, string reques
 0616            => ConvertToRemoteImageInfo(images, Plugin.Instance.Configuration.LogoSize, ImageType.Logo, requestLanguage)
 617
 618        /// <summary>
 619        /// Converts profile <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
 620        /// </summary>
 621        /// <param name="images">The input images.</param>
 622        /// <param name="requestLanguage">The requested language.</param>
 623        /// <returns>The remote images.</returns>
 624        public IEnumerable<RemoteImageInfo> ConvertProfilesToRemoteImageInfo(IReadOnlyList<ImageData> images, string req
 0625            => ConvertToRemoteImageInfo(images, Plugin.Instance.Configuration.ProfileSize, ImageType.Primary, requestLan
 626
 627        /// <summary>
 628        /// Converts still <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
 629        /// </summary>
 630        /// <param name="images">The input images.</param>
 631        /// <param name="requestLanguage">The requested language.</param>
 632        /// <returns>The remote images.</returns>
 633        public IEnumerable<RemoteImageInfo> ConvertStillsToRemoteImageInfo(IReadOnlyList<ImageData> images, string reque
 0634            => ConvertToRemoteImageInfo(images, Plugin.Instance.Configuration.StillSize, ImageType.Primary, requestLangu
 635
 636        /// <summary>
 637        /// Converts <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
 638        /// </summary>
 639        /// <param name="images">The input images.</param>
 640        /// <param name="size">The size of the image to fetch.</param>
 641        /// <param name="type">The type of the image.</param>
 642        /// <param name="requestLanguage">The requested language.</param>
 643        /// <returns>The remote images.</returns>
 644        private IEnumerable<RemoteImageInfo> ConvertToRemoteImageInfo(IReadOnlyList<ImageData> images, string? size, Ima
 645        {
 646            // sizes provided are for original resolution, don't store them when downloading scaled images
 0647            var scaleImage = !TmdbUtils.IsOriginalImageSize(size);
 648
 0649            for (var i = 0; i < images.Count; i++)
 650            {
 0651                var image = images[i];
 652
 0653                var imageType = type;
 0654                var language = TmdbUtils.GetImageLanguage(image.Iso_639_1, image.Iso_3166_1, requestLanguage);
 655
 656                // Return Backdrops with a language specified (it has text) as Thumb.
 0657                if (imageType == ImageType.Backdrop && !string.IsNullOrEmpty(language))
 658                {
 0659                    imageType = ImageType.Thumb;
 660                }
 661
 0662                yield return new RemoteImageInfo
 0663                {
 0664                    Url = GetUrl(size, image.FilePath),
 0665                    ThumbnailUrl = GetUrl(ThumbnailSizes.GetValueOrDefault(type.ToString(), string.Empty), image.FilePat
 0666                    CommunityRating = image.VoteAverage,
 0667                    VoteCount = image.VoteCount,
 0668                    Width = scaleImage ? null : image.Width,
 0669                    Height = scaleImage ? null : image.Height,
 0670                    Language = language,
 0671                    ProviderName = TmdbUtils.ProviderName,
 0672                    Type = imageType,
 0673                    RatingType = RatingType.Score
 0674                };
 675            }
 0676        }
 677
 678        private async Task EnsureClientConfigAsync()
 679        {
 0680            if (!_tmDbClient.HasConfig)
 681            {
 0682                var config = await _tmDbClient.GetConfigAsync().ConfigureAwait(false);
 0683                ValidatePreferences(config);
 684            }
 0685        }
 686
 687        private static void ValidatePreferences(TMDbConfig config)
 688        {
 0689            var imageConfig = config.Images;
 0690            if (imageConfig is null)
 691            {
 0692                return;
 693            }
 694
 0695            var pluginConfig = Plugin.Instance.Configuration;
 696
 0697            if (imageConfig.PosterSizes is not null
 0698                && pluginConfig.PosterSize is not null
 0699                && !imageConfig.PosterSizes.Contains(pluginConfig.PosterSize))
 700            {
 0701                pluginConfig.PosterSize = imageConfig.PosterSizes[^1];
 702            }
 703
 0704            if (imageConfig.BackdropSizes is not null
 0705                && pluginConfig.BackdropSize is not null
 0706                && !imageConfig.BackdropSizes.Contains(pluginConfig.BackdropSize))
 707            {
 0708                pluginConfig.BackdropSize = imageConfig.BackdropSizes[^1];
 709            }
 710
 0711            if (imageConfig.LogoSizes is not null
 0712                && pluginConfig.LogoSize is not null
 0713                && !imageConfig.LogoSizes.Contains(pluginConfig.LogoSize))
 714            {
 0715                pluginConfig.LogoSize = imageConfig.LogoSizes[^1];
 716            }
 717
 0718            if (imageConfig.ProfileSizes is not null
 0719                && pluginConfig.ProfileSize is not null
 0720                && !imageConfig.ProfileSizes.Contains(pluginConfig.ProfileSize))
 721            {
 0722                pluginConfig.ProfileSize = imageConfig.ProfileSizes[^1];
 723            }
 724
 0725            if (imageConfig.StillSizes is not null
 0726                && pluginConfig.StillSize is not null
 0727                && !imageConfig.StillSizes.Contains(pluginConfig.StillSize))
 728            {
 0729                pluginConfig.StillSize = imageConfig.StillSizes[^1];
 730            }
 0731        }
 732
 733        /// <summary>
 734        /// Gets the <see cref="TMDbClient"/> configuration.
 735        /// </summary>
 736        /// <returns>The configuration.</returns>
 737        public async Task<TMDbConfig> GetClientConfiguration()
 738        {
 0739            await EnsureClientConfigAsync().ConfigureAwait(false);
 740
 0741            return _tmDbClient.Config;
 0742        }
 743
 744        /// <summary>
 745        /// Stores a response under the shared expiry, weighed by what it costs to keep.
 746        /// </summary>
 747        private void Cache<T>(string key, T value)
 748            where T : class
 0749            => _memoryCache.Set(
 0750                key,
 0751                value,
 0752                new MemoryCacheEntryOptions
 0753                {
 0754                    AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(CacheDurationInHours),
 0755                    Size = EstimateSize(value)
 0756                });
 757
 758        /// <summary>
 759        /// Stores a page of search results, whose weight is simply how many there are.
 760        /// </summary>
 761        private void CacheSearch<T>(string key, SearchContainer<T> results)
 0762            => _memoryCache.Set(
 0763                key,
 0764                results,
 0765                new MemoryCacheEntryOptions
 0766                {
 0767                    AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(CacheDurationInHours),
 0768                    Size = 1 + Count(results.Results)
 0769                });
 770
 0771        private static long Count<T>(IReadOnlyCollection<T>? items) => items?.Count ?? 0;
 772
 773        /// <summary>
 774        /// Estimates what keeping a response costs, counting the sub-records that dominate it.
 775        /// </summary>
 0776        private static long EstimateSize(object? value) => value switch
 0777        {
 0778            TvShow series => 1
 0779                + Count(series.Credits?.Cast) + Count(series.Credits?.Crew)
 0780                + EstimateAggregateSize(series.AggregateCredits)
 0781                + Count(series.Seasons),
 0782            TvSeason season => 1
 0783                + Count(season.Credits?.Cast) + Count(season.Credits?.Crew)
 0784                + Count(season.Episodes),
 0785            TvEpisode episode => 1
 0786                + Count(episode.Credits?.Cast) + Count(episode.Credits?.Crew)
 0787                + Count(episode.Credits?.GuestStars),
 0788            Movie movie => 1 + Count(movie.Credits?.Cast) + Count(movie.Credits?.Crew),
 0789            Collection collection => 1 + Count(collection.Parts),
 0790            TvGroupCollection groups => 1 + Count(groups.Groups),
 0791            FindContainer found => 1
 0792                + Count(found.MovieResults) + Count(found.TvResults)
 0793                + Count(found.PersonResults) + Count(found.TvEpisode) + Count(found.TvSeason),
 0794            _ => 1
 0795        };
 796
 797        /// <summary>
 798        /// Weighs aggregate credits, where each person carries one record per episode they worked on.
 799        /// </summary>
 800        private static long EstimateAggregateSize(CreditsAggregate? credits)
 801        {
 0802            if (credits is null)
 803            {
 0804                return 0;
 805            }
 806
 0807            var size = Count(credits.Cast) + Count(credits.Crew);
 808
 0809            foreach (var cast in credits.Cast ?? [])
 810            {
 0811                size += Count(cast.Roles);
 812            }
 813
 0814            foreach (var crew in credits.Crew ?? [])
 815            {
 0816                size += Count(crew.Jobs);
 817            }
 818
 0819            return size;
 820        }
 821
 822        /// <inheritdoc />
 823        public void Dispose()
 824        {
 27825            Dispose(true);
 27826            GC.SuppressFinalize(this);
 27827        }
 828
 829        /// <summary>
 830        /// Releases unmanaged and - optionally - managed resources.
 831        /// </summary>
 832        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release
 833        protected virtual void Dispose(bool disposing)
 834        {
 27835            if (disposing)
 836            {
 27837                _memoryCache.Dispose();
 27838                _tmDbClient?.Dispose();
 839            }
 27840        }
 841    }
 842}

Methods/Properties

.cctor()
.ctor()
GetMovieAsync()
GetCollectionAsync()
GetSeriesAsync()
GetSeriesGroupAsync()
GetSeasonAsync()
GetEpisodeAsync()
GetPersonAsync()
FindByExternalIdAsync()
SearchSeriesAsync()
SearchPersonAsync()
SearchMovieAsync(System.String,System.String,System.Threading.CancellationToken)
SearchMovieAsync()
SearchCollectionAsync()
GetMovieRecommendationsPageAsync()
GetSeriesRecommendationsPageAsync()
GetUrl(System.String,System.String)
GetPosterUrl(System.String)
GetProfileUrl(System.String)
GetStillUrl(System.String)
ConvertPostersToRemoteImageInfo(System.Collections.Generic.IReadOnlyList`1<TMDbLib.Objects.General.ImageData>,System.String)
ConvertBackdropsToRemoteImageInfo(System.Collections.Generic.IReadOnlyList`1<TMDbLib.Objects.General.ImageData>,System.String)
ConvertLogosToRemoteImageInfo(System.Collections.Generic.IReadOnlyList`1<TMDbLib.Objects.General.ImageData>,System.String)
ConvertProfilesToRemoteImageInfo(System.Collections.Generic.IReadOnlyList`1<TMDbLib.Objects.General.ImageData>,System.String)
ConvertStillsToRemoteImageInfo(System.Collections.Generic.IReadOnlyList`1<TMDbLib.Objects.General.ImageData>,System.String)
ConvertToRemoteImageInfo()
EnsureClientConfigAsync()
ValidatePreferences(TMDbLib.Objects.General.TMDbConfig)
GetClientConfiguration()
Cache(System.String,T)
CacheSearch(System.String,TMDbLib.Objects.General.SearchContainer`1<T>)
Count(System.Collections.Generic.IReadOnlyCollection`1<T>)
EstimateSize(System.Object)
EstimateAggregateSize(TMDbLib.Objects.TvShows.CreditsAggregate)
Dispose()
Dispose(System.Boolean)