From 12e93a002c4304d93d7c38553a5a55ec319c694d Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Sat, 31 Oct 2026 13:28:51 +0100 Subject: Migrate to TMDbLib 4.0.0 --- Directory.Packages.props | 2 +- .../Plugins/Tmdb/Movies/TmdbMovieProvider.cs | 30 ++++++++++----- .../Tmdb/Movies/TmdbMovieSimilarProvider.cs | 43 ++++++++++------------ .../Plugins/Tmdb/TV/TmdbSeriesSimilarProvider.cs | 43 ++++++++++------------ .../Plugins/Tmdb/TmdbClientManager.cs | 41 +++++++-------------- MediaBrowser.Providers/Plugins/Tmdb/TmdbUtils.cs | 10 +++++ 6 files changed, 84 insertions(+), 85 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index a68ab68934..4f8b99e54d 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -81,7 +81,7 @@ - + diff --git a/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieProvider.cs b/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieProvider.cs index 9501da7ec6..ffb6e0edce 100644 --- a/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieProvider.cs +++ b/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieProvider.cs @@ -17,6 +17,8 @@ using MediaBrowser.Model.Providers; using TMDbLib.Objects.Find; using TMDbLib.Objects.General; using TMDbLib.Objects.Search; +using ReleaseDatesContainer = TMDbLib.Objects.Movies.ReleaseDatesContainer; +using ReleaseDateType = TMDbLib.Objects.Movies.ReleaseDateType; namespace MediaBrowser.Providers.Plugins.Tmdb.Movies { @@ -223,22 +225,20 @@ namespace MediaBrowser.Providers.Plugins.Tmdb.Movies movie.CommunityRating = Convert.ToSingle(movieResult.VoteAverage); - if (movieResult.Releases?.Countries is not null) + if (movieResult.ReleaseDates?.Results is not null) { - var releases = movieResult.Releases.Countries.Where(i => !string.IsNullOrWhiteSpace(i.Certification)).ToList(); + var ourCertification = GetCertification(movieResult.ReleaseDates.Results, info.MetadataCountryCode); - var ourRelease = releases.FirstOrDefault(c => string.Equals(c.Iso_3166_1, info.MetadataCountryCode, StringComparison.OrdinalIgnoreCase)); - - if (ourRelease?.Certification is not null) + if (ourCertification is not null) { - movie.OfficialRating = TmdbUtils.BuildParentalRating(info.MetadataCountryCode, ourRelease.Certification); + movie.OfficialRating = TmdbUtils.BuildParentalRating(info.MetadataCountryCode, ourCertification); } else { - var usRelease = releases.FirstOrDefault(c => string.Equals(c.Iso_3166_1, "US", StringComparison.OrdinalIgnoreCase)); - if (usRelease?.Certification is not null) + var usCertification = GetCertification(movieResult.ReleaseDates.Results, "US"); + if (usCertification is not null) { - movie.OfficialRating = usRelease.Certification; + movie.OfficialRating = usCertification; } } } @@ -392,6 +392,18 @@ namespace MediaBrowser.Providers.Plugins.Tmdb.Movies return metadataResult; } + private static string? GetCertification(IEnumerable releases, string? countryCode) + { + var certified = releases + .FirstOrDefault(r => string.Equals(r.Iso_3166_1, countryCode, StringComparison.OrdinalIgnoreCase))? + .ReleaseDates? + .Where(r => !string.IsNullOrWhiteSpace(r.Certification)) + .ToList(); + + // A country lists one entry per release type; the theatrical one carries the rating that applies. + return (certified?.Find(r => r.Type == ReleaseDateType.Theatrical) ?? certified?.FirstOrDefault())?.Certification; + } + /// public Task GetImageResponse(string url, CancellationToken cancellationToken) { diff --git a/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieSimilarProvider.cs b/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieSimilarProvider.cs index 6a3c72d2fa..b90844afe4 100644 --- a/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieSimilarProvider.cs +++ b/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieSimilarProvider.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Globalization; using System.Runtime.CompilerServices; using System.Threading; +using System.Threading.Tasks; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Entities; @@ -58,39 +59,33 @@ public class TmdbMovieSimilarProvider : IRemoteSimilarItemsProvider } var providerName = MetadataProvider.Tmdb.ToString(); - var page = 1; - var totalPages = 1; + var recommendations = _tmdbClientManager + .GetMovieRecommendationsAsync(tmdbId, TmdbUtils.GetImageLanguagesParam(string.Empty), cancellationToken) + .GetAsyncEnumerator(cancellationToken); - while (page <= totalPages && !cancellationToken.IsCancellationRequested) + await using (recommendations.ConfigureAwait(false)) { - IReadOnlyList pageResults; - try + while (true) { - (pageResults, totalPages) = await _tmdbClientManager - .GetMovieRecommendationsPageAsync(tmdbId, page, TmdbUtils.GetImageLanguagesParam(string.Empty), cancellationToken) - .ConfigureAwait(false); - } - catch (Exception ex) - { - _logger.LogWarning(ex, "Failed to get recommended movies from TMDb for {TmdbId} page {Page}", tmdbId, page); - yield break; - } - - if (pageResults.Count == 0) - { - yield break; - } + try + { + if (!await recommendations.MoveNextAsync().ConfigureAwait(false)) + { + yield break; + } + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Failed to get recommended movies from TMDb for {TmdbId}", tmdbId); + yield break; + } - foreach (var recommendation in pageResults) - { yield return new SimilarItemReference { ProviderName = providerName, - ProviderId = recommendation.Id.ToString(CultureInfo.InvariantCulture) + ProviderId = recommendations.Current.Id.ToString(CultureInfo.InvariantCulture) }; } - - page++; } } } diff --git a/MediaBrowser.Providers/Plugins/Tmdb/TV/TmdbSeriesSimilarProvider.cs b/MediaBrowser.Providers/Plugins/Tmdb/TV/TmdbSeriesSimilarProvider.cs index 40c7de05ad..c439049dd4 100644 --- a/MediaBrowser.Providers/Plugins/Tmdb/TV/TmdbSeriesSimilarProvider.cs +++ b/MediaBrowser.Providers/Plugins/Tmdb/TV/TmdbSeriesSimilarProvider.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Globalization; using System.Runtime.CompilerServices; using System.Threading; +using System.Threading.Tasks; using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Configuration; @@ -58,39 +59,33 @@ public class TmdbSeriesSimilarProvider : IRemoteSimilarItemsProvider } var providerName = MetadataProvider.Tmdb.ToString(); - var page = 1; - var totalPages = 1; + var recommendations = _tmdbClientManager + .GetSeriesRecommendationsAsync(tmdbId, TmdbUtils.GetImageLanguagesParam(string.Empty), cancellationToken) + .GetAsyncEnumerator(cancellationToken); - while (page <= totalPages && !cancellationToken.IsCancellationRequested) + await using (recommendations.ConfigureAwait(false)) { - IReadOnlyList pageResults; - try + while (true) { - (pageResults, totalPages) = await _tmdbClientManager - .GetSeriesRecommendationsPageAsync(tmdbId, page, TmdbUtils.GetImageLanguagesParam(string.Empty), cancellationToken) - .ConfigureAwait(false); - } - catch (Exception ex) - { - _logger.LogWarning(ex, "Failed to get recommended TV shows from TMDb for {TmdbId} page {Page}", tmdbId, page); - yield break; - } - - if (pageResults.Count == 0) - { - yield break; - } + try + { + if (!await recommendations.MoveNextAsync().ConfigureAwait(false)) + { + yield break; + } + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Failed to get recommended TV shows from TMDb for {TmdbId}", tmdbId); + yield break; + } - foreach (var recommendation in pageResults) - { yield return new SimilarItemReference { ProviderName = providerName, - ProviderId = recommendation.Id.ToString(CultureInfo.InvariantCulture) + ProviderId = recommendations.Current.Id.ToString(CultureInfo.InvariantCulture) }; } - - page++; } } } diff --git a/MediaBrowser.Providers/Plugins/Tmdb/TmdbClientManager.cs b/MediaBrowser.Providers/Plugins/Tmdb/TmdbClientManager.cs index 7d119e8b84..db4fa4dd25 100644 --- a/MediaBrowser.Providers/Plugins/Tmdb/TmdbClientManager.cs +++ b/MediaBrowser.Providers/Plugins/Tmdb/TmdbClientManager.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using MediaBrowser.Model.Dto; @@ -74,7 +75,7 @@ namespace MediaBrowser.Providers.Plugins.Tmdb await EnsureClientConfigAsync().ConfigureAwait(false); - var extraMethods = MovieMethods.Credits | MovieMethods.Releases | MovieMethods.Images | MovieMethods.Videos; + var extraMethods = MovieMethods.Credits | MovieMethods.ReleaseDates | MovieMethods.Images | MovieMethods.Videos; if (!(Plugin.Instance?.Configuration.ExcludeTagsMovies).GetValueOrDefault()) { extraMethods |= MovieMethods.Keywords; @@ -251,7 +252,7 @@ namespace MediaBrowser.Providers.Plugins.Tmdb /// The country code, ISO 3166-1. /// The cancellation token. /// The TMDb tv episode information or null if not found. - public async Task GetEpisodeAsync(int tvShowId, int seasonNumber, long episodeNumber, string? episodeGroup, string? language, string? imageLanguages, string? countryCode, CancellationToken cancellationToken) + public async Task GetEpisodeAsync(int tvShowId, int seasonNumber, int 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)) @@ -492,51 +493,37 @@ namespace MediaBrowser.Providers.Plugins.Tmdb } /// - /// Gets a single page of recommended movies for a movie from the TMDb API. + /// Gets the recommended movies for a movie from the TMDb API, fetching further pages as the sequence is consumed. /// - /// The TMDb id of the movie. - /// The page number to fetch (1-based). + /// /// The TMDb id of the movie. /// The language for results. /// The cancellation token. - /// A tuple containing the list of recommended movies and the total number of pages available. - public async Task<(IReadOnlyList Results, int TotalPages)> GetMovieRecommendationsPageAsync(int tmdbId, int page, string? language, CancellationToken cancellationToken) + /// The recommended movies. + public async IAsyncEnumerable GetMovieRecommendationsAsync(int tmdbId, string? language, [EnumeratorCancellation] 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) + await foreach (var movie in _tmDbClient.GetMovieRecommendationsAllAsync(tmdbId, language, cancellationToken).ConfigureAwait(false)) { - return ([], 0); + yield return movie; } - - return (searchResults.Results, searchResults.TotalPages); } /// - /// Gets a single page of recommended TV shows for a series from the TMDb API. + /// Gets the recommended TV shows for a series from the TMDb API, fetching further pages as the sequence is consumed. /// /// The TMDb id of the TV show. - /// The page number to fetch (1-based). /// The language for results. /// The cancellation token. - /// A tuple containing the list of recommended TV shows and the total number of pages available. - public async Task<(IReadOnlyList Results, int TotalPages)> GetSeriesRecommendationsPageAsync(int tmdbId, int page, string? language, CancellationToken cancellationToken) + /// The recommended TV shows. + public async IAsyncEnumerable GetSeriesRecommendationsAsync(int tmdbId, string? language, [EnumeratorCancellation] 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) + await foreach (var series in _tmDbClient.GetTvShowRecommendationsAllAsync(tmdbId, language, cancellationToken).ConfigureAwait(false)) { - return ([], 0); + yield return series; } - - return (searchResults.Results, searchResults.TotalPages); } /// diff --git a/MediaBrowser.Providers/Plugins/Tmdb/TmdbUtils.cs b/MediaBrowser.Providers/Plugins/Tmdb/TmdbUtils.cs index 4ef8c53b30..5cd529d936 100644 --- a/MediaBrowser.Providers/Plugins/Tmdb/TmdbUtils.cs +++ b/MediaBrowser.Providers/Plugins/Tmdb/TmdbUtils.cs @@ -115,6 +115,16 @@ namespace MediaBrowser.Providers.Plugins.Tmdb return int.TryParse(value, NumberStyles.None, CultureInfo.InvariantCulture, out tmdbId) && tmdbId > 0; } + /// + /// Sets a numeric external id reported by TMDb, skipping the zero TMDb sends for "no id". + /// + /// The item. + /// The provider the id belongs to. + /// The id. + /// true if the id was set; otherwise, false. + public static bool TrySetProviderId(this IHasProviderIds instance, MetadataProvider provider, int? value) + => value > 0 && instance.TrySetProviderId(provider, value.Value.ToString(CultureInfo.InvariantCulture)); + /// /// Cleans the name according to TMDb requirements. /// -- cgit v1.2.3 From fd68a2a0c0163c22b17521b34ef77be9165e7e76 Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Sat, 31 Oct 2026 15:06:56 +0100 Subject: Apply review suggestions --- .../Plugins/Tmdb/Movies/TmdbMovieProvider.cs | 4 +-- .../Tmdb/Movies/TmdbMovieSimilarProvider.cs | 36 +++++----------------- .../Plugins/Tmdb/TV/TmdbSeriesSimilarProvider.cs | 36 +++++----------------- .../Plugins/Tmdb/TmdbClientManager.cs | 2 +- 4 files changed, 17 insertions(+), 61 deletions(-) diff --git a/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieProvider.cs b/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieProvider.cs index ffb6e0edce..353c9011ec 100644 --- a/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieProvider.cs +++ b/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieProvider.cs @@ -398,10 +398,10 @@ namespace MediaBrowser.Providers.Plugins.Tmdb.Movies .FirstOrDefault(r => string.Equals(r.Iso_3166_1, countryCode, StringComparison.OrdinalIgnoreCase))? .ReleaseDates? .Where(r => !string.IsNullOrWhiteSpace(r.Certification)) - .ToList(); + .ToArray(); // A country lists one entry per release type; the theatrical one carries the rating that applies. - return (certified?.Find(r => r.Type == ReleaseDateType.Theatrical) ?? certified?.FirstOrDefault())?.Certification; + return (certified?.FirstOrDefault(r => r.Type == ReleaseDateType.Theatrical) ?? certified?.FirstOrDefault())?.Certification; } /// diff --git a/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieSimilarProvider.cs b/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieSimilarProvider.cs index b90844afe4..9ac50029e1 100644 --- a/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieSimilarProvider.cs +++ b/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieSimilarProvider.cs @@ -7,7 +7,6 @@ using System.Threading.Tasks; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Entities; -using Microsoft.Extensions.Logging; using Movie = MediaBrowser.Controller.Entities.Movies.Movie; namespace MediaBrowser.Providers.Plugins.Tmdb.Movies; @@ -18,17 +17,14 @@ namespace MediaBrowser.Providers.Plugins.Tmdb.Movies; public class TmdbMovieSimilarProvider : IRemoteSimilarItemsProvider { private readonly TmdbClientManager _tmdbClientManager; - private readonly ILogger _logger; /// /// Initializes a new instance of the class. /// /// The TMDb client manager. - /// The logger. - public TmdbMovieSimilarProvider(TmdbClientManager tmdbClientManager, ILogger logger) + public TmdbMovieSimilarProvider(TmdbClientManager tmdbClientManager) { _tmdbClientManager = tmdbClientManager; - _logger = logger; } /// @@ -59,33 +55,15 @@ public class TmdbMovieSimilarProvider : IRemoteSimilarItemsProvider } var providerName = MetadataProvider.Tmdb.ToString(); - var recommendations = _tmdbClientManager + await foreach (var recommendation in _tmdbClientManager .GetMovieRecommendationsAsync(tmdbId, TmdbUtils.GetImageLanguagesParam(string.Empty), cancellationToken) - .GetAsyncEnumerator(cancellationToken); - - await using (recommendations.ConfigureAwait(false)) + .ConfigureAwait(false)) { - while (true) + yield return new SimilarItemReference { - try - { - if (!await recommendations.MoveNextAsync().ConfigureAwait(false)) - { - yield break; - } - } - catch (Exception ex) - { - _logger.LogWarning(ex, "Failed to get recommended movies from TMDb for {TmdbId}", tmdbId); - yield break; - } - - yield return new SimilarItemReference - { - ProviderName = providerName, - ProviderId = recommendations.Current.Id.ToString(CultureInfo.InvariantCulture) - }; - } + ProviderName = providerName, + ProviderId = recommendation.Id.ToString(CultureInfo.InvariantCulture) + }; } } } diff --git a/MediaBrowser.Providers/Plugins/Tmdb/TV/TmdbSeriesSimilarProvider.cs b/MediaBrowser.Providers/Plugins/Tmdb/TV/TmdbSeriesSimilarProvider.cs index c439049dd4..bda5a69604 100644 --- a/MediaBrowser.Providers/Plugins/Tmdb/TV/TmdbSeriesSimilarProvider.cs +++ b/MediaBrowser.Providers/Plugins/Tmdb/TV/TmdbSeriesSimilarProvider.cs @@ -8,7 +8,6 @@ using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Entities; -using Microsoft.Extensions.Logging; namespace MediaBrowser.Providers.Plugins.Tmdb.TV; @@ -18,17 +17,14 @@ namespace MediaBrowser.Providers.Plugins.Tmdb.TV; public class TmdbSeriesSimilarProvider : IRemoteSimilarItemsProvider { private readonly TmdbClientManager _tmdbClientManager; - private readonly ILogger _logger; /// /// Initializes a new instance of the class. /// /// The TMDb client manager. - /// The logger. - public TmdbSeriesSimilarProvider(TmdbClientManager tmdbClientManager, ILogger logger) + public TmdbSeriesSimilarProvider(TmdbClientManager tmdbClientManager) { _tmdbClientManager = tmdbClientManager; - _logger = logger; } /// @@ -59,33 +55,15 @@ public class TmdbSeriesSimilarProvider : IRemoteSimilarItemsProvider } var providerName = MetadataProvider.Tmdb.ToString(); - var recommendations = _tmdbClientManager + await foreach (var recommendation in _tmdbClientManager .GetSeriesRecommendationsAsync(tmdbId, TmdbUtils.GetImageLanguagesParam(string.Empty), cancellationToken) - .GetAsyncEnumerator(cancellationToken); - - await using (recommendations.ConfigureAwait(false)) + .ConfigureAwait(false)) { - while (true) + yield return new SimilarItemReference { - try - { - if (!await recommendations.MoveNextAsync().ConfigureAwait(false)) - { - yield break; - } - } - catch (Exception ex) - { - _logger.LogWarning(ex, "Failed to get recommended TV shows from TMDb for {TmdbId}", tmdbId); - yield break; - } - - yield return new SimilarItemReference - { - ProviderName = providerName, - ProviderId = recommendations.Current.Id.ToString(CultureInfo.InvariantCulture) - }; - } + ProviderName = providerName, + ProviderId = recommendation.Id.ToString(CultureInfo.InvariantCulture) + }; } } } diff --git a/MediaBrowser.Providers/Plugins/Tmdb/TmdbClientManager.cs b/MediaBrowser.Providers/Plugins/Tmdb/TmdbClientManager.cs index db4fa4dd25..3911d860f1 100644 --- a/MediaBrowser.Providers/Plugins/Tmdb/TmdbClientManager.cs +++ b/MediaBrowser.Providers/Plugins/Tmdb/TmdbClientManager.cs @@ -495,7 +495,7 @@ namespace MediaBrowser.Providers.Plugins.Tmdb /// /// Gets the recommended movies for a movie from the TMDb API, fetching further pages as the sequence is consumed. /// - /// /// The TMDb id of the movie. + /// The TMDb id of the movie. /// The language for results. /// The cancellation token. /// The recommended movies. -- cgit v1.2.3