2019-08-18 11:20:52 +00:00
|
|
|
using System;
|
2020-09-24 17:49:35 +00:00
|
|
|
using System.Collections.Generic;
|
2021-03-17 09:29:45 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2019-08-18 11:20:52 +00:00
|
|
|
using MediaBrowser.Model.Entities;
|
2020-09-24 17:49:35 +00:00
|
|
|
using TMDbLib.Objects.General;
|
2019-08-18 11:20:52 +00:00
|
|
|
|
2020-05-31 06:23:09 +00:00
|
|
|
namespace MediaBrowser.Providers.Plugins.Tmdb
|
2019-08-18 11:20:52 +00:00
|
|
|
{
|
2020-05-12 13:05:58 +00:00
|
|
|
/// <summary>
|
2020-05-31 06:28:01 +00:00
|
|
|
/// Utilities for the TMDb provider.
|
2020-05-12 13:05:58 +00:00
|
|
|
/// </summary>
|
2019-08-18 11:20:52 +00:00
|
|
|
public static class TmdbUtils
|
|
|
|
{
|
2021-12-24 17:28:27 +00:00
|
|
|
private static readonly Regex _nonWords = new(@"[\W_]+", RegexOptions.Compiled);
|
2021-03-17 09:29:45 +00:00
|
|
|
|
2020-05-12 13:05:58 +00:00
|
|
|
/// <summary>
|
|
|
|
/// URL of the TMDB instance to use.
|
|
|
|
/// </summary>
|
2019-08-18 11:34:44 +00:00
|
|
|
public const string BaseTmdbUrl = "https://www.themoviedb.org/";
|
2020-05-12 13:05:58 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Name of the provider.
|
|
|
|
/// </summary>
|
2019-08-18 11:20:52 +00:00
|
|
|
public const string ProviderName = "TheMovieDb";
|
2020-05-12 13:05:58 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// API key to use when performing an API call.
|
|
|
|
/// </summary>
|
2019-08-18 11:20:52 +00:00
|
|
|
public const string ApiKey = "4219e299c89411838049ab0dab19ebd5";
|
2020-05-12 13:05:58 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The crew types to keep.
|
|
|
|
/// </summary>
|
|
|
|
public static readonly string[] WantedCrewTypes =
|
|
|
|
{
|
|
|
|
PersonType.Director,
|
|
|
|
PersonType.Writer,
|
|
|
|
PersonType.Producer
|
|
|
|
};
|
|
|
|
|
2021-03-17 09:29:45 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Cleans the name according to TMDb requirements.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The name of the entity.</param>
|
|
|
|
/// <returns>The cleaned name.</returns>
|
|
|
|
public static string CleanName(string name)
|
|
|
|
{
|
|
|
|
// TMDb expects a space separated list of words make sure that is the case
|
|
|
|
return _nonWords.Replace(name, " ");
|
|
|
|
}
|
|
|
|
|
2020-05-12 13:05:58 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Maps the TMDB provided roles for crew members to Jellyfin roles.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="crew">Crew member to map against the Jellyfin person types.</param>
|
|
|
|
/// <returns>The Jellyfin person type.</returns>
|
2019-08-18 11:20:52 +00:00
|
|
|
public static string MapCrewToPersonType(Crew crew)
|
|
|
|
{
|
2021-05-01 13:59:21 +00:00
|
|
|
if (crew.Department.Equals("production", StringComparison.OrdinalIgnoreCase)
|
|
|
|
&& crew.Job.Contains("director", StringComparison.OrdinalIgnoreCase))
|
2020-05-10 13:16:19 +00:00
|
|
|
{
|
|
|
|
return PersonType.Director;
|
|
|
|
}
|
|
|
|
|
2021-05-01 13:59:21 +00:00
|
|
|
if (crew.Department.Equals("production", StringComparison.OrdinalIgnoreCase)
|
|
|
|
&& crew.Job.Contains("producer", StringComparison.OrdinalIgnoreCase))
|
2019-08-18 11:20:52 +00:00
|
|
|
{
|
|
|
|
return PersonType.Producer;
|
|
|
|
}
|
|
|
|
|
2021-05-01 13:59:21 +00:00
|
|
|
if (crew.Department.Equals("writing", StringComparison.OrdinalIgnoreCase))
|
2019-08-18 11:20:52 +00:00
|
|
|
{
|
|
|
|
return PersonType.Writer;
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:31:57 +00:00
|
|
|
return string.Empty;
|
2019-08-18 11:20:52 +00:00
|
|
|
}
|
2020-09-24 17:49:35 +00:00
|
|
|
|
2020-09-24 18:31:57 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Determines whether a video is a trailer.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="video">The TMDb video.</param>
|
|
|
|
/// <returns>A boolean indicating whether the video is a trailer.</returns>
|
2020-09-24 17:49:35 +00:00
|
|
|
public static bool IsTrailerType(Video video)
|
|
|
|
{
|
|
|
|
return video.Site.Equals("youtube", StringComparison.OrdinalIgnoreCase)
|
|
|
|
&& (!video.Type.Equals("trailer", StringComparison.OrdinalIgnoreCase)
|
|
|
|
|| !video.Type.Equals("teaser", StringComparison.OrdinalIgnoreCase));
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:31:57 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Normalizes a language string for use with TMDb's include image language parameter.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="preferredLanguage">The preferred language as either a 2 letter code with or without country code.</param>
|
|
|
|
/// <returns>The comma separated language string.</returns>
|
2020-09-24 17:49:35 +00:00
|
|
|
public static string GetImageLanguagesParam(string preferredLanguage)
|
|
|
|
{
|
|
|
|
var languages = new List<string>();
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(preferredLanguage))
|
|
|
|
{
|
|
|
|
preferredLanguage = NormalizeLanguage(preferredLanguage);
|
|
|
|
|
|
|
|
languages.Add(preferredLanguage);
|
|
|
|
|
|
|
|
if (preferredLanguage.Length == 5) // like en-US
|
|
|
|
{
|
2020-11-18 13:46:14 +00:00
|
|
|
// Currently, TMDB supports 2-letter language codes only
|
2020-09-24 17:49:35 +00:00
|
|
|
// They are planning to change this in the future, thus we're
|
|
|
|
// supplying both codes if we're having a 5-letter code.
|
|
|
|
languages.Add(preferredLanguage.Substring(0, 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
languages.Add("null");
|
|
|
|
|
|
|
|
if (!string.Equals(preferredLanguage, "en", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
languages.Add("en");
|
|
|
|
}
|
|
|
|
|
|
|
|
return string.Join(',', languages);
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:31:57 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Normalizes a language string for use with TMDb's language parameter.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="language">The language code.</param>
|
|
|
|
/// <returns>The normalized language code.</returns>
|
2020-09-24 17:49:35 +00:00
|
|
|
public static string NormalizeLanguage(string language)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(language))
|
|
|
|
{
|
|
|
|
return language;
|
|
|
|
}
|
|
|
|
|
|
|
|
// They require this to be uppercase
|
|
|
|
// Everything after the hyphen must be written in uppercase due to a way TMDB wrote their api.
|
|
|
|
// See here: https://www.themoviedb.org/talk/5119221d760ee36c642af4ad?page=3#56e372a0c3a3685a9e0019ab
|
|
|
|
var parts = language.Split('-');
|
|
|
|
|
|
|
|
if (parts.Length == 2)
|
|
|
|
{
|
2021-05-12 12:48:45 +00:00
|
|
|
// TMDB doesn't support Switzerland (de-CH, it-CH or fr-CH) so use the language (de, it or fr) without country code
|
2021-05-17 10:55:27 +00:00
|
|
|
if (string.Equals(parts[1], "CH", StringComparison.OrdinalIgnoreCase))
|
2021-05-12 12:48:45 +00:00
|
|
|
{
|
|
|
|
return parts[0];
|
|
|
|
}
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
language = parts[0] + "-" + parts[1].ToUpperInvariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
return language;
|
|
|
|
}
|
|
|
|
|
2020-09-24 18:31:57 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Adjusts the image's language code preferring the 5 letter language code eg. en-US.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="imageLanguage">The image's actual language code.</param>
|
|
|
|
/// <param name="requestLanguage">The requested language code.</param>
|
|
|
|
/// <returns>The language code.</returns>
|
2020-09-24 17:49:35 +00:00
|
|
|
public static string AdjustImageLanguage(string imageLanguage, string requestLanguage)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(imageLanguage)
|
|
|
|
&& !string.IsNullOrEmpty(requestLanguage)
|
|
|
|
&& requestLanguage.Length > 2
|
|
|
|
&& imageLanguage.Length == 2
|
|
|
|
&& requestLanguage.StartsWith(imageLanguage, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
return requestLanguage;
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageLanguage;
|
|
|
|
}
|
2021-05-10 18:51:41 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Combines the metadata country code and the parental rating from the Api into the value we store in our database.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="countryCode">The Iso 3166-1 country code of the rating country.</param>
|
|
|
|
/// <param name="ratingValue">The rating value returned by the Tmdb Api.</param>
|
|
|
|
/// <returns>The combined parental rating of country code+rating value.</returns>
|
|
|
|
public static string BuildParentalRating(string countryCode, string ratingValue)
|
|
|
|
{
|
|
|
|
// exclude US because we store us values as TV-14 without the country code.
|
|
|
|
var ratingPrefix = string.Equals(countryCode, "US", StringComparison.OrdinalIgnoreCase) ? string.Empty : countryCode + "-";
|
|
|
|
var newRating = ratingPrefix + ratingValue;
|
|
|
|
|
|
|
|
return newRating.Replace("DE-", "FSK-", StringComparison.OrdinalIgnoreCase);
|
|
|
|
}
|
2019-08-18 11:20:52 +00:00
|
|
|
}
|
|
|
|
}
|