TmdbUtils.cs 7.5 KB

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