2
0

TmdbUtils.cs 7.4 KB

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