2
0

TmdbUtils.cs 7.5 KB

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