jellyfin/MediaBrowser.Providers/Plugins/Tmdb/Movies/TmdbMovieProvider.cs

339 lines
13 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
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;
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;
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;
2021-04-17 03:37:55 -07:00
using TMDbLib.Objects.Find;
using TMDbLib.Objects.Search;
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>
/// Class MovieDbProvider.
2013-02-20 18:33:05 -07:00
/// </summary>
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;
private readonly TmdbClientManager _tmdbClientManager;
2020-02-23 02:53:51 -07:00
public TmdbMovieProvider(
ILibraryManager libraryManager,
TmdbClientManager tmdbClientManager,
IHttpClientFactory httpClientFactory)
{
2014-11-17 19:48:22 -07:00
_libraryManager = libraryManager;
_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;
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(MovieInfo searchInfo, CancellationToken cancellationToken)
2014-03-02 10:09:35 -07:00
{
if (searchInfo.TryGetProviderId(MetadataProvider.Tmdb, out var id))
2014-03-02 10:09:35 -07:00
{
var movie = await _tmdbClientManager
.GetMovieAsync(
int.Parse(id, CultureInfo.InvariantCulture),
searchInfo.MetadataLanguage,
TmdbUtils.GetImageLanguagesParam(searchInfo.MetadataLanguage),
cancellationToken)
.ConfigureAwait(false);
2021-02-18 14:54:12 -07:00
var remoteResult = new RemoteSearchResult
2014-03-02 10:09:35 -07:00
{
Name = movie.Title ?? movie.OriginalTitle,
SearchProviderName = Name,
ImageUrl = _tmdbClientManager.GetPosterUrl(movie.PosterPath),
Overview = movie.Overview
};
if (movie.ReleaseDate != null)
{
var releaseDate = movie.ReleaseDate.Value.ToUniversalTime();
remoteResult.PremiereDate = releaseDate;
remoteResult.ProductionYear = releaseDate.Year;
2014-03-02 11:01:46 -07:00
}
remoteResult.SetProviderId(MetadataProvider.Tmdb, movie.Id.ToString(CultureInfo.InvariantCulture));
2014-03-02 10:09:35 -07:00
if (!string.IsNullOrWhiteSpace(movie.ImdbId))
{
remoteResult.SetProviderId(MetadataProvider.Imdb, movie.ImdbId);
}
2013-02-24 14:53:54 -07:00
return new[] { remoteResult };
}
2013-02-20 18:33:05 -07:00
IReadOnlyList<SearchMovie> movieResults;
if (searchInfo.TryGetProviderId(MetadataProvider.Imdb, out id))
2020-08-17 12:59:29 -07:00
{
var result = await _tmdbClientManager.FindByExternalIdAsync(
id,
FindExternalSource.Imdb,
TmdbUtils.GetImageLanguagesParam(searchInfo.MetadataLanguage),
cancellationToken).ConfigureAwait(false);
movieResults = result.MovieResults;
}
else if (searchInfo.TryGetProviderId(MetadataProvider.Tvdb, out id))
{
var result = await _tmdbClientManager.FindByExternalIdAsync(
id,
FindExternalSource.TvDb,
TmdbUtils.GetImageLanguagesParam(searchInfo.MetadataLanguage),
cancellationToken).ConfigureAwait(false);
movieResults = result.MovieResults;
}
else
{
movieResults = await _tmdbClientManager
.SearchMovieAsync(searchInfo.Name, searchInfo.Year ?? 0, searchInfo.MetadataLanguage, cancellationToken)
.ConfigureAwait(false);
2020-08-17 12:59:29 -07:00
}
var len = movieResults.Count;
var remoteSearchResults = new RemoteSearchResult[len];
for (var i = 0; i < len; i++)
2020-06-20 01:35:29 -07:00
{
var movieResult = movieResults[i];
var remoteSearchResult = new RemoteSearchResult
{
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[i] = remoteSearchResult;
2020-06-20 01:35:29 -07:00
}
2013-02-20 18:33:05 -07:00
return remoteSearchResults;
2013-02-20 18:33:05 -07:00
}
public async Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, CancellationToken cancellationToken)
2014-01-10 06:52:01 -07:00
{
var tmdbId = info.GetProviderId(MetadataProvider.Tmdb);
var imdbId = info.GetProviderId(MetadataProvider.Imdb);
if (string.IsNullOrEmpty(tmdbId) && string.IsNullOrEmpty(imdbId))
2014-01-10 06:52:01 -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 cleanedName = TmdbUtils.CleanName(parsedName.Name);
var searchResults = await _tmdbClientManager.SearchMovieAsync(cleanedName, info.Year ?? parsedName.Year ?? 0, info.MetadataLanguage, cancellationToken).ConfigureAwait(false);
if (searchResults.Count > 0)
2014-01-10 06:52:01 -07:00
{
tmdbId = searchResults[0].Id.ToString(CultureInfo.InvariantCulture);
2014-01-10 06:52:01 -07:00
}
}
if (string.IsNullOrEmpty(tmdbId) && !string.IsNullOrEmpty(imdbId))
{
var movieResultFromImdbId = await _tmdbClientManager.FindByExternalIdAsync(imdbId, FindExternalSource.Imdb, info.MetadataLanguage, cancellationToken).ConfigureAwait(false);
if (movieResultFromImdbId?.MovieResults.Count > 0)
{
tmdbId = movieResultFromImdbId.MovieResults[0].Id.ToString(CultureInfo.InvariantCulture);
}
}
if (string.IsNullOrEmpty(tmdbId))
{
return new MetadataResult<Movie>();
}
var movieResult = await _tmdbClientManager
.GetMovieAsync(Convert.ToInt32(tmdbId, CultureInfo.InvariantCulture), info.MetadataLanguage, TmdbUtils.GetImageLanguagesParam(info.MetadataLanguage), cancellationToken)
.ConfigureAwait(false);
2020-11-22 06:11:05 -07:00
if (movieResult == null)
{
return new MetadataResult<Movie>();
}
var movie = new Movie
{
Name = movieResult.Title ?? movieResult.OriginalTitle,
OriginalTitle = 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)
{
movie.SetProviderId(MetadataProvider.TmdbCollection, movieResult.BelongsToCollection.Id.ToString(CultureInfo.InvariantCulture));
movie.CollectionName = movieResult.BelongsToCollection.Name;
}
movie.CommunityRating = Convert.ToSingle(movieResult.VoteAverage);
2013-12-28 09:58:13 -07:00
if (movieResult.Releases?.Countries != null)
{
var releases = movieResult.Releases.Countries.Where(i => !string.IsNullOrWhiteSpace(i.Certification)).ToList();
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
if (ourRelease != null)
{
movie.OfficialRating = TmdbUtils.BuildParentalRating(ourRelease.Iso_3166_1, ourRelease.Certification);
}
else if (usRelease != null)
{
movie.OfficialRating = usRelease.Certification;
}
2014-10-06 16:30:05 -07:00
}
movie.PremiereDate = movieResult.ReleaseDate;
movie.ProductionYear = movieResult.ReleaseDate?.Year;
if (movieResult.ProductionCompanies != null)
2014-10-06 16:30:05 -07:00
{
movie.SetStudios(movieResult.ProductionCompanies.Select(c => c.Name));
2014-10-06 16:30:05 -07:00
}
var genres = movieResult.Genres;
2014-10-06 16:30:05 -07:00
foreach (var genre in genres.Select(g => g.Name))
2016-03-12 08:14:17 -07:00
{
movie.AddGenre(genre);
2016-03-12 08:14:17 -07:00
}
if (movieResult.Keywords?.Keywords != null)
2020-09-10 02:05:46 -07:00
{
for (var i = 0; i < movieResult.Keywords.Keywords.Count; i++)
{
movie.AddTag(movieResult.Keywords.Keywords[i].Name);
}
}
if (movieResult.Credits?.Cast != null)
{
// 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
};
if (!string.IsNullOrWhiteSpace(actor.ProfilePath))
{
personInfo.ImageUrl = _tmdbClientManager.GetProfileUrl(actor.ProfilePath);
}
2013-10-22 12:52:33 -07:00
if (actor.Id > 0)
{
personInfo.SetProviderId(MetadataProvider.Tmdb, actor.Id.ToString(CultureInfo.InvariantCulture));
}
2013-10-22 12:52:33 -07:00
metadataResult.AddPerson(personInfo);
}
}
if (movieResult.Credits?.Crew != null)
2020-08-17 12:59:29 -07:00
{
var keepTypes = new[]
{
PersonType.Director,
PersonType.Writer,
PersonType.Producer
};
2020-08-17 12:59:29 -07:00
foreach (var person in movieResult.Credits.Crew)
{
// Normalize this
var type = TmdbUtils.MapCrewToPersonType(person);
2015-10-25 22:29:32 -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
var personInfo = new PersonInfo
{
Name = person.Name.Trim(),
Role = person.Job,
Type = type
};
2013-02-20 18:33:05 -07:00
if (!string.IsNullOrWhiteSpace(person.ProfilePath))
{
personInfo.ImageUrl = _tmdbClientManager.GetPosterUrl(person.ProfilePath);
}
if (person.Id > 0)
{
personInfo.SetProviderId(MetadataProvider.Tmdb, person.Id.ToString(CultureInfo.InvariantCulture));
}
metadataResult.AddPerson(personInfo);
}
}
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
{
var video = movieResult.Videos.Results[0];
if (!TmdbUtils.IsTrailerType(video))
{
continue;
}
2020-08-17 12:59:29 -07:00
trailers.Add(new MediaUrl
{
Url = string.Format(CultureInfo.InvariantCulture, "https://www.youtube.com/watch?v={0}", video.Key),
Name = video.Name
});
}
movie.RemoteTrailers = trailers;
2013-02-20 18:33:05 -07:00
}
return metadataResult;
}
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)
{
2020-08-31 10:05:21 -07:00
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
}
2013-02-20 18:33:05 -07:00
}
}