2020-06-19 18:24:13 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2014-02-06 04:39:16 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
2019-01-26 20:47:11 +00:00
|
|
|
using System.IO;
|
2014-02-06 04:39:16 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
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.IO;
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2020-05-31 06:23:09 +00:00
|
|
|
using MediaBrowser.Providers.Plugins.Tmdb.Models.Movies;
|
2019-01-13 19:26:31 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2020-05-31 06:23:09 +00:00
|
|
|
namespace MediaBrowser.Providers.Plugins.Tmdb.Movies
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
public class GenericTmdbMovieInfo<T>
|
2015-03-14 20:00:32 +00:00
|
|
|
where T : BaseItem, new()
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
2014-11-18 02:48:22 +00:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
2016-11-01 18:28:36 +00:00
|
|
|
private readonly IFileSystem _fileSystem;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
public GenericTmdbMovieInfo(ILogger logger, IJsonSerializer jsonSerializer, ILibraryManager libraryManager, IFileSystem fileSystem)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_jsonSerializer = jsonSerializer;
|
2014-11-18 02:48:22 +00:00
|
|
|
_libraryManager = libraryManager;
|
2016-11-01 18:28:36 +00:00
|
|
|
_fileSystem = fileSystem;
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
|
2014-02-07 03:10:13 +00:00
|
|
|
public async Task<MetadataResult<T>> GetMetadata(ItemLookupInfo itemId, CancellationToken cancellationToken)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2020-06-06 19:17:49 +00:00
|
|
|
var tmdbId = itemId.GetProviderId(MetadataProvider.Tmdb);
|
|
|
|
var imdbId = itemId.GetProviderId(MetadataProvider.Imdb);
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2019-01-07 23:27:46 +00:00
|
|
|
// Don't search for music video id's because it is very easy to misidentify.
|
2014-02-06 04:39:16 +00:00
|
|
|
if (string.IsNullOrEmpty(tmdbId) && string.IsNullOrEmpty(imdbId) && typeof(T) != typeof(MusicVideo))
|
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
var searchResults = await new TmdbSearch(_logger, _jsonSerializer, _libraryManager).GetMovieSearchResults(itemId, cancellationToken).ConfigureAwait(false);
|
2014-03-01 22:34:27 +00:00
|
|
|
|
|
|
|
var searchResult = searchResults.FirstOrDefault();
|
2014-02-15 16:36:09 +00:00
|
|
|
|
|
|
|
if (searchResult != null)
|
|
|
|
{
|
2020-06-06 19:17:49 +00:00
|
|
|
tmdbId = searchResult.GetProviderId(MetadataProvider.Tmdb);
|
2014-02-15 16:36:09 +00:00
|
|
|
}
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tmdbId) || !string.IsNullOrEmpty(imdbId))
|
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2015-06-29 01:10:45 +00:00
|
|
|
return await FetchMovieData(tmdbId, imdbId, itemId.MetadataLanguage, itemId.MetadataCountryCode, cancellationToken).ConfigureAwait(false);
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 01:10:45 +00:00
|
|
|
return new MetadataResult<T>();
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Fetches the movie data.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="tmdbId">The TMDB identifier.</param>
|
|
|
|
/// <param name="imdbId">The imdb identifier.</param>
|
|
|
|
/// <param name="language">The language.</param>
|
|
|
|
/// <param name="preferredCountryCode">The preferred country code.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>Task{`0}.</returns>
|
2015-06-29 01:10:45 +00:00
|
|
|
private async Task<MetadataResult<T>> FetchMovieData(string tmdbId, string imdbId, string language, string preferredCountryCode, CancellationToken cancellationToken)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2015-06-29 01:10:45 +00:00
|
|
|
var item = new MetadataResult<T>
|
|
|
|
{
|
|
|
|
Item = new T()
|
|
|
|
};
|
|
|
|
|
2014-02-06 04:39:16 +00:00
|
|
|
string dataFilePath = null;
|
2019-08-18 11:20:52 +00:00
|
|
|
MovieResult movieInfo = null;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
|
|
|
// Id could be ImdbId or TmdbId
|
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
movieInfo = await TmdbMovieProvider.Current.FetchMainResult(imdbId, false, language, cancellationToken).ConfigureAwait(false);
|
2015-10-26 05:29:32 +00:00
|
|
|
if (movieInfo != null)
|
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
tmdbId = movieInfo.Id.ToString(_usCulture);
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
dataFilePath = TmdbMovieProvider.Current.GetDataFilePath(tmdbId, language);
|
2019-01-26 21:08:04 +00:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(dataFilePath));
|
2015-10-26 05:29:32 +00:00
|
|
|
_jsonSerializer.SerializeToFile(movieInfo, dataFilePath);
|
|
|
|
}
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 05:29:32 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(tmdbId))
|
|
|
|
{
|
2019-08-18 11:20:52 +00:00
|
|
|
await TmdbMovieProvider.Current.EnsureMovieInfo(tmdbId, language, cancellationToken).ConfigureAwait(false);
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
dataFilePath = dataFilePath ?? TmdbMovieProvider.Current.GetDataFilePath(tmdbId, language);
|
|
|
|
movieInfo = movieInfo ?? _jsonSerializer.DeserializeFromFile<MovieResult>(dataFilePath);
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
var settings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
|
2016-01-21 18:50:43 +00:00
|
|
|
|
|
|
|
ProcessMainInfo(item, settings, preferredCountryCode, movieInfo);
|
2015-10-26 05:29:32 +00:00
|
|
|
item.HasMetadata = true;
|
|
|
|
}
|
2014-02-06 04:39:16 +00:00
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Processes the main info.
|
|
|
|
/// </summary>
|
2015-06-29 01:10:45 +00:00
|
|
|
/// <param name="resultItem">The result item.</param>
|
2016-01-21 18:50:43 +00:00
|
|
|
/// <param name="settings">The settings.</param>
|
2014-02-06 04:39:16 +00:00
|
|
|
/// <param name="preferredCountryCode">The preferred country code.</param>
|
|
|
|
/// <param name="movieData">The movie data.</param>
|
2019-08-18 11:20:52 +00:00
|
|
|
private void ProcessMainInfo(MetadataResult<T> resultItem, TmdbSettingsResult settings, string preferredCountryCode, MovieResult movieData)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2015-06-29 01:10:45 +00:00
|
|
|
var movie = resultItem.Item;
|
|
|
|
|
2015-03-10 01:30:20 +00:00
|
|
|
movie.Name = movieData.GetTitle() ?? movie.Name;
|
|
|
|
|
2016-10-01 20:29:24 +00:00
|
|
|
movie.OriginalTitle = movieData.GetOriginalTitle();
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
movie.Overview = string.IsNullOrWhiteSpace(movieData.Overview) ? null : WebUtility.HtmlDecode(movieData.Overview);
|
2014-02-06 04:39:16 +00:00
|
|
|
movie.Overview = movie.Overview != null ? movie.Overview.Replace("\n\n", "\n") : null;
|
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
// movie.HomePageUrl = movieData.homepage;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
if (!string.IsNullOrEmpty(movieData.Tagline))
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
movie.Tagline = movieData.Tagline;
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
if (movieData.Production_Countries != null)
|
2014-05-16 19:16:29 +00:00
|
|
|
{
|
2016-10-09 07:18:43 +00:00
|
|
|
movie.ProductionLocations = movieData
|
2019-08-18 12:44:13 +00:00
|
|
|
.Production_Countries
|
|
|
|
.Select(i => i.Name)
|
2018-12-28 15:48:26 +00:00
|
|
|
.ToArray();
|
2014-05-16 19:16:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 19:17:49 +00:00
|
|
|
movie.SetProviderId(MetadataProvider.Tmdb, movieData.Id.ToString(_usCulture));
|
|
|
|
movie.SetProviderId(MetadataProvider.Imdb, movieData.Imdb_Id);
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
if (movieData.Belongs_To_Collection != null)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2020-06-06 19:17:49 +00:00
|
|
|
movie.SetProviderId(MetadataProvider.TmdbCollection,
|
2019-08-18 12:44:13 +00:00
|
|
|
movieData.Belongs_To_Collection.Id.ToString(CultureInfo.InvariantCulture));
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2019-08-18 11:20:52 +00:00
|
|
|
if (movie is Movie movieItem)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
movieItem.CollectionName = movieData.Belongs_To_Collection.Name;
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
string voteAvg = movieData.Vote_Average.ToString(CultureInfo.InvariantCulture);
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2019-01-13 20:46:33 +00:00
|
|
|
if (float.TryParse(voteAvg, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var rating))
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
|
|
|
movie.CommunityRating = rating;
|
|
|
|
}
|
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
// movie.VoteCount = movieData.vote_count;
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
if (movieData.Releases != null && movieData.Releases.Countries != null)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
var releases = movieData.Releases.Countries.Where(i => !string.IsNullOrWhiteSpace(i.Certification)).ToList();
|
2015-08-19 16:43:23 +00:00
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
var ourRelease = releases.FirstOrDefault(c => string.Equals(c.Iso_3166_1, preferredCountryCode, StringComparison.OrdinalIgnoreCase));
|
|
|
|
var usRelease = releases.FirstOrDefault(c => string.Equals(c.Iso_3166_1, "US", StringComparison.OrdinalIgnoreCase));
|
2015-08-19 16:43:23 +00:00
|
|
|
|
|
|
|
if (ourRelease != null)
|
|
|
|
{
|
|
|
|
var ratingPrefix = string.Equals(preferredCountryCode, "us", StringComparison.OrdinalIgnoreCase) ? "" : preferredCountryCode + "-";
|
2019-08-18 12:44:13 +00:00
|
|
|
var newRating = ratingPrefix + ourRelease.Certification;
|
2016-09-18 05:52:10 +00:00
|
|
|
|
|
|
|
newRating = newRating.Replace("de-", "FSK-", StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
movie.OfficialRating = newRating;
|
2015-08-19 16:43:23 +00:00
|
|
|
}
|
|
|
|
else if (usRelease != null)
|
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
movie.OfficialRating = usRelease.Certification;
|
2015-08-19 16:43:23 +00:00
|
|
|
}
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(movieData.Release_Date))
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2014-03-02 18:01:46 +00:00
|
|
|
// These dates are always in this exact format
|
2019-08-18 12:44:13 +00:00
|
|
|
if (DateTime.TryParse(movieData.Release_Date, _usCulture, DateTimeStyles.None, out var r))
|
2014-03-02 18:01:46 +00:00
|
|
|
{
|
|
|
|
movie.PremiereDate = r.ToUniversalTime();
|
|
|
|
movie.ProductionYear = movie.PremiereDate.Value.Year;
|
|
|
|
}
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
// studios
|
2019-08-18 12:44:13 +00:00
|
|
|
if (movieData.Production_Companies != null)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
movie.SetStudios(movieData.Production_Companies.Select(c => c.Name));
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// genres
|
|
|
|
// Movies get this from imdb
|
2019-08-18 12:44:13 +00:00
|
|
|
var genres = movieData.Genres ?? new List<Tmdb.Models.General.Genre>();
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
foreach (var genre in genres.Select(g => g.Name))
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
|
|
|
movie.AddGenre(genre);
|
|
|
|
}
|
|
|
|
|
2015-07-24 02:48:10 +00:00
|
|
|
resultItem.ResetPeople();
|
2018-09-12 17:26:21 +00:00
|
|
|
var tmdbImageUrl = settings.images.GetImageUrl("original");
|
2015-08-19 16:43:23 +00:00
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
// Actors, Directors, Writers - all in People
|
|
|
|
// actors come from cast
|
2019-08-18 12:44:13 +00:00
|
|
|
if (movieData.Casts != null && movieData.Casts.Cast != null)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
foreach (var actor in movieData.Casts.Cast.OrderBy(a => a.Order))
|
2015-06-28 16:36:25 +00:00
|
|
|
{
|
2016-01-21 18:50:43 +00:00
|
|
|
var personInfo = new PersonInfo
|
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
Name = actor.Name.Trim(),
|
|
|
|
Role = actor.Character,
|
2016-01-21 18:50:43 +00:00
|
|
|
Type = PersonType.Actor,
|
2019-08-18 12:44:13 +00:00
|
|
|
SortOrder = actor.Order
|
2016-01-21 18:50:43 +00:00
|
|
|
};
|
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(actor.Profile_Path))
|
2016-01-21 18:50:43 +00:00
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
personInfo.ImageUrl = tmdbImageUrl + actor.Profile_Path;
|
2016-01-21 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
if (actor.Id > 0)
|
2016-01-21 18:50:43 +00:00
|
|
|
{
|
2020-06-06 19:17:49 +00:00
|
|
|
personInfo.SetProviderId(MetadataProvider.Tmdb, actor.Id.ToString(CultureInfo.InvariantCulture));
|
2016-01-21 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resultItem.AddPerson(personInfo);
|
2015-06-28 16:36:25 +00:00
|
|
|
}
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
// and the rest from crew
|
2019-08-18 12:44:13 +00:00
|
|
|
if (movieData.Casts?.Crew != null)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2017-01-31 21:20:01 +00:00
|
|
|
var keepTypes = new[]
|
|
|
|
{
|
|
|
|
PersonType.Director,
|
2019-08-18 11:38:49 +00:00
|
|
|
PersonType.Writer,
|
|
|
|
PersonType.Producer
|
2017-01-31 21:20:01 +00:00
|
|
|
};
|
2016-11-01 18:28:36 +00:00
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
foreach (var person in movieData.Casts.Crew)
|
2015-06-28 16:36:25 +00:00
|
|
|
{
|
2015-09-25 12:53:38 +00:00
|
|
|
// Normalize this
|
2019-08-18 11:38:49 +00:00
|
|
|
var type = TmdbUtils.MapCrewToPersonType(person);
|
2015-09-25 12:53:38 +00:00
|
|
|
|
2019-08-18 11:38:49 +00:00
|
|
|
if (!keepTypes.Contains(type, StringComparer.OrdinalIgnoreCase) &&
|
2019-08-18 12:44:13 +00:00
|
|
|
!keepTypes.Contains(person.Job ?? string.Empty, StringComparer.OrdinalIgnoreCase))
|
2016-11-01 18:28:36 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-01-21 18:50:43 +00:00
|
|
|
var personInfo = new PersonInfo
|
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
Name = person.Name.Trim(),
|
|
|
|
Role = person.Job,
|
2016-01-21 18:50:43 +00:00
|
|
|
Type = type
|
|
|
|
};
|
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
if (!string.IsNullOrWhiteSpace(person.Profile_Path))
|
2016-01-21 18:50:43 +00:00
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
personInfo.ImageUrl = tmdbImageUrl + person.Profile_Path;
|
2016-01-21 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
if (person.Id > 0)
|
2016-01-21 18:50:43 +00:00
|
|
|
{
|
2020-06-06 19:17:49 +00:00
|
|
|
personInfo.SetProviderId(MetadataProvider.Tmdb, person.Id.ToString(CultureInfo.InvariantCulture));
|
2016-01-21 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resultItem.AddPerson(personInfo);
|
2015-06-28 16:36:25 +00:00
|
|
|
}
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 09:11:11 +00:00
|
|
|
// if (movieData.keywords != null && movieData.keywords.keywords != null)
|
2017-07-23 22:29:53 +00:00
|
|
|
//{
|
|
|
|
// movie.Keywords = movieData.keywords.keywords.Select(i => i.name).ToList();
|
|
|
|
//}
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2019-08-18 12:44:13 +00:00
|
|
|
if (movieData.Trailers != null && movieData.Trailers.Youtube != null)
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2019-08-18 12:44:13 +00:00
|
|
|
movie.RemoteTrailers = movieData.Trailers.Youtube.Select(i => new MediaUrl
|
2014-02-06 04:39:16 +00:00
|
|
|
{
|
2020-08-07 17:26:28 +00:00
|
|
|
Url = string.Format(CultureInfo.InvariantCulture, "https://www.youtube.com/watch?v={0}", i.Source),
|
2019-08-18 12:44:13 +00:00
|
|
|
Name = i.Name
|
2014-02-06 04:39:16 +00:00
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
}).ToArray();
|
2014-02-06 04:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|