diff options
Diffstat (limited to 'MediaBrowser.Providers/Plugins/Tmdb/TmdbUtils.cs')
| -rw-r--r-- | MediaBrowser.Providers/Plugins/Tmdb/TmdbUtils.cs | 118 |
1 files changed, 108 insertions, 10 deletions
diff --git a/MediaBrowser.Providers/Plugins/Tmdb/TmdbUtils.cs b/MediaBrowser.Providers/Plugins/Tmdb/TmdbUtils.cs index 1415d6976..b754a0795 100644 --- a/MediaBrowser.Providers/Plugins/Tmdb/TmdbUtils.cs +++ b/MediaBrowser.Providers/Plugins/Tmdb/TmdbUtils.cs @@ -1,7 +1,9 @@ +#nullable enable + using System; -using System.Net.Mime; +using System.Collections.Generic; using MediaBrowser.Model.Entities; -using MediaBrowser.Providers.Plugins.Tmdb.Models.General; +using TMDbLib.Objects.General; namespace MediaBrowser.Providers.Plugins.Tmdb { @@ -16,11 +18,6 @@ namespace MediaBrowser.Providers.Plugins.Tmdb public const string BaseTmdbUrl = "https://www.themoviedb.org/"; /// <summary> - /// URL of the TMDB API instance to use. - /// </summary> - public const string BaseTmdbApiUrl = "https://api.themoviedb.org/"; - - /// <summary> /// Name of the provider. /// </summary> public const string ProviderName = "TheMovieDb"; @@ -31,9 +28,19 @@ namespace MediaBrowser.Providers.Plugins.Tmdb public const string ApiKey = "4219e299c89411838049ab0dab19ebd5"; /// <summary> - /// Value of the Accept header for requests to the provider. + /// Maximum number of cast members to pull. + /// </summary> + public const int MaxCastMembers = 15; + + /// <summary> + /// The crew types to keep. /// </summary> - public static readonly string[] AcceptHeaders = { MediaTypeNames.Application.Json, "image/*" }; + public static readonly string[] WantedCrewTypes = + { + PersonType.Director, + PersonType.Writer, + PersonType.Producer + }; /// <summary> /// Maps the TMDB provided roles for crew members to Jellyfin roles. @@ -59,7 +66,98 @@ namespace MediaBrowser.Providers.Plugins.Tmdb return PersonType.Writer; } - return null; + return string.Empty; + } + + /// <summary> + /// Determines whether a video is a trailer. + /// </summary> + /// <param name="video">The TMDb video.</param> + /// <returns>A boolean indicating whether the video is a trailer.</returns> + public static bool IsTrailerType(Video video) + { + return video.Site.Equals("youtube", StringComparison.OrdinalIgnoreCase) + && (!video.Type.Equals("trailer", StringComparison.OrdinalIgnoreCase) + || !video.Type.Equals("teaser", StringComparison.OrdinalIgnoreCase)); + } + + /// <summary> + /// Normalizes a language string for use with TMDb's include image language parameter. + /// </summary> + /// <param name="preferredLanguage">The preferred language as either a 2 letter code with or without country code.</param> + /// <returns>The comma separated language string.</returns> + public static string GetImageLanguagesParam(string preferredLanguage) + { + var languages = new List<string>(); + + if (!string.IsNullOrEmpty(preferredLanguage)) + { + preferredLanguage = NormalizeLanguage(preferredLanguage); + + languages.Add(preferredLanguage); + + if (preferredLanguage.Length == 5) // like en-US + { + // Currenty, TMDB supports 2-letter language codes only + // They are planning to change this in the future, thus we're + // supplying both codes if we're having a 5-letter code. + languages.Add(preferredLanguage.Substring(0, 2)); + } + } + + languages.Add("null"); + + if (!string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase)) + { + languages.Add("en"); + } + + return string.Join(',', languages); + } + + /// <summary> + /// Normalizes a language string for use with TMDb's language parameter. + /// </summary> + /// <param name="language">The language code.</param> + /// <returns>The normalized language code.</returns> + public static string NormalizeLanguage(string language) + { + if (string.IsNullOrEmpty(language)) + { + return language; + } + + // They require this to be uppercase + // Everything after the hyphen must be written in uppercase due to a way TMDB wrote their api. + // See here: https://www.themoviedb.org/talk/5119221d760ee36c642af4ad?page=3#56e372a0c3a3685a9e0019ab + var parts = language.Split('-'); + + if (parts.Length == 2) + { + language = parts[0] + "-" + parts[1].ToUpperInvariant(); + } + + return language; + } + + /// <summary> + /// Adjusts the image's language code preferring the 5 letter language code eg. en-US. + /// </summary> + /// <param name="imageLanguage">The image's actual language code.</param> + /// <param name="requestLanguage">The requested language code.</param> + /// <returns>The language code.</returns> + public static string AdjustImageLanguage(string imageLanguage, string requestLanguage) + { + if (!string.IsNullOrEmpty(imageLanguage) + && !string.IsNullOrEmpty(requestLanguage) + && requestLanguage.Length > 2 + && imageLanguage.Length == 2 + && requestLanguage.StartsWith(imageLanguage, StringComparison.OrdinalIgnoreCase)) + { + return requestLanguage; + } + + return imageLanguage; } } } |
