Skip to content

The Free Software Media System - Server Backend & API

36.6 KB
Raw Blame
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using Microsoft.Extensions.Caching.Memory;
using TMDbLib.Client;
using TMDbLib.Objects.Collections;
using TMDbLib.Objects.Find;
using TMDbLib.Objects.General;
using TMDbLib.Objects.Movies;
using TMDbLib.Objects.People;
using TMDbLib.Objects.Search;
using TMDbLib.Objects.TvShows;

namespace MediaBrowser.Providers.Plugins.Tmdb
{
    /// <summary>
    /// Manager class for abstracting the TMDb API client library.
    /// </summary>
    public class TmdbClientManager : IDisposable
    {
        private const int CacheDurationInHours = 1;

        // Sized in TMDb records - see EstimateSize - rather than in responses, because the responses
        // differ in weight by orders of magnitude.
        private const int CacheSizeLimit = 100_000;

        private static readonly Dictionary<string, string> ThumbnailSizes = new Dictionary<string, string>
        {
            { "Primary", "w500" },
            { "Backdrop", "w780" },
            { "Thumb", "w780" },
            { "Logo", "w500" },
        };

        private readonly MemoryCache _memoryCache;
        private readonly TMDbClient _tmDbClient;

        /// <summary>
        /// Initializes a new instance of the <see cref="TmdbClientManager"/> class.
        /// </summary>
        public TmdbClientManager()
        {
            _memoryCache = new MemoryCache(new MemoryCacheOptions { SizeLimit = CacheSizeLimit });

            var apiKey = Plugin.Instance.Configuration.TmdbApiKey;
            apiKey = string.IsNullOrEmpty(apiKey) ? TmdbUtils.ApiKey : apiKey;
            _tmDbClient = new TMDbClient(apiKey);

            // Not really interested in NotFoundException
            _tmDbClient.ThrowApiExceptions = false;
        }

        /// <summary>
        /// Gets a movie from the TMDb API based on its TMDb id.
        /// </summary>
        /// <param name="tmdbId">The movie's TMDb id.</param>
        /// <param name="language">The movie's language.</param>
        /// <param name="imageLanguages">A comma-separated list of image languages.</param>
        /// <param name="countryCode">The country code, ISO 3166-1.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The TMDb movie or null if not found.</returns>
        public async Task<Movie?> GetMovieAsync(int tmdbId, string? language, string? imageLanguages, string? countryCode, CancellationToken cancellationToken)
        {
            var key = $"movie-{tmdbId.ToString(CultureInfo.InvariantCulture)}-{language}";
            if (_memoryCache.TryGetValue(key, out Movie? movie))
            {
                return movie;
            }

            await EnsureClientConfigAsync().ConfigureAwait(false);

            var extraMethods = MovieMethods.Credits | MovieMethods.Releases | MovieMethods.Images | MovieMethods.Videos;
            if (!(Plugin.Instance?.Configuration.ExcludeTagsMovies).GetValueOrDefault())
            {
                extraMethods |= MovieMethods.Keywords;
            }

            movie = await _tmDbClient.GetMovieAsync(
                tmdbId,
                TmdbUtils.NormalizeLanguage(language, countryCode),
                imageLanguages,
                extraMethods,
                cancellationToken).ConfigureAwait(false);

            if (movie is not null)
            {
                Cache(key, movie);
            }

            return movie;
        }

        /// <summary>
        /// Gets a collection from the TMDb API based on its TMDb id.
        /// </summary>
        /// <param name="tmdbId">The collection's TMDb id.</param>
        /// <param name="language">The collection's language.</param>
        /// <param name="imageLanguages">A comma-separated list of image languages.</param>
        /// <param name="countryCode">The country code, ISO 3166-1.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The TMDb collection or null if not found.</returns>
        public async Task<Collection?> GetCollectionAsync(int tmdbId, string? language, string? imageLanguages, string? countryCode, CancellationToken cancellationToken)
        {
            var key = $"collection-{tmdbId.ToString(CultureInfo.InvariantCulture)}-{language}";
            if (_memoryCache.TryGetValue(key, out Collection? collection))
            {
                return collection;
            }

            await EnsureClientConfigAsync().ConfigureAwait(false);

            collection = await _tmDbClient.GetCollectionAsync(
                tmdbId,
                TmdbUtils.NormalizeLanguage(language, countryCode),
                imageLanguages,
                CollectionMethods.Images,
                cancellationToken).ConfigureAwait(false);

            if (collection is not null)
            {
                Cache(key, collection);
            }

            return collection;
        }

        /// <summary>
        /// Gets a tv show from the TMDb API based on its TMDb id.
        /// </summary>
        /// <param name="tmdbId">The tv show's TMDb id.</param>
        /// <param name="language">The tv show's language.</param>
        /// <param name="imageLanguages">A comma-separated list of image languages.</param>
        /// <param name="countryCode">The country code, ISO 3166-1.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The TMDb tv show information or null if not found.</returns>
        public async Task<TvShow?> GetSeriesAsync(int tmdbId, string? language, string? imageLanguages, string? countryCode, CancellationToken cancellationToken)
        {
            var key = $"series-{tmdbId.ToString(CultureInfo.InvariantCulture)}-{language}";
            if (_memoryCache.TryGetValue(key, out TvShow? series))
            {
                return series;
            }

            await EnsureClientConfigAsync().ConfigureAwait(false);

            var extraMethods = TvShowMethods.Credits | TvShowMethods.CreditsAggregate | TvShowMethods.Images | TvShowMethods.ExternalIds | TvShowMethods.Videos | TvShowMethods.ContentRatings | TvShowMethods.EpisodeGroups;
            if (!(Plugin.Instance?.Configuration.ExcludeTagsSeries).GetValueOrDefault())
            {
                extraMethods |= TvShowMethods.Keywords;
            }

            series = await _tmDbClient.GetTvShowAsync(
                tmdbId,
                language: TmdbUtils.NormalizeLanguage(language, countryCode),
                includeImageLanguage: imageLanguages,
                extraMethods: extraMethods,
                cancellationToken: cancellationToken).ConfigureAwait(false);

            if (series is not null)
            {
                Cache(key, series);
            }

            return series;
        }

        /// <summary>
        /// Gets a tv show episode group from the TMDb API based on the episode group.
        /// </summary>
        /// <param name="episodeGroupId">The Tmdb episode group.</param>
        /// <param name="seasonNumber">The season number of the episode group.</param>
        /// <param name="language">The tv show's language.</param>
        /// <param name="imageLanguages">A comma-separated list of image languages.</param>
        /// <param name="countryCode">The country code, ISO 3166-1.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The TMDb tv show episode group information or null if not found.</returns>
        public async Task<TvGroup?> GetSeriesGroupAsync(string episodeGroupId, int seasonNumber, string? language, string? imageLanguages, string? countryCode, CancellationToken cancellationToken)
        {
            var key = $"group-{episodeGroupId}-{seasonNumber.ToString(CultureInfo.InvariantCulture)}-{language}";
            if (_memoryCache.TryGetValue(key, out TvGroup? group))
            {
                return group;
            }

            await EnsureClientConfigAsync().ConfigureAwait(false);

            var groups = await _tmDbClient.GetTvEpisodeGroupsAsync(
                episodeGroupId,
                language: TmdbUtils.NormalizeLanguage(language, countryCode),
                cancellationToken: cancellationToken).ConfigureAwait(false);

            group = groups?.Groups?.Find(s => s.Order == seasonNumber);
            if (group is not null)
            {
                Cache(key, group);
            }

            return group;
        }

        /// <summary>
        /// Gets a tv season from the TMDb API based on the tv show's TMDb id.
        /// </summary>
        /// <param name="tvShowId">The tv season's TMDb id.</param>
        /// <param name="seasonNumber">The season number.</param>
        /// <param name="language">The tv season's language.</param>
        /// <param name="imageLanguages">A comma-separated list of image languages.</param>
        /// <param name="countryCode">The country code, ISO 3166-1.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The TMDb tv season information or null if not found.</returns>
        public async Task<TvSeason?> GetSeasonAsync(int tvShowId, int seasonNumber, string? language, string? imageLanguages, string? countryCode, CancellationToken cancellationToken)
        {
            var key = $"season-{tvShowId.ToString(CultureInfo.InvariantCulture)}-s{seasonNumber.ToString(CultureInfo.InvariantCulture)}-{language}";
            if (_memoryCache.TryGetValue(key, out TvSeason? season))
            {
                return season;
            }

            await EnsureClientConfigAsync().ConfigureAwait(false);

            season = await _tmDbClient.GetTvSeasonAsync(
                tvShowId,
                seasonNumber,
                language: TmdbUtils.NormalizeLanguage(language, countryCode),
                includeImageLanguage: imageLanguages,
                extraMethods: TvSeasonMethods.Credits | TvSeasonMethods.Images | TvSeasonMethods.ExternalIds | TvSeasonMethods.Videos,
                cancellationToken: cancellationToken).ConfigureAwait(false);

            if (season is not null)
            {
                Cache(key, season);
            }

            return season;
        }

        /// <summary>
        /// Gets an episode from the TMDb API based on the tv show's TMDb id.
        /// </summary>
        /// <param name="tvShowId">The tv show's TMDb id.</param>
        /// <param name="seasonNumber">The season number.</param>
        /// <param name="episodeNumber">The episode number.</param>
        /// <param name="episodeGroup">The containing episode group.</param>
        /// <param name="language">The episode's language.</param>
        /// <param name="imageLanguages">A comma-separated list of image languages.</param>
        /// <param name="countryCode">The country code, ISO 3166-1.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The TMDb tv episode information or null if not found.</returns>
        public async Task<TvEpisode?> GetEpisodeAsync(int tvShowId, int seasonNumber, long episodeNumber, string? episodeGroup, string? language, string? imageLanguages, string? countryCode, CancellationToken cancellationToken)
        {
            var key = $"episode-{tvShowId.ToString(CultureInfo.InvariantCulture)}-s{seasonNumber.ToString(CultureInfo.InvariantCulture)}e{episodeNumber.ToString(CultureInfo.InvariantCulture)}-{episodeGroup}-{language}";
            if (_memoryCache.TryGetValue(key, out TvEpisode? episode))
            {
                return episode;
            }

            await EnsureClientConfigAsync().ConfigureAwait(false);

            if (!string.IsNullOrWhiteSpace(episodeGroup))
            {
                var group = await GetSeriesGroupAsync(episodeGroup, seasonNumber, language, imageLanguages, countryCode, cancellationToken).ConfigureAwait(false);
                var ep = group?.Episodes?.Find(e => e.Order == episodeNumber - 1);
                if (ep is not null)
                {
                    seasonNumber = ep.SeasonNumber;
                    episodeNumber = ep.EpisodeNumber;
                }
            }

            episode = await _tmDbClient.GetTvEpisodeAsync(
                tvShowId,
                seasonNumber,
                episodeNumber,
                language: TmdbUtils.NormalizeLanguage(language, countryCode),
                includeImageLanguage: imageLanguages,
                extraMethods: TvEpisodeMethods.Credits | TvEpisodeMethods.Images | TvEpisodeMethods.ExternalIds | TvEpisodeMethods.Videos,
                cancellationToken: cancellationToken).ConfigureAwait(false);

            if (episode is not null)
            {
                Cache(key, episode);
            }

            return episode;
        }

        /// <summary>
        /// Gets a person eg. cast or crew member from the TMDb API based on its TMDb id.
        /// </summary>
        /// <param name="personTmdbId">The person's TMDb id.</param>
        /// <param name="language">The person's language.</param>
        /// <param name="countryCode">The country code, ISO 3166-1.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The TMDb person information or null if not found.</returns>
        public async Task<Person?> GetPersonAsync(int personTmdbId, string language, string? countryCode, CancellationToken cancellationToken)
        {
            var key = $"person-{personTmdbId.ToString(CultureInfo.InvariantCulture)}-{language}";
            if (_memoryCache.TryGetValue(key, out Person? person))
            {
                return person;
            }

            await EnsureClientConfigAsync().ConfigureAwait(false);

            person = await _tmDbClient.GetPersonAsync(
                personTmdbId,
                TmdbUtils.NormalizeLanguage(language, countryCode),
                PersonMethods.Images | PersonMethods.ExternalIds,
                cancellationToken).ConfigureAwait(false);

            if (person is not null)
            {
                Cache(key, person);
            }

            return person;
        }

        /// <summary>
        /// Gets an item from the TMDb API based on its id from an external service eg. IMDb id, TvDb id.
        /// </summary>
        /// <param name="externalId">The item's external id.</param>
        /// <param name="source">The source of the id eg. IMDb.</param>
        /// <param name="language">The item's language.</param>
        /// <param name="countryCode">The country code, ISO 3166-1.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The TMDb item or null if not found.</returns>
        public async Task<FindContainer?> FindByExternalIdAsync(
            string externalId,
            FindExternalSource source,
            string language,
            string? countryCode,
            CancellationToken cancellationToken)
        {
            var key = $"find-{source.ToString()}-{externalId.ToString(CultureInfo.InvariantCulture)}-{language}";
            if (_memoryCache.TryGetValue(key, out FindContainer? result))
            {
                return result;
            }

            await EnsureClientConfigAsync().ConfigureAwait(false);

            result = await _tmDbClient.FindAsync(
                source,
                externalId,
                TmdbUtils.NormalizeLanguage(language, countryCode),
                cancellationToken).ConfigureAwait(false);

            if (result is not null)
            {
                Cache(key, result);
            }

            return result;
        }

        /// <summary>
        /// Searches for a tv show using the TMDb API based on its name.
        /// </summary>
        /// <param name="name">The name of the tv show.</param>
        /// <param name="language">The tv show's language.</param>
        /// <param name="countryCode">The country code, ISO 3166-1.</param>
        /// <param name="year">The year the tv show first aired.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The TMDb tv show information.</returns>
        public async Task<IReadOnlyList<SearchTv>?> SearchSeriesAsync(string name, string language, string? countryCode, int year = 0, CancellationToken cancellationToken = default)
        {
            var key = $"searchseries-{name}-{year.ToString(CultureInfo.InvariantCulture)}-{language}";
            if (_memoryCache.TryGetValue(key, out SearchContainer<SearchTv>? series) && series is not null)
            {
                return series.Results;
            }

            await EnsureClientConfigAsync().ConfigureAwait(false);

            var searchResults = await _tmDbClient
                .SearchTvShowAsync(name, TmdbUtils.NormalizeLanguage(language, countryCode), includeAdult: Plugin.Instance.Configuration.IncludeAdult, firstAirDateYear: year, cancellationToken: cancellationToken)
                .ConfigureAwait(false);

            if (searchResults?.Results?.Count > 0)
            {
                CacheSearch(key, searchResults);
            }

            return searchResults?.Results;
        }

        /// <summary>
        /// Searches for a person based on their name using the TMDb API.
        /// </summary>
        /// <param name="name">The name of the person.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The TMDb person information.</returns>
        public async Task<IReadOnlyList<SearchPerson>?> SearchPersonAsync(string name, CancellationToken cancellationToken)
        {
            var key = $"searchperson-{name}";
            if (_memoryCache.TryGetValue(key, out SearchContainer<SearchPerson>? person) && person is not null)
            {
                return person.Results;
            }

            await EnsureClientConfigAsync().ConfigureAwait(false);

            var searchResults = await _tmDbClient
                .SearchPersonAsync(name, includeAdult: Plugin.Instance.Configuration.IncludeAdult, cancellationToken: cancellationToken)
                .ConfigureAwait(false);

            if (searchResults?.Results?.Count > 0)
            {
                CacheSearch(key, searchResults);
            }

            return searchResults?.Results;
        }

        /// <summary>
        /// Searches for a movie based on its name using the TMDb API.
        /// </summary>
        /// <param name="name">The name of the movie.</param>
        /// <param name="language">The movie's language.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The TMDb movie information.</returns>
        public Task<IReadOnlyList<SearchMovie>?> SearchMovieAsync(string name, string language, CancellationToken cancellationToken)
        {
            return SearchMovieAsync(name, 0, language, null, cancellationToken);
        }

        /// <summary>
        /// Searches for a movie based on its name using the TMDb API.
        /// </summary>
        /// <param name="name">The name of the movie.</param>
        /// <param name="year">The release year of the movie.</param>
        /// <param name="language">The movie's language.</param>
        /// <param name="countryCode">The country code, ISO 3166-1.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The TMDb movie information.</returns>
        public async Task<IReadOnlyList<SearchMovie>?> SearchMovieAsync(string name, int year, string language, string? countryCode, CancellationToken cancellationToken)
        {
            var key = $"moviesearch-{name}-{year.ToString(CultureInfo.InvariantCulture)}-{language}";
            if (_memoryCache.TryGetValue(key, out SearchContainer<SearchMovie>? movies) && movies is not null)
            {
                return movies.Results;
            }

            await EnsureClientConfigAsync().ConfigureAwait(false);

            var searchResults = await _tmDbClient
                .SearchMovieAsync(name, TmdbUtils.NormalizeLanguage(language, countryCode), includeAdult: Plugin.Instance.Configuration.IncludeAdult, year: year, cancellationToken: cancellationToken)
                .ConfigureAwait(false);

            if (searchResults?.Results?.Count > 0)
            {
                CacheSearch(key, searchResults);
            }

            return searchResults?.Results;
        }

        /// <summary>
        /// Searches for a collection based on its name using the TMDb API.
        /// </summary>
        /// <param name="name">The name of the collection.</param>
        /// <param name="language">The collection's language.</param>
        /// <param name="countryCode">The country code, ISO 3166-1.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>The TMDb collection information.</returns>
        public async Task<IReadOnlyList<SearchCollection>?> SearchCollectionAsync(string name, string language, string? countryCode, CancellationToken cancellationToken)
        {
            var key = $"collectionsearch-{name}-{language}";
            if (_memoryCache.TryGetValue(key, out SearchContainer<SearchCollection>? collections) && collections is not null)
            {
                return collections.Results;
            }

            await EnsureClientConfigAsync().ConfigureAwait(false);

            var searchResults = await _tmDbClient
                .SearchCollectionAsync(name, TmdbUtils.NormalizeLanguage(language, countryCode), cancellationToken: cancellationToken)
                .ConfigureAwait(false);

            if (searchResults?.Results?.Count > 0)
            {
                CacheSearch(key, searchResults);
            }

            return searchResults?.Results;
        }

        /// <summary>
        /// Gets a single page of recommended movies for a movie from the TMDb API.
        /// </summary>
        /// <param name="tmdbId">The TMDb id of the movie.</param>
        /// <param name="page">The page number to fetch (1-based).</param>
        /// <param name="language">The language for results.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A tuple containing the list of recommended movies and the total number of pages available.</returns>
        public async Task<(IReadOnlyList<SearchMovie> Results, int TotalPages)> GetMovieRecommendationsPageAsync(int tmdbId, int page, string? language, CancellationToken cancellationToken)
        {
            await EnsureClientConfigAsync().ConfigureAwait(false);

            var searchResults = await _tmDbClient
                .GetMovieRecommendationsAsync(tmdbId, language, page, cancellationToken)
                .ConfigureAwait(false);

            if (searchResults?.Results is null || searchResults.Results.Count == 0)
            {
                return ([], 0);
            }

            return (searchResults.Results, searchResults.TotalPages);
        }

        /// <summary>
        /// Gets a single page of recommended TV shows for a series from the TMDb API.
        /// </summary>
        /// <param name="tmdbId">The TMDb id of the TV show.</param>
        /// <param name="page">The page number to fetch (1-based).</param>
        /// <param name="language">The language for results.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A tuple containing the list of recommended TV shows and the total number of pages available.</returns>
        public async Task<(IReadOnlyList<SearchTv> Results, int TotalPages)> GetSeriesRecommendationsPageAsync(int tmdbId, int page, string? language, CancellationToken cancellationToken)
        {
            await EnsureClientConfigAsync().ConfigureAwait(false);

            var searchResults = await _tmDbClient
                .GetTvShowRecommendationsAsync(tmdbId, language, page, cancellationToken)
                .ConfigureAwait(false);

            if (searchResults?.Results is null || searchResults.Results.Count == 0)
            {
                return ([], 0);
            }

            return (searchResults.Results, searchResults.TotalPages);
        }

        /// <summary>
        /// Handles bad path checking and builds the absolute url.
        /// </summary>
        /// <param name="size">The image size to fetch.</param>
        /// <param name="path">The relative URL of the image.</param>
        /// <returns>The absolute URL.</returns>
        private string? GetUrl(string? size, string? path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return null;
            }

            // Use the original size as default if size is null or empty to prevent malformed URLs
            var imageSize = string.IsNullOrEmpty(size) ? TmdbUtils.OriginalImageSize : size;

            return _tmDbClient.GetImageUrl(imageSize, path, true).ToString();
        }

        /// <summary>
        /// Gets the absolute URL of the poster.
        /// </summary>
        /// <param name="posterPath">The relative URL of the poster.</param>
        /// <returns>The absolute URL.</returns>
        public string? GetPosterUrl(string? posterPath)
        {
            return GetUrl(Plugin.Instance.Configuration.PosterSize, posterPath);
        }

        /// <summary>
        /// Gets the absolute URL of the profile image.
        /// </summary>
        /// <param name="actorProfilePath">The relative URL of the profile image.</param>
        /// <returns>The absolute URL.</returns>
        public string? GetProfileUrl(string? actorProfilePath)
        {
            return GetUrl(Plugin.Instance.Configuration.ProfileSize, actorProfilePath);
        }

        /// <summary>
        /// Gets the absolute URL of an episode still.
        /// </summary>
        /// <param name="stillPath">The relative URL of the still.</param>
        /// <returns>The absolute URL.</returns>
        public string? GetStillUrl(string? stillPath)
        {
            return GetUrl(Plugin.Instance.Configuration.StillSize, stillPath);
        }

        /// <summary>
        /// Converts poster <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
        /// </summary>
        /// <param name="images">The input images.</param>
        /// <param name="requestLanguage">The requested language.</param>
        /// <returns>The remote images.</returns>
        public IEnumerable<RemoteImageInfo> ConvertPostersToRemoteImageInfo(IReadOnlyList<ImageData> images, string requestLanguage)
            => ConvertToRemoteImageInfo(images, Plugin.Instance.Configuration.PosterSize, ImageType.Primary, requestLanguage);

        /// <summary>
        /// Converts backdrop <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
        /// </summary>
        /// <param name="images">The input images.</param>
        /// <param name="requestLanguage">The requested language.</param>
        /// <returns>The remote images.</returns>
        public IEnumerable<RemoteImageInfo> ConvertBackdropsToRemoteImageInfo(IReadOnlyList<ImageData> images, string requestLanguage)
            => ConvertToRemoteImageInfo(images, Plugin.Instance.Configuration.BackdropSize, ImageType.Backdrop, requestLanguage);

        /// <summary>
        /// Converts logo <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
        /// </summary>
        /// <param name="images">The input images.</param>
        /// <param name="requestLanguage">The requested language.</param>
        /// <returns>The remote images.</returns>
        public IEnumerable<RemoteImageInfo> ConvertLogosToRemoteImageInfo(IReadOnlyList<ImageData> images, string requestLanguage)
            => ConvertToRemoteImageInfo(images, Plugin.Instance.Configuration.LogoSize, ImageType.Logo, requestLanguage);

        /// <summary>
        /// Converts profile <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
        /// </summary>
        /// <param name="images">The input images.</param>
        /// <param name="requestLanguage">The requested language.</param>
        /// <returns>The remote images.</returns>
        public IEnumerable<RemoteImageInfo> ConvertProfilesToRemoteImageInfo(IReadOnlyList<ImageData> images, string requestLanguage)
            => ConvertToRemoteImageInfo(images, Plugin.Instance.Configuration.ProfileSize, ImageType.Primary, requestLanguage);

        /// <summary>
        /// Converts still <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
        /// </summary>
        /// <param name="images">The input images.</param>
        /// <param name="requestLanguage">The requested language.</param>
        /// <returns>The remote images.</returns>
        public IEnumerable<RemoteImageInfo> ConvertStillsToRemoteImageInfo(IReadOnlyList<ImageData> images, string requestLanguage)
            => ConvertToRemoteImageInfo(images, Plugin.Instance.Configuration.StillSize, ImageType.Primary, requestLanguage);

        /// <summary>
        /// Converts <see cref="ImageData"/>s into <see cref="RemoteImageInfo"/>s.
        /// </summary>
        /// <param name="images">The input images.</param>
        /// <param name="size">The size of the image to fetch.</param>
        /// <param name="type">The type of the image.</param>
        /// <param name="requestLanguage">The requested language.</param>
        /// <returns>The remote images.</returns>
        private IEnumerable<RemoteImageInfo> ConvertToRemoteImageInfo(IReadOnlyList<ImageData> images, string? size, ImageType type, string requestLanguage)
        {
            // sizes provided are for original resolution, don't store them when downloading scaled images
            var scaleImage = !TmdbUtils.IsOriginalImageSize(size);

            for (var i = 0; i < images.Count; i++)
            {
                var image = images[i];

                var imageType = type;
                var language = TmdbUtils.GetImageLanguage(image.Iso_639_1, image.Iso_3166_1, requestLanguage);

                // Return Backdrops with a language specified (it has text) as Thumb.
                if (imageType == ImageType.Backdrop && !string.IsNullOrEmpty(language))
                {
                    imageType = ImageType.Thumb;
                }

                yield return new RemoteImageInfo
                {
                    Url = GetUrl(size, image.FilePath),
                    ThumbnailUrl = GetUrl(ThumbnailSizes.GetValueOrDefault(type.ToString(), string.Empty), image.FilePath),
                    CommunityRating = image.VoteAverage,
                    VoteCount = image.VoteCount,
                    Width = scaleImage ? null : image.Width,
                    Height = scaleImage ? null : image.Height,
                    Language = language,
                    ProviderName = TmdbUtils.ProviderName,
                    Type = imageType,
                    RatingType = RatingType.Score
                };
            }
        }

        private async Task EnsureClientConfigAsync()
        {
            if (!_tmDbClient.HasConfig)
            {
                var config = await _tmDbClient.GetConfigAsync().ConfigureAwait(false);
                ValidatePreferences(config);
            }
        }

        private static void ValidatePreferences(TMDbConfig config)
        {
            var imageConfig = config.Images;
            if (imageConfig is null)
            {
                return;
            }

            var pluginConfig = Plugin.Instance.Configuration;

            if (imageConfig.PosterSizes is not null
                && pluginConfig.PosterSize is not null
                && !imageConfig.PosterSizes.Contains(pluginConfig.PosterSize))
            {
                pluginConfig.PosterSize = imageConfig.PosterSizes[^1];
            }

            if (imageConfig.BackdropSizes is not null
                && pluginConfig.BackdropSize is not null
                && !imageConfig.BackdropSizes.Contains(pluginConfig.BackdropSize))
            {
                pluginConfig.BackdropSize = imageConfig.BackdropSizes[^1];
            }

            if (imageConfig.LogoSizes is not null
                && pluginConfig.LogoSize is not null
                && !imageConfig.LogoSizes.Contains(pluginConfig.LogoSize))
            {
                pluginConfig.LogoSize = imageConfig.LogoSizes[^1];
            }

            if (imageConfig.ProfileSizes is not null
                && pluginConfig.ProfileSize is not null
                && !imageConfig.ProfileSizes.Contains(pluginConfig.ProfileSize))
            {
                pluginConfig.ProfileSize = imageConfig.ProfileSizes[^1];
            }

            if (imageConfig.StillSizes is not null
                && pluginConfig.StillSize is not null
                && !imageConfig.StillSizes.Contains(pluginConfig.StillSize))
            {
                pluginConfig.StillSize = imageConfig.StillSizes[^1];
            }
        }

        /// <summary>
        /// Gets the <see cref="TMDbClient"/> configuration.
        /// </summary>
        /// <returns>The configuration.</returns>
        public async Task<TMDbConfig> GetClientConfiguration()
        {
            await EnsureClientConfigAsync().ConfigureAwait(false);

            return _tmDbClient.Config;
        }

        /// <summary>
        /// Stores a response under the shared expiry, weighed by what it costs to keep.
        /// </summary>
        private void Cache<T>(string key, T value)
            where T : class
            => _memoryCache.Set(
                key,
                value,
                new MemoryCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(CacheDurationInHours),
                    Size = EstimateSize(value)
                });

        /// <summary>
        /// Stores a page of search results, whose weight is simply how many there are.
        /// </summary>
        private void CacheSearch<T>(string key, SearchContainer<T> results)
            => _memoryCache.Set(
                key,
                results,
                new MemoryCacheEntryOptions
                {
                    AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(CacheDurationInHours),
                    Size = 1 + Count(results.Results)
                });

        private static long Count<T>(IReadOnlyCollection<T>? items) => items?.Count ?? 0;

        /// <summary>
        /// Estimates what keeping a response costs, counting the sub-records that dominate it.
        /// </summary>
        private static long EstimateSize(object? value) => value switch
        {
            TvShow series => 1
                + Count(series.Credits?.Cast) + Count(series.Credits?.Crew)
                + EstimateAggregateSize(series.AggregateCredits)
                + Count(series.Seasons),
            TvSeason season => 1
                + Count(season.Credits?.Cast) + Count(season.Credits?.Crew)
                + Count(season.Episodes),
            TvEpisode episode => 1
                + Count(episode.Credits?.Cast) + Count(episode.Credits?.Crew)
                + Count(episode.Credits?.GuestStars),
            Movie movie => 1 + Count(movie.Credits?.Cast) + Count(movie.Credits?.Crew),
            Collection collection => 1 + Count(collection.Parts),
            TvGroupCollection groups => 1 + Count(groups.Groups),
            FindContainer found => 1
                + Count(found.MovieResults) + Count(found.TvResults)
                + Count(found.PersonResults) + Count(found.TvEpisode) + Count(found.TvSeason),
            _ => 1
        };

        /// <summary>
        /// Weighs aggregate credits, where each person carries one record per episode they worked on.
        /// </summary>
        private static long EstimateAggregateSize(CreditsAggregate? credits)
        {
            if (credits is null)
            {
                return 0;
            }

            var size = Count(credits.Cast) + Count(credits.Crew);

            foreach (var cast in credits.Cast ?? [])
            {
                size += Count(cast.Roles);
            }

            foreach (var crew in credits.Crew ?? [])
            {
                size += Count(crew.Jobs);
            }

            return size;
        }

        /// <inheritdoc />
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                _memoryCache.Dispose();
                _tmDbClient?.Dispose();
            }
        }
    }
}