jellyfin-server/MediaBrowser.Providers/Tmdb/People/TmdbPersonProvider.cs

272 lines
10 KiB
C#
Raw Normal View History

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