123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- using MediaBrowser.Common.Configuration;
- using MediaBrowser.Common.Extensions;
- using MediaBrowser.Common.Net;
- using MediaBrowser.Controller.Configuration;
- using MediaBrowser.Controller.Entities;
- using MediaBrowser.Controller.Providers;
- using MediaBrowser.Model.Entities;
- using MediaBrowser.Model.Providers;
- using MediaBrowser.Model.Serialization;
- using MediaBrowser.Providers.Movies;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Threading;
- using System.Threading.Tasks;
- using MediaBrowser.Controller.IO;
- using MediaBrowser.Model.IO;
- using MediaBrowser.Model.Logging;
- using MediaBrowser.Model.Net;
- namespace MediaBrowser.Providers.People
- {
- public class MovieDbPersonProvider : IRemoteMetadataProvider<Person, PersonLookupInfo>
- {
- const string DataFileName = "info.json";
- internal static MovieDbPersonProvider Current { get; private set; }
- private readonly IJsonSerializer _jsonSerializer;
- private readonly IFileSystem _fileSystem;
- private readonly IServerConfigurationManager _configurationManager;
- private readonly IHttpClient _httpClient;
- private readonly ILogger _logger;
- private int _requestCount;
- private readonly object _requestCountLock = new object();
- private DateTime _lastRequestCountReset;
- public MovieDbPersonProvider(IFileSystem fileSystem, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogger logger)
- {
- _fileSystem = fileSystem;
- _configurationManager = configurationManager;
- _jsonSerializer = jsonSerializer;
- _httpClient = httpClient;
- _logger = logger;
- Current = this;
- }
- public string Name
- {
- get { return "TheMovieDb"; }
- }
- public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(PersonLookupInfo searchInfo, CancellationToken cancellationToken)
- {
- var tmdbId = searchInfo.GetProviderId(MetadataProviders.Tmdb);
- var tmdbSettings = await MovieDbProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
- var tmdbImageUrl = tmdbSettings.images.secure_base_url + "original";
- if (!string.IsNullOrEmpty(tmdbId))
- {
- await EnsurePersonInfo(tmdbId, cancellationToken).ConfigureAwait(false);
- var dataFilePath = GetPersonDataFilePath(_configurationManager.ApplicationPaths, tmdbId);
- var info = _jsonSerializer.DeserializeFromFile<PersonResult>(dataFilePath);
- var images = (info.images ?? new Images()).profiles ?? new List<Profile>();
- var result = new RemoteSearchResult
- {
- Name = info.name,
- SearchProviderName = Name,
- ImageUrl = images.Count == 0 ? null : (tmdbImageUrl + images[0].file_path)
- };
- result.SetProviderId(MetadataProviders.Tmdb, info.id.ToString(_usCulture));
- result.SetProviderId(MetadataProviders.Imdb, info.imdb_id);
- return new[] { result };
- }
- if (searchInfo.IsAutomated)
- {
- lock (_requestCountLock)
- {
- if ((DateTime.UtcNow - _lastRequestCountReset).TotalHours >= 1)
- {
- _requestCount = 0;
- _lastRequestCountReset = DateTime.UtcNow;
- }
- var requestCount = _requestCount;
- if (requestCount >= 40)
- {
- //_logger.Debug("Throttling Tmdb people");
- // This needs to be throttled
- return new List<RemoteSearchResult>();
- }
- _requestCount = requestCount + 1;
- }
- }
- var url = string.Format(@"https://api.themoviedb.org/3/search/person?api_key={1}&query={0}", WebUtility.UrlEncode(searchInfo.Name), MovieDbProvider.ApiKey);
- using (var json = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
- {
- Url = url,
- CancellationToken = cancellationToken,
- AcceptHeader = MovieDbProvider.AcceptHeader
- }).ConfigureAwait(false))
- {
- var result = _jsonSerializer.DeserializeFromStream<PersonSearchResults>(json) ??
- new PersonSearchResults();
- return result.Results.Select(i => GetSearchResult(i, tmdbImageUrl));
- }
- }
- private RemoteSearchResult GetSearchResult(PersonSearchResult i, string baseImageUrl)
- {
- var result = new RemoteSearchResult
- {
- SearchProviderName = Name,
- Name = i.Name,
- ImageUrl = string.IsNullOrEmpty(i.Profile_Path) ? null : (baseImageUrl + i.Profile_Path)
- };
- result.SetProviderId(MetadataProviders.Tmdb, i.Id.ToString(_usCulture));
- return result;
- }
- public async Task<MetadataResult<Person>> GetMetadata(PersonLookupInfo id, CancellationToken cancellationToken)
- {
- var tmdbId = id.GetProviderId(MetadataProviders.Tmdb);
- // We don't already have an Id, need to fetch it
- if (string.IsNullOrEmpty(tmdbId))
- {
- tmdbId = await GetTmdbId(id, cancellationToken).ConfigureAwait(false);
- }
- var result = new MetadataResult<Person>();
- if (!string.IsNullOrEmpty(tmdbId))
- {
- try
- {
- await EnsurePersonInfo(tmdbId, cancellationToken).ConfigureAwait(false);
- }
- catch (HttpException ex)
- {
- if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound)
- {
- return result;
- }
- throw;
- }
- var dataFilePath = GetPersonDataFilePath(_configurationManager.ApplicationPaths, tmdbId);
- var info = _jsonSerializer.DeserializeFromFile<PersonResult>(dataFilePath);
- var item = new Person();
- result.HasMetadata = true;
- item.Name = info.name;
- item.HomePageUrl = info.homepage;
- if (!string.IsNullOrWhiteSpace(info.place_of_birth))
- {
- item.ProductionLocations = new List<string> { info.place_of_birth };
- }
- item.Overview = info.biography;
- DateTime date;
- if (DateTime.TryParseExact(info.birthday, "yyyy-MM-dd", new CultureInfo("en-US"), DateTimeStyles.None, out date))
- {
- item.PremiereDate = date.ToUniversalTime();
- }
- if (DateTime.TryParseExact(info.deathday, "yyyy-MM-dd", new CultureInfo("en-US"), DateTimeStyles.None, out date))
- {
- item.EndDate = date.ToUniversalTime();
- }
- item.SetProviderId(MetadataProviders.Tmdb, info.id.ToString(_usCulture));
- if (!string.IsNullOrEmpty(info.imdb_id))
- {
- item.SetProviderId(MetadataProviders.Imdb, info.imdb_id);
- }
- result.HasMetadata = true;
- result.Item = item;
- }
- return result;
- }
- private readonly CultureInfo _usCulture = new CultureInfo("en-US");
- /// <summary>
- /// Gets the TMDB id.
- /// </summary>
- /// <param name="info">The information.</param>
- /// <param name="cancellationToken">The cancellation token.</param>
- /// <returns>Task{System.String}.</returns>
- private async Task<string> GetTmdbId(PersonLookupInfo info, CancellationToken cancellationToken)
- {
- var results = await GetSearchResults(info, cancellationToken).ConfigureAwait(false);
- return results.Select(i => i.GetProviderId(MetadataProviders.Tmdb)).FirstOrDefault();
- }
- internal async Task EnsurePersonInfo(string id, CancellationToken cancellationToken)
- {
- var dataFilePath = GetPersonDataFilePath(_configurationManager.ApplicationPaths, id);
- var fileInfo = _fileSystem.GetFileSystemInfo(dataFilePath);
- if (fileInfo.Exists && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 3)
- {
- return;
- }
- var url = string.Format(@"https://api.themoviedb.org/3/person/{1}?api_key={0}&append_to_response=credits,images,external_ids", MovieDbProvider.ApiKey, id);
- using (var json = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
- {
- Url = url,
- CancellationToken = cancellationToken,
- AcceptHeader = MovieDbProvider.AcceptHeader
- }).ConfigureAwait(false))
- {
- _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(dataFilePath));
- using (var fs = _fileSystem.GetFileStream(dataFilePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true))
- {
- await json.CopyToAsync(fs).ConfigureAwait(false);
- }
- }
- }
- private static string GetPersonDataPath(IApplicationPaths appPaths, string tmdbId)
- {
- var letter = tmdbId.GetMD5().ToString().Substring(0, 1);
- return Path.Combine(GetPersonsDataPath(appPaths), letter, tmdbId);
- }
- internal static string GetPersonDataFilePath(IApplicationPaths appPaths, string tmdbId)
- {
- return Path.Combine(GetPersonDataPath(appPaths, tmdbId), DataFileName);
- }
- private static string GetPersonsDataPath(IApplicationPaths appPaths)
- {
- return Path.Combine(appPaths.CachePath, "tmdb-people");
- }
- #region Result Objects
- /// <summary>
- /// Class PersonSearchResult
- /// </summary>
- public class PersonSearchResult
- {
- /// <summary>
- /// Gets or sets a value indicating whether this <see cref="MovieDbPersonProvider.PersonSearchResult" /> is adult.
- /// </summary>
- /// <value><c>true</c> if adult; otherwise, <c>false</c>.</value>
- public bool Adult { get; set; }
- /// <summary>
- /// Gets or sets the id.
- /// </summary>
- /// <value>The id.</value>
- public int Id { get; set; }
- /// <summary>
- /// Gets or sets the name.
- /// </summary>
- /// <value>The name.</value>
- public string Name { get; set; }
- /// <summary>
- /// Gets or sets the profile_ path.
- /// </summary>
- /// <value>The profile_ path.</value>
- public string Profile_Path { get; set; }
- }
- /// <summary>
- /// Class PersonSearchResults
- /// </summary>
- public class PersonSearchResults
- {
- /// <summary>
- /// Gets or sets the page.
- /// </summary>
- /// <value>The page.</value>
- public int Page { get; set; }
- /// <summary>
- /// Gets or sets the results.
- /// </summary>
- /// <value>The results.</value>
- public List<MovieDbPersonProvider.PersonSearchResult> Results { get; set; }
- /// <summary>
- /// Gets or sets the total_ pages.
- /// </summary>
- /// <value>The total_ pages.</value>
- public int Total_Pages { get; set; }
- /// <summary>
- /// Gets or sets the total_ results.
- /// </summary>
- /// <value>The total_ results.</value>
- public int Total_Results { get; set; }
- }
- public class Cast
- {
- public int id { get; set; }
- public string title { get; set; }
- public string character { get; set; }
- public string original_title { get; set; }
- public string poster_path { get; set; }
- public string release_date { get; set; }
- public bool adult { get; set; }
- }
- public class Crew
- {
- public int id { get; set; }
- public string title { get; set; }
- public string original_title { get; set; }
- public string department { get; set; }
- public string job { get; set; }
- public string poster_path { get; set; }
- public string release_date { get; set; }
- public bool adult { get; set; }
- }
- public class Credits
- {
- public List<Cast> cast { get; set; }
- public List<Crew> crew { get; set; }
- }
- public class Profile
- {
- public string file_path { get; set; }
- public int width { get; set; }
- public int height { get; set; }
- public object iso_639_1 { get; set; }
- public double aspect_ratio { get; set; }
- }
- public class Images
- {
- public List<Profile> profiles { get; set; }
- }
- public class ExternalIds
- {
- public string imdb_id { get; set; }
- public string freebase_mid { get; set; }
- public string freebase_id { get; set; }
- public int tvrage_id { get; set; }
- }
- public class PersonResult
- {
- public bool adult { get; set; }
- public List<object> also_known_as { get; set; }
- public string biography { get; set; }
- public string birthday { get; set; }
- public string deathday { get; set; }
- public string homepage { get; set; }
- public int id { get; set; }
- public string imdb_id { get; set; }
- public string name { get; set; }
- public string place_of_birth { get; set; }
- public double popularity { get; set; }
- public string profile_path { get; set; }
- public Credits credits { get; set; }
- public Images images { get; set; }
- public ExternalIds external_ids { get; set; }
- }
- #endregion
- public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
- {
- return _httpClient.GetResponse(new HttpRequestOptions
- {
- CancellationToken = cancellationToken,
- Url = url
- });
- }
- }
- }
|