2019-01-13 13:03:10 -07:00
|
|
|
using System;
|
2019-01-13 12:26:31 -07:00
|
|
|
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.Common.Configuration;
|
2014-01-28 11:37:01 -07:00
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2019-01-13 12:26:31 -07:00
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using MediaBrowser.Model.Net;
|
2014-02-19 21:53:15 -07:00
|
|
|
using MediaBrowser.Model.Providers;
|
2014-01-28 11:37:01 -07:00
|
|
|
using MediaBrowser.Model.Serialization;
|
2019-08-18 04:20:52 -07:00
|
|
|
using MediaBrowser.Providers.Tmdb.Models.General;
|
|
|
|
using MediaBrowser.Providers.Tmdb.Models.People;
|
2019-08-18 05:44:13 -07:00
|
|
|
using MediaBrowser.Providers.Tmdb.Models.Search;
|
2019-08-18 04:20:52 -07:00
|
|
|
using MediaBrowser.Providers.Tmdb.Movies;
|
2018-12-13 06:18:25 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2019-08-18 04:20:52 -07:00
|
|
|
namespace MediaBrowser.Providers.Tmdb.People
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2019-08-18 04:20:52 -07:00
|
|
|
public class TmdbPersonProvider : IRemoteMetadataProvider<Person, PersonLookupInfo>
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
const string DataFileName = "info.json";
|
2014-02-18 22:21:03 -07:00
|
|
|
|
2019-08-18 04:20:52 -07:00
|
|
|
internal static TmdbPersonProvider Current { get; private set; }
|
2014-02-18 22:21:03 -07:00
|
|
|
|
2014-01-28 11:37:01 -07:00
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
private readonly IServerConfigurationManager _configurationManager;
|
2014-03-01 15:34:27 -07:00
|
|
|
private readonly IHttpClient _httpClient;
|
2016-01-22 19:32:14 -07:00
|
|
|
private readonly ILogger _logger;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2020-03-03 15:07:10 -07:00
|
|
|
public TmdbPersonProvider(
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
IServerConfigurationManager configurationManager,
|
|
|
|
IJsonSerializer jsonSerializer,
|
|
|
|
IHttpClient httpClient,
|
|
|
|
ILogger<TmdbPersonProvider> logger)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
_configurationManager = configurationManager;
|
|
|
|
_jsonSerializer = jsonSerializer;
|
2014-03-01 15:34:27 -07:00
|
|
|
_httpClient = httpClient;
|
2016-01-22 19:32:14 -07:00
|
|
|
_logger = logger;
|
2014-01-28 11:37:01 -07:00
|
|
|
Current = this;
|
|
|
|
}
|
|
|
|
|
2019-08-18 04:20:52 -07:00
|
|
|
public string Name => TmdbUtils.ProviderName;
|
2014-02-06 20:10:13 -07:00
|
|
|
|
2014-02-19 21:53:15 -07:00
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(PersonLookupInfo searchInfo, CancellationToken cancellationToken)
|
2014-02-18 22:21:03 -07:00
|
|
|
{
|
|
|
|
var tmdbId = searchInfo.GetProviderId(MetadataProviders.Tmdb);
|
|
|
|
|
2019-08-18 04:20:52 -07:00
|
|
|
var tmdbSettings = await TmdbMovieProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
|
2014-02-18 22:21:03 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var tmdbImageUrl = tmdbSettings.images.GetImageUrl("original");
|
2014-02-18 22:21:03 -07:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
|
|
|
await EnsurePersonInfo(tmdbId, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
var dataFilePath = GetPersonDataFilePath(_configurationManager.ApplicationPaths, tmdbId);
|
|
|
|
var info = _jsonSerializer.DeserializeFromFile<PersonResult>(dataFilePath);
|
|
|
|
|
2019-08-18 05:44:13 -07:00
|
|
|
var images = (info.Images ?? new PersonImages()).Profiles ?? new List<Profile>();
|
2014-02-18 22:21:03 -07:00
|
|
|
|
2014-02-19 21:53:15 -07:00
|
|
|
var result = new RemoteSearchResult
|
2014-02-18 22:21:03 -07:00
|
|
|
{
|
2019-08-18 05:44:13 -07:00
|
|
|
Name = info.Name,
|
2014-02-18 22:21:03 -07:00
|
|
|
|
2014-03-01 15:34:27 -07:00
|
|
|
SearchProviderName = Name,
|
2015-12-22 09:38:08 -07:00
|
|
|
|
2019-08-18 05:44:13 -07:00
|
|
|
ImageUrl = images.Count == 0 ? null : (tmdbImageUrl + images[0].File_Path)
|
2014-02-18 22:21:03 -07:00
|
|
|
};
|
|
|
|
|
2019-08-18 05:44:13 -07:00
|
|
|
result.SetProviderId(MetadataProviders.Tmdb, info.Id.ToString(_usCulture));
|
|
|
|
result.SetProviderId(MetadataProviders.Imdb, info.Imdb_Id);
|
2014-02-18 22:21:03 -07:00
|
|
|
|
|
|
|
return new[] { result };
|
|
|
|
}
|
|
|
|
|
2016-01-22 19:32:14 -07:00
|
|
|
if (searchInfo.IsAutomated)
|
|
|
|
{
|
2017-09-09 17:24:45 -07:00
|
|
|
// Don't hammer moviedb searching by name
|
|
|
|
return new List<RemoteSearchResult>();
|
2016-01-22 19:32:14 -07:00
|
|
|
}
|
|
|
|
|
2019-08-18 04:34:44 -07:00
|
|
|
var url = string.Format(TmdbUtils.BaseTmdbApiUrl + @"3/search/person?api_key={1}&query={0}", WebUtility.UrlEncode(searchInfo.Name), TmdbUtils.ApiKey);
|
2014-02-18 22:21:03 -07:00
|
|
|
|
2019-08-18 04:20:52 -07:00
|
|
|
using (var response = await TmdbMovieProvider.Current.GetMovieDbResponse(new HttpRequestOptions
|
2014-02-18 22:21:03 -07:00
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
CancellationToken = cancellationToken,
|
2019-08-18 04:20:52 -07:00
|
|
|
AcceptHeader = TmdbUtils.AcceptHeader
|
2014-02-18 22:21:03 -07:00
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
{
|
2017-10-20 09:16:56 -07:00
|
|
|
using (var json = response.Content)
|
|
|
|
{
|
2019-08-18 05:44:13 -07:00
|
|
|
var result = await _jsonSerializer.DeserializeFromStreamAsync<TmdbSearchResult<PersonSearchResult>>(json).ConfigureAwait(false) ??
|
|
|
|
new TmdbSearchResult<PersonSearchResult>();
|
2014-02-18 22:21:03 -07:00
|
|
|
|
2017-10-20 09:16:56 -07:00
|
|
|
return result.Results.Select(i => GetSearchResult(i, tmdbImageUrl));
|
|
|
|
}
|
2014-02-18 22:21:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 21:53:15 -07:00
|
|
|
private RemoteSearchResult GetSearchResult(PersonSearchResult i, string baseImageUrl)
|
2014-02-18 22:21:03 -07:00
|
|
|
{
|
2014-02-19 21:53:15 -07:00
|
|
|
var result = new RemoteSearchResult
|
2014-02-18 22:21:03 -07:00
|
|
|
{
|
2014-03-01 15:34:27 -07:00
|
|
|
SearchProviderName = Name,
|
2015-12-22 09:38:08 -07:00
|
|
|
|
2014-02-19 21:53:15 -07:00
|
|
|
Name = i.Name,
|
2014-02-18 22:21:03 -07:00
|
|
|
|
2019-08-18 04:20:52 -07:00
|
|
|
ImageUrl = string.IsNullOrEmpty(i.Profile_Path) ? null : baseImageUrl + i.Profile_Path
|
2014-02-18 22:21:03 -07:00
|
|
|
};
|
|
|
|
|
2014-02-19 21:53:15 -07:00
|
|
|
result.SetProviderId(MetadataProviders.Tmdb, i.Id.ToString(_usCulture));
|
2014-02-18 22:21:03 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-02-06 20:10:13 -07:00
|
|
|
public async Task<MetadataResult<Person>> GetMetadata(PersonLookupInfo id, CancellationToken cancellationToken)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
var tmdbId = id.GetProviderId(MetadataProviders.Tmdb);
|
|
|
|
|
|
|
|
// We don't already have an Id, need to fetch it
|
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
2014-02-18 22:21:03 -07:00
|
|
|
tmdbId = await GetTmdbId(id, cancellationToken).ConfigureAwait(false);
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var result = new MetadataResult<Person>();
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tmdbId))
|
|
|
|
{
|
2015-12-22 09:38:08 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
await EnsurePersonInfo(tmdbId, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
catch (HttpException ex)
|
|
|
|
{
|
|
|
|
if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
|
|
|
|
var dataFilePath = GetPersonDataFilePath(_configurationManager.ApplicationPaths, tmdbId);
|
|
|
|
|
|
|
|
var info = _jsonSerializer.DeserializeFromFile<PersonResult>(dataFilePath);
|
|
|
|
|
|
|
|
var item = new Person();
|
|
|
|
result.HasMetadata = true;
|
|
|
|
|
2017-09-09 17:24:45 -07:00
|
|
|
// Take name from incoming info, don't rename the person
|
|
|
|
// TODO: This should go in PersonMetadataService, not each person provider
|
|
|
|
item.Name = id.Name;
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
//item.HomePageUrl = info.homepage;
|
2016-10-09 00:18:43 -07:00
|
|
|
|
2019-08-18 05:44:13 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(info.Place_Of_Birth))
|
2016-10-09 00:18:43 -07:00
|
|
|
{
|
2019-08-18 05:44:13 -07:00
|
|
|
item.ProductionLocations = new string[] { info.Place_Of_Birth };
|
2016-10-09 00:18:43 -07:00
|
|
|
}
|
2019-08-18 05:44:13 -07:00
|
|
|
item.Overview = info.Biography;
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2019-08-18 05:44:13 -07:00
|
|
|
if (DateTime.TryParseExact(info.Birthday, "yyyy-MM-dd", new CultureInfo("en-US"), DateTimeStyles.None, out var date))
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
item.PremiereDate = date.ToUniversalTime();
|
|
|
|
}
|
|
|
|
|
2019-08-18 05:44:13 -07:00
|
|
|
if (DateTime.TryParseExact(info.Deathday, "yyyy-MM-dd", new CultureInfo("en-US"), DateTimeStyles.None, out date))
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
item.EndDate = date.ToUniversalTime();
|
|
|
|
}
|
|
|
|
|
2019-08-18 05:44:13 -07:00
|
|
|
item.SetProviderId(MetadataProviders.Tmdb, info.Id.ToString(_usCulture));
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2019-08-18 05:44:13 -07:00
|
|
|
if (!string.IsNullOrEmpty(info.Imdb_Id))
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2019-08-18 05:44:13 -07:00
|
|
|
item.SetProviderId(MetadataProviders.Imdb, info.Imdb_Id);
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
|
2014-01-30 14:23:54 -07:00
|
|
|
result.HasMetadata = true;
|
2014-01-28 11:37:01 -07:00
|
|
|
result.Item = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the TMDB id.
|
|
|
|
/// </summary>
|
2014-02-18 22:21:03 -07:00
|
|
|
/// <param name="info">The information.</param>
|
2014-01-28 11:37:01 -07:00
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>Task{System.String}.</returns>
|
2014-02-18 22:21:03 -07:00
|
|
|
private async Task<string> GetTmdbId(PersonLookupInfo info, CancellationToken cancellationToken)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2014-02-18 22:21:03 -07:00
|
|
|
var results = await GetSearchResults(info, cancellationToken).ConfigureAwait(false);
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2014-02-19 21:53:15 -07:00
|
|
|
return results.Select(i => i.GetProviderId(MetadataProviders.Tmdb)).FirstOrDefault();
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal async Task EnsurePersonInfo(string id, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var dataFilePath = GetPersonDataFilePath(_configurationManager.ApplicationPaths, id);
|
|
|
|
|
|
|
|
var fileInfo = _fileSystem.GetFileSystemInfo(dataFilePath);
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
if (fileInfo.Exists && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-18 04:34:44 -07:00
|
|
|
var url = string.Format(TmdbUtils.BaseTmdbApiUrl + @"3/person/{1}?api_key={0}&append_to_response=credits,images,external_ids", TmdbUtils.ApiKey, id);
|
2014-01-28 11:37:01 -07:00
|
|
|
|
2019-08-18 04:20:52 -07:00
|
|
|
using (var response = await TmdbMovieProvider.Current.GetMovieDbResponse(new HttpRequestOptions
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
|
|
|
Url = url,
|
|
|
|
CancellationToken = cancellationToken,
|
2019-08-18 04:20:52 -07:00
|
|
|
AcceptHeader = TmdbUtils.AcceptHeader
|
2014-01-28 11:37:01 -07:00
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
{
|
2017-10-20 09:16:56 -07:00
|
|
|
using (var json = response.Content)
|
2014-01-28 11:37:01 -07:00
|
|
|
{
|
2019-01-26 14:08:04 -07:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(dataFilePath));
|
2017-10-20 09:16:56 -07:00
|
|
|
|
2020-01-08 09:52:50 -07:00
|
|
|
using (var fs = new FileStream(dataFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, true))
|
2017-10-20 09:16:56 -07:00
|
|
|
{
|
|
|
|
await json.CopyToAsync(fs).ConfigureAwait(false);
|
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2014-02-28 21:13:44 -07:00
|
|
|
return Path.Combine(appPaths.CachePath, "tmdb-people");
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
|
2014-02-19 21:53:15 -07:00
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
2014-03-01 15:34:27 -07:00
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
{
|
|
|
|
CancellationToken = cancellationToken,
|
2016-10-31 11:39:41 -07:00
|
|
|
Url = url
|
2014-03-01 15:34:27 -07:00
|
|
|
});
|
2014-02-19 21:53:15 -07:00
|
|
|
}
|
2014-01-28 11:37:01 -07:00
|
|
|
}
|
|
|
|
}
|