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