TmdbUtils.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  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. /// <summary>
  14. /// URL of the TMDB instance to use.
  15. /// </summary>
  16. public const string BaseTmdbUrl = "https://www.themoviedb.org/";
  17. /// <summary>
  18. /// Name of the provider.
  19. /// </summary>
  20. public const string ProviderName = "TheMovieDb";
  21. /// <summary>
  22. /// API key to use when performing an API call.
  23. /// </summary>
  24. public const string ApiKey = "4219e299c89411838049ab0dab19ebd5";
  25. /// <summary>
  26. /// Maximum number of cast members to pull.
  27. /// </summary>
  28. public const int MaxCastMembers = 15;
  29. /// <summary>
  30. /// The crew types to keep.
  31. /// </summary>
  32. public static readonly string[] WantedCrewTypes =
  33. {
  34. PersonType.Director,
  35. PersonType.Writer,
  36. PersonType.Producer
  37. };
  38. /// <summary>
  39. /// Maps the TMDB provided roles for crew members to Jellyfin roles.
  40. /// </summary>
  41. /// <param name="crew">Crew member to map against the Jellyfin person types.</param>
  42. /// <returns>The Jellyfin person type.</returns>
  43. public static string MapCrewToPersonType(Crew crew)
  44. {
  45. if (crew.Department.Equals("production", StringComparison.InvariantCultureIgnoreCase)
  46. && crew.Job.Contains("director", StringComparison.InvariantCultureIgnoreCase))
  47. {
  48. return PersonType.Director;
  49. }
  50. if (crew.Department.Equals("production", StringComparison.InvariantCultureIgnoreCase)
  51. && crew.Job.Contains("producer", StringComparison.InvariantCultureIgnoreCase))
  52. {
  53. return PersonType.Producer;
  54. }
  55. if (crew.Department.Equals("writing", StringComparison.InvariantCultureIgnoreCase))
  56. {
  57. return PersonType.Writer;
  58. }
  59. return string.Empty;
  60. }
  61. /// <summary>
  62. /// Determines whether a video is a trailer.
  63. /// </summary>
  64. /// <param name="video">The TMDb video.</param>
  65. /// <returns>A boolean indicating whether the video is a trailer.</returns>
  66. public static bool IsTrailerType(Video video)
  67. {
  68. return video.Site.Equals("youtube", StringComparison.OrdinalIgnoreCase)
  69. && (!video.Type.Equals("trailer", StringComparison.OrdinalIgnoreCase)
  70. || !video.Type.Equals("teaser", StringComparison.OrdinalIgnoreCase));
  71. }
  72. /// <summary>
  73. /// Normalizes a language string for use with TMDb's include image language parameter.
  74. /// </summary>
  75. /// <param name="preferredLanguage">The preferred language as either a 2 letter code with or without country code.</param>
  76. /// <returns>The comma separated language string.</returns>
  77. public static string GetImageLanguagesParam(string preferredLanguage)
  78. {
  79. var languages = new List<string>();
  80. if (!string.IsNullOrEmpty(preferredLanguage))
  81. {
  82. preferredLanguage = NormalizeLanguage(preferredLanguage);
  83. languages.Add(preferredLanguage);
  84. if (preferredLanguage.Length == 5) // like en-US
  85. {
  86. // Currently, TMDB supports 2-letter language codes only
  87. // They are planning to change this in the future, thus we're
  88. // supplying both codes if we're having a 5-letter code.
  89. languages.Add(preferredLanguage.Substring(0, 2));
  90. }
  91. }
  92. languages.Add("null");
  93. if (!string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase))
  94. {
  95. languages.Add("en");
  96. }
  97. return string.Join(',', languages);
  98. }
  99. /// <summary>
  100. /// Normalizes a language string for use with TMDb's language parameter.
  101. /// </summary>
  102. /// <param name="language">The language code.</param>
  103. /// <returns>The normalized language code.</returns>
  104. public static string NormalizeLanguage(string language)
  105. {
  106. if (string.IsNullOrEmpty(language))
  107. {
  108. return language;
  109. }
  110. // They require this to be uppercase
  111. // Everything after the hyphen must be written in uppercase due to a way TMDB wrote their api.
  112. // See here: https://www.themoviedb.org/talk/5119221d760ee36c642af4ad?page=3#56e372a0c3a3685a9e0019ab
  113. var parts = language.Split('-');
  114. if (parts.Length == 2)
  115. {
  116. language = parts[0] + "-" + parts[1].ToUpperInvariant();
  117. }
  118. return language;
  119. }
  120. /// <summary>
  121. /// Adjusts the image's language code preferring the 5 letter language code eg. en-US.
  122. /// </summary>
  123. /// <param name="imageLanguage">The image's actual language code.</param>
  124. /// <param name="requestLanguage">The requested language code.</param>
  125. /// <returns>The language code.</returns>
  126. public static string AdjustImageLanguage(string imageLanguage, string requestLanguage)
  127. {
  128. if (!string.IsNullOrEmpty(imageLanguage)
  129. && !string.IsNullOrEmpty(requestLanguage)
  130. && requestLanguage.Length > 2
  131. && imageLanguage.Length == 2
  132. && requestLanguage.StartsWith(imageLanguage, StringComparison.OrdinalIgnoreCase))
  133. {
  134. return requestLanguage;
  135. }
  136. return imageLanguage;
  137. }
  138. }
  139. }