TmdbUtils.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Text.RegularExpressions;
  5. using Jellyfin.Data.Enums;
  6. using MediaBrowser.Model.Entities;
  7. using TMDbLib.Objects.General;
  8. namespace MediaBrowser.Providers.Plugins.Tmdb
  9. {
  10. /// <summary>
  11. /// Utilities for the TMDb provider.
  12. /// </summary>
  13. public static class TmdbUtils
  14. {
  15. private static readonly Regex _nonWords = new(@"[\W_]+", RegexOptions.Compiled);
  16. /// <summary>
  17. /// URL of the TMDb instance to use.
  18. /// </summary>
  19. public const string BaseTmdbUrl = "https://www.themoviedb.org/";
  20. /// <summary>
  21. /// Name of the provider.
  22. /// </summary>
  23. public const string ProviderName = "TheMovieDb";
  24. /// <summary>
  25. /// API key to use when performing an API call.
  26. /// </summary>
  27. public const string ApiKey = "4219e299c89411838049ab0dab19ebd5";
  28. /// <summary>
  29. /// The crew types to keep.
  30. /// </summary>
  31. public static readonly string[] WantedCrewTypes =
  32. {
  33. PersonType.Director,
  34. PersonType.Writer,
  35. PersonType.Producer
  36. };
  37. /// <summary>
  38. /// The crew kinds to keep.
  39. /// </summary>
  40. public static readonly PersonKind[] WantedCrewKinds =
  41. {
  42. PersonKind.Director,
  43. PersonKind.Writer,
  44. PersonKind.Producer
  45. };
  46. /// <summary>
  47. /// Cleans the name according to TMDb requirements.
  48. /// </summary>
  49. /// <param name="name">The name of the entity.</param>
  50. /// <returns>The cleaned name.</returns>
  51. public static string CleanName(string name)
  52. {
  53. // TMDb expects a space separated list of words make sure that is the case
  54. return _nonWords.Replace(name, " ");
  55. }
  56. /// <summary>
  57. /// Maps the TMDb provided roles for crew members to Jellyfin roles.
  58. /// </summary>
  59. /// <param name="crew">Crew member to map against the Jellyfin person types.</param>
  60. /// <returns>The Jellyfin person type.</returns>
  61. public static PersonKind MapCrewToPersonType(Crew crew)
  62. {
  63. if (crew.Department.Equals("production", StringComparison.OrdinalIgnoreCase)
  64. && crew.Job.Contains("director", StringComparison.OrdinalIgnoreCase))
  65. {
  66. return PersonKind.Director;
  67. }
  68. if (crew.Department.Equals("production", StringComparison.OrdinalIgnoreCase)
  69. && crew.Job.Contains("producer", StringComparison.OrdinalIgnoreCase))
  70. {
  71. return PersonKind.Producer;
  72. }
  73. if (crew.Department.Equals("writing", StringComparison.OrdinalIgnoreCase))
  74. {
  75. return PersonKind.Writer;
  76. }
  77. return PersonKind.Unknown;
  78. }
  79. /// <summary>
  80. /// Determines whether a video is a trailer.
  81. /// </summary>
  82. /// <param name="video">The TMDb video.</param>
  83. /// <returns>A boolean indicating whether the video is a trailer.</returns>
  84. public static bool IsTrailerType(Video video)
  85. {
  86. return video.Site.Equals("youtube", StringComparison.OrdinalIgnoreCase)
  87. && (video.Type.Equals("trailer", StringComparison.OrdinalIgnoreCase)
  88. || video.Type.Equals("teaser", StringComparison.OrdinalIgnoreCase));
  89. }
  90. /// <summary>
  91. /// Normalizes a language string for use with TMDb's include image language parameter.
  92. /// </summary>
  93. /// <param name="preferredLanguage">The preferred language as either a 2 letter code with or without country code.</param>
  94. /// <returns>The comma separated language string.</returns>
  95. public static string GetImageLanguagesParam(string preferredLanguage)
  96. {
  97. var languages = new List<string>();
  98. if (!string.IsNullOrEmpty(preferredLanguage))
  99. {
  100. preferredLanguage = NormalizeLanguage(preferredLanguage);
  101. languages.Add(preferredLanguage);
  102. if (preferredLanguage.Length == 5) // Like en-US
  103. {
  104. // Currently, TMDb supports 2-letter language codes only.
  105. // They are planning to change this in the future, thus we're
  106. // supplying both codes if we're having a 5-letter code.
  107. languages.Add(preferredLanguage.Substring(0, 2));
  108. }
  109. }
  110. languages.Add("null");
  111. // Always add English as fallback language
  112. if (!string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase))
  113. {
  114. languages.Add("en");
  115. }
  116. return string.Join(',', languages);
  117. }
  118. /// <summary>
  119. /// Normalizes a language string for use with TMDb's language parameter.
  120. /// </summary>
  121. /// <param name="language">The language code.</param>
  122. /// <returns>The normalized language code.</returns>
  123. [return: NotNullIfNotNull(nameof(language))]
  124. public static string? NormalizeLanguage(string? language)
  125. {
  126. if (string.IsNullOrEmpty(language))
  127. {
  128. return language;
  129. }
  130. // TMDb requires this to be uppercase
  131. // Everything after the hyphen must be written in uppercase due to a way TMDb wrote their API.
  132. // See here: https://www.themoviedb.org/talk/5119221d760ee36c642af4ad?page=3#56e372a0c3a3685a9e0019ab
  133. var parts = language.Split('-');
  134. if (parts.Length == 2)
  135. {
  136. // TMDb doesn't support Switzerland (de-CH, it-CH or fr-CH) so use the language (de, it or fr) without country code
  137. if (string.Equals(parts[1], "CH", StringComparison.OrdinalIgnoreCase))
  138. {
  139. return parts[0];
  140. }
  141. language = parts[0] + "-" + parts[1].ToUpperInvariant();
  142. }
  143. return language;
  144. }
  145. /// <summary>
  146. /// Adjusts the image's language code preferring the 5 letter language code eg. en-US.
  147. /// </summary>
  148. /// <param name="imageLanguage">The image's actual language code.</param>
  149. /// <param name="requestLanguage">The requested language code.</param>
  150. /// <returns>The language code.</returns>
  151. public static string AdjustImageLanguage(string imageLanguage, string requestLanguage)
  152. {
  153. if (!string.IsNullOrEmpty(imageLanguage)
  154. && !string.IsNullOrEmpty(requestLanguage)
  155. && requestLanguage.Length > 2
  156. && imageLanguage.Length == 2
  157. && requestLanguage.StartsWith(imageLanguage, StringComparison.OrdinalIgnoreCase))
  158. {
  159. return requestLanguage;
  160. }
  161. return imageLanguage;
  162. }
  163. /// <summary>
  164. /// Combines the metadata country code and the parental rating from the API into the value we store in our database.
  165. /// </summary>
  166. /// <param name="countryCode">The ISO 3166-1 country code of the rating country.</param>
  167. /// <param name="ratingValue">The rating value returned by the TMDb API.</param>
  168. /// <returns>The combined parental rating of country code+rating value.</returns>
  169. public static string BuildParentalRating(string countryCode, string ratingValue)
  170. {
  171. // Exclude US because we store US values as TV-14 without the country code.
  172. var ratingPrefix = string.Equals(countryCode, "US", StringComparison.OrdinalIgnoreCase) ? string.Empty : countryCode + "-";
  173. var newRating = ratingPrefix + ratingValue;
  174. return newRating.Replace("DE-", "FSK-", StringComparison.OrdinalIgnoreCase);
  175. }
  176. }
  177. }