TmdbUtils.cs 7.5 KB

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