2020-06-19 18:24:13 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2013-02-21 01:33:05 +00:00
|
|
|
using System.Collections.Generic;
|
2014-03-02 17:09:35 +00:00
|
|
|
using System.Globalization;
|
2020-09-24 17:49:35 +00:00
|
|
|
using System.Linq;
|
2020-02-23 09:53:51 +00:00
|
|
|
using System.Net.Http;
|
2013-02-21 01:33:05 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-08-31 17:05:21 +00:00
|
|
|
using MediaBrowser.Common.Net;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Entities.Movies;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Providers;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2020-05-31 06:23:09 +00:00
|
|
|
namespace MediaBrowser.Providers.Plugins.Tmdb.Movies
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Class MovieDbProvider.
|
2013-02-21 01:33:05 +00:00
|
|
|
/// </summary>
|
2019-08-18 11:20:52 +00:00
|
|
|
public class TmdbMovieProvider : IRemoteMetadataProvider<Movie, MovieInfo>, IHasOrder
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2020-08-17 19:10:02 +00:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2014-11-18 02:48:22 +00:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
2020-09-24 17:49:35 +00:00
|
|
|
private readonly TmdbClientManager _tmdbClientManager;
|
|
|
|
|
2020-02-23 09:53:51 +00:00
|
|
|
public TmdbMovieProvider(
|
|
|
|
ILibraryManager libraryManager,
|
2020-09-24 17:49:35 +00:00
|
|
|
TmdbClientManager tmdbClientManager,
|
|
|
|
IHttpClientFactory httpClientFactory)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2014-11-18 02:48:22 +00:00
|
|
|
_libraryManager = libraryManager;
|
2020-09-24 17:49:35 +00:00
|
|
|
_tmdbClientManager = tmdbClientManager;
|
|
|
|
_httpClientFactory = httpClientFactory;
|
2013-03-04 05:43:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:56:11 +00:00
|
|
|
public string Name => TmdbUtils.ProviderName;
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public int Order => 1;
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(MovieInfo searchInfo, CancellationToken cancellationToken)
|
2014-03-02 17:09:35 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
var tmdbId = Convert.ToInt32(searchInfo.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
|
2014-03-02 17:09:35 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (tmdbId == 0)
|
2014-03-02 17:09:35 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
var movieResults = await _tmdbClientManager
|
2021-02-18 21:54:12 +00:00
|
|
|
.SearchMovieAsync(searchInfo.Name, searchInfo.Year ?? 0, searchInfo.MetadataLanguage, cancellationToken)
|
2020-09-24 17:49:35 +00:00
|
|
|
.ConfigureAwait(false);
|
2021-02-18 21:54:12 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
var remoteSearchResults = new List<RemoteSearchResult>();
|
|
|
|
for (var i = 0; i < movieResults.Count; i++)
|
2014-03-02 17:09:35 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
var movieResult = movieResults[i];
|
|
|
|
var remoteSearchResult = new RemoteSearchResult
|
2014-03-02 18:01:46 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
Name = movieResult.Title ?? movieResult.OriginalTitle,
|
|
|
|
ImageUrl = _tmdbClientManager.GetPosterUrl(movieResult.PosterPath),
|
|
|
|
Overview = movieResult.Overview,
|
|
|
|
SearchProviderName = Name
|
|
|
|
};
|
|
|
|
|
|
|
|
var releaseDate = movieResult.ReleaseDate?.ToUniversalTime();
|
|
|
|
remoteSearchResult.PremiereDate = releaseDate;
|
|
|
|
remoteSearchResult.ProductionYear = releaseDate?.Year;
|
|
|
|
|
|
|
|
remoteSearchResult.SetProviderId(MetadataProvider.Tmdb, movieResult.Id.ToString(CultureInfo.InvariantCulture));
|
|
|
|
remoteSearchResults.Add(remoteSearchResult);
|
2014-03-02 18:01:46 +00:00
|
|
|
}
|
2014-07-05 03:17:24 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
return remoteSearchResults;
|
2014-03-02 17:09:35 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
var movie = await _tmdbClientManager
|
|
|
|
.GetMovieAsync(tmdbId, searchInfo.MetadataLanguage, TmdbUtils.GetImageLanguagesParam(searchInfo.MetadataLanguage), cancellationToken)
|
|
|
|
.ConfigureAwait(false);
|
2013-02-24 21:53:54 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
var remoteResult = new RemoteSearchResult
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
Name = movie.Title ?? movie.OriginalTitle,
|
|
|
|
SearchProviderName = Name,
|
|
|
|
ImageUrl = _tmdbClientManager.GetPosterUrl(movie.PosterPath),
|
|
|
|
Overview = movie.Overview
|
|
|
|
};
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (movie.ReleaseDate != null)
|
2020-08-17 19:59:29 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
var releaseDate = movie.ReleaseDate.Value.ToUniversalTime();
|
|
|
|
remoteResult.PremiereDate = releaseDate;
|
|
|
|
remoteResult.ProductionYear = releaseDate.Year;
|
2020-08-17 19:59:29 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
remoteResult.SetProviderId(MetadataProvider.Tmdb, movie.Id.ToString(CultureInfo.InvariantCulture));
|
2013-10-14 17:34:54 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(movie.ImdbId))
|
2020-06-20 08:35:29 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
remoteResult.SetProviderId(MetadataProvider.Imdb, movie.ImdbId);
|
2020-06-20 08:35:29 +00:00
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
return new[] { remoteResult };
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, CancellationToken cancellationToken)
|
2014-01-10 13:52:01 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
var tmdbId = info.GetProviderId(MetadataProvider.Tmdb);
|
|
|
|
var imdbId = info.GetProviderId(MetadataProvider.Imdb);
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (string.IsNullOrEmpty(tmdbId) && string.IsNullOrEmpty(imdbId))
|
2014-01-10 13:52:01 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
// ParseName is required here.
|
|
|
|
// Caller provides the filename with extension stripped and NOT the parsed filename
|
|
|
|
var parsedName = _libraryManager.ParseName(info.Name);
|
|
|
|
var searchResults = await _tmdbClientManager.SearchMovieAsync(parsedName.Name, parsedName.Year ?? 0, info.MetadataLanguage, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (searchResults.Count > 0)
|
2014-01-10 13:52:01 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
tmdbId = searchResults[0].Id.ToString(CultureInfo.InvariantCulture);
|
2014-01-10 13:52:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
2013-10-14 17:34:54 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
return new MetadataResult<Movie>();
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
2013-10-14 17:34:54 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
var movieResult = await _tmdbClientManager
|
|
|
|
.GetMovieAsync(Convert.ToInt32(tmdbId, CultureInfo.InvariantCulture), info.MetadataLanguage, TmdbUtils.GetImageLanguagesParam(info.MetadataLanguage), cancellationToken)
|
|
|
|
.ConfigureAwait(false);
|
2013-10-14 17:34:54 +00:00
|
|
|
|
2020-11-22 13:11:05 +00:00
|
|
|
if (movieResult == null)
|
|
|
|
{
|
|
|
|
return new MetadataResult<Movie>();
|
|
|
|
}
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
var movie = new Movie
|
|
|
|
{
|
|
|
|
Name = movieResult.Title ?? movieResult.OriginalTitle,
|
|
|
|
Overview = movieResult.Overview?.Replace("\n\n", "\n", StringComparison.InvariantCulture),
|
|
|
|
Tagline = movieResult.Tagline,
|
|
|
|
ProductionLocations = movieResult.ProductionCountries.Select(pc => pc.Name).ToArray()
|
|
|
|
};
|
|
|
|
var metadataResult = new MetadataResult<Movie>
|
|
|
|
{
|
|
|
|
HasMetadata = true,
|
|
|
|
ResultLanguage = info.MetadataLanguage,
|
|
|
|
Item = movie
|
|
|
|
};
|
|
|
|
|
|
|
|
movie.SetProviderId(MetadataProvider.Tmdb, tmdbId);
|
|
|
|
movie.SetProviderId(MetadataProvider.Imdb, movieResult.ImdbId);
|
|
|
|
if (movieResult.BelongsToCollection != null)
|
2016-03-04 17:38:51 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
movie.SetProviderId(MetadataProvider.TmdbCollection, movieResult.BelongsToCollection.Id.ToString(CultureInfo.InvariantCulture));
|
|
|
|
movie.CollectionName = movieResult.BelongsToCollection.Name;
|
2016-03-04 17:38:51 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
movie.CommunityRating = Convert.ToSingle(movieResult.VoteAverage);
|
2013-12-28 16:58:13 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (movieResult.Releases?.Countries != null)
|
|
|
|
{
|
|
|
|
var releases = movieResult.Releases.Countries.Where(i => !string.IsNullOrWhiteSpace(i.Certification)).ToList();
|
2013-11-01 15:55:25 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
var ourRelease = releases.FirstOrDefault(c => string.Equals(c.Iso_3166_1, info.MetadataCountryCode, StringComparison.OrdinalIgnoreCase));
|
|
|
|
var usRelease = releases.FirstOrDefault(c => string.Equals(c.Iso_3166_1, "US", StringComparison.OrdinalIgnoreCase));
|
2014-10-06 23:30:05 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (ourRelease != null)
|
|
|
|
{
|
|
|
|
var ratingPrefix = string.Equals(info.MetadataCountryCode, "us", StringComparison.OrdinalIgnoreCase) ? string.Empty : info.MetadataCountryCode + "-";
|
|
|
|
var newRating = ratingPrefix + ourRelease.Certification;
|
2016-06-24 23:53:38 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
newRating = newRating.Replace("de-", "FSK-", StringComparison.OrdinalIgnoreCase);
|
2016-06-24 23:53:38 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
movie.OfficialRating = newRating;
|
|
|
|
}
|
|
|
|
else if (usRelease != null)
|
2016-06-24 23:53:38 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
movie.OfficialRating = usRelease.Certification;
|
2016-06-24 23:53:38 +00:00
|
|
|
}
|
2014-10-06 23:30:05 +00:00
|
|
|
}
|
2016-06-24 23:53:38 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
movie.PremiereDate = movieResult.ReleaseDate;
|
|
|
|
movie.ProductionYear = movieResult.ReleaseDate?.Year;
|
2016-06-24 23:53:38 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (movieResult.ProductionCompanies != null)
|
2014-10-06 23:30:05 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
movie.SetStudios(movieResult.ProductionCompanies.Select(c => c.Name));
|
2014-10-06 23:30:05 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
var genres = movieResult.Genres;
|
2014-10-06 23:30:05 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
foreach (var genre in genres.Select(g => g.Name))
|
2016-03-12 15:14:17 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
movie.AddGenre(genre);
|
2016-03-12 15:14:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 09:05:46 +00:00
|
|
|
if (movieResult.Keywords?.Keywords != null)
|
2020-09-10 09:05:46 +00:00
|
|
|
{
|
|
|
|
for (var i = 0; i < movieResult.Keywords.Keywords.Count; i++)
|
|
|
|
{
|
|
|
|
movie.AddTag(movieResult.Keywords.Keywords[i].Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (movieResult.Credits?.Cast != null)
|
2016-06-24 23:53:38 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
// TODO configurable
|
|
|
|
foreach (var actor in movieResult.Credits.Cast.OrderBy(a => a.Order).Take(TmdbUtils.MaxCastMembers))
|
|
|
|
{
|
|
|
|
var personInfo = new PersonInfo
|
|
|
|
{
|
|
|
|
Name = actor.Name.Trim(),
|
|
|
|
Role = actor.Character,
|
|
|
|
Type = PersonType.Actor,
|
|
|
|
SortOrder = actor.Order
|
|
|
|
};
|
2016-06-24 23:53:38 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(actor.ProfilePath))
|
|
|
|
{
|
|
|
|
personInfo.ImageUrl = _tmdbClientManager.GetProfileUrl(actor.ProfilePath);
|
|
|
|
}
|
2013-10-22 19:52:33 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (actor.Id > 0)
|
|
|
|
{
|
|
|
|
personInfo.SetProviderId(MetadataProvider.Tmdb, actor.Id.ToString(CultureInfo.InvariantCulture));
|
|
|
|
}
|
2013-10-22 19:52:33 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
metadataResult.AddPerson(personInfo);
|
|
|
|
}
|
2016-03-04 17:38:51 +00:00
|
|
|
}
|
2014-02-17 21:35:08 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (movieResult.Credits?.Crew != null)
|
2020-08-17 19:59:29 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
var keepTypes = new[]
|
|
|
|
{
|
|
|
|
PersonType.Director,
|
|
|
|
PersonType.Writer,
|
|
|
|
PersonType.Producer
|
|
|
|
};
|
2020-08-17 19:59:29 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
foreach (var person in movieResult.Credits.Crew)
|
|
|
|
{
|
|
|
|
// Normalize this
|
|
|
|
var type = TmdbUtils.MapCrewToPersonType(person);
|
2015-10-26 05:29:32 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (!keepTypes.Contains(type, StringComparer.OrdinalIgnoreCase) &&
|
|
|
|
!keepTypes.Contains(person.Job ?? string.Empty, StringComparer.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
var personInfo = new PersonInfo
|
|
|
|
{
|
|
|
|
Name = person.Name.Trim(),
|
|
|
|
Role = person.Job,
|
|
|
|
Type = type
|
|
|
|
};
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(person.ProfilePath))
|
|
|
|
{
|
|
|
|
personInfo.ImageUrl = _tmdbClientManager.GetPosterUrl(person.ProfilePath);
|
|
|
|
}
|
2013-05-06 22:52:13 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
if (person.Id > 0)
|
|
|
|
{
|
|
|
|
personInfo.SetProviderId(MetadataProvider.Tmdb, person.Id.ToString(CultureInfo.InvariantCulture));
|
|
|
|
}
|
2016-03-04 17:38:51 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
metadataResult.AddPerson(personInfo);
|
2016-03-04 17:38:51 +00:00
|
|
|
}
|
2020-09-24 17:49:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (movieResult.Videos?.Results != null)
|
|
|
|
{
|
|
|
|
var trailers = new List<MediaUrl>();
|
|
|
|
for (var i = 0; i < movieResult.Videos.Results.Count; i++)
|
2020-08-17 19:59:29 +00:00
|
|
|
{
|
2020-09-24 17:49:35 +00:00
|
|
|
var video = movieResult.Videos.Results[0];
|
|
|
|
if (!TmdbUtils.IsTrailerType(video))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-17 19:59:29 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
trailers.Add(new MediaUrl
|
|
|
|
{
|
|
|
|
Url = string.Format(CultureInfo.InvariantCulture, "https://www.youtube.com/watch?v={0}", video.Key),
|
|
|
|
Name = video.Name
|
|
|
|
});
|
|
|
|
}
|
2015-01-31 03:19:41 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
movie.RemoteTrailers = trailers;
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
2014-07-05 03:17:24 +00:00
|
|
|
|
2020-09-24 17:49:35 +00:00
|
|
|
return metadataResult;
|
2013-05-19 18:42:58 +00:00
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2020-02-23 09:53:51 +00:00
|
|
|
/// <inheritdoc />
|
2020-08-17 19:10:02 +00:00
|
|
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
2014-02-20 04:53:15 +00:00
|
|
|
{
|
2020-08-31 17:05:21 +00:00
|
|
|
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
|
2014-02-20 04:53:15 +00:00
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
}
|