TmdbUtils.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. if (!string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase))
  101. {
  102. languages.Add("en");
  103. }
  104. return string.Join(',', languages);
  105. }
  106. /// <summary>
  107. /// Normalizes a language string for use with TMDb's language parameter.
  108. /// </summary>
  109. /// <param name="language">The language code.</param>
  110. /// <returns>The normalized language code.</returns>
  111. public static string NormalizeLanguage(string language)
  112. {
  113. if (string.IsNullOrEmpty(language))
  114. {
  115. return language;
  116. }
  117. // They require this to be uppercase
  118. // Everything after the hyphen must be written in uppercase due to a way TMDB wrote their api.
  119. // See here: https://www.themoviedb.org/talk/5119221d760ee36c642af4ad?page=3#56e372a0c3a3685a9e0019ab
  120. var parts = language.Split('-');
  121. if (parts.Length == 2)
  122. {
  123. // TMDB doesn't support Switzerland (de-CH, it-CH or fr-CH) so use the language (de, it or fr) without country code
  124. if (string.Equals(parts[1], "CH", StringComparison.OrdinalIgnoreCase))
  125. {
  126. return parts[0];
  127. }
  128. language = parts[0] + "-" + parts[1].ToUpperInvariant();
  129. }
  130. return language;
  131. }
  132. /// <summary>
  133. /// Adjusts the image's language code preferring the 5 letter language code eg. en-US.
  134. /// </summary>
  135. /// <param name="imageLanguage">The image's actual language code.</param>
  136. /// <param name="requestLanguage">The requested language code.</param>
  137. /// <returns>The language code.</returns>
  138. public static string AdjustImageLanguage(string imageLanguage, string requestLanguage)
  139. {
  140. if (!string.IsNullOrEmpty(imageLanguage)
  141. && !string.IsNullOrEmpty(requestLanguage)
  142. && requestLanguage.Length > 2
  143. && imageLanguage.Length == 2
  144. && requestLanguage.StartsWith(imageLanguage, StringComparison.OrdinalIgnoreCase))
  145. {
  146. return requestLanguage;
  147. }
  148. return imageLanguage;
  149. }
  150. /// <summary>
  151. /// Combines the metadata country code and the parental rating from the Api into the value we store in our database.
  152. /// </summary>
  153. /// <param name="countryCode">The Iso 3166-1 country code of the rating country.</param>
  154. /// <param name="ratingValue">The rating value returned by the Tmdb Api.</param>
  155. /// <returns>The combined parental rating of country code+rating value.</returns>
  156. public static string BuildParentalRating(string countryCode, string ratingValue)
  157. {
  158. // exclude US because we store us values as TV-14 without the country code.
  159. var ratingPrefix = string.Equals(countryCode, "US", StringComparison.OrdinalIgnoreCase) ? string.Empty : countryCode + "-";
  160. var newRating = ratingPrefix + ratingValue;
  161. return newRating.Replace("DE-", "FSK-", StringComparison.OrdinalIgnoreCase);
  162. }
  163. }
  164. }