2019-01-13 21:03:10 +01:00
|
|
|
using System;
|
2019-01-13 20:26:31 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
2020-08-17 21:10:02 +02:00
|
|
|
using System.Net.Http;
|
2019-01-13 20:26:31 +01:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2020-08-31 19:05:21 +02:00
|
|
|
using MediaBrowser.Common.Net;
|
2014-01-28 19:37:01 +01:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-02-20 05:53:15 +01:00
|
|
|
using MediaBrowser.Model.Providers;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
2020-05-31 08:23:09 +02:00
|
|
|
namespace MediaBrowser.Providers.Plugins.Tmdb.People
|
2014-01-28 19:37:01 +01:00
|
|
|
{
|
2022-04-15 19:27:38 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Person image provider powered by TMDb.
|
|
|
|
/// </summary>
|
2019-08-18 13:20:52 +02:00
|
|
|
public class TmdbPersonProvider : IRemoteMetadataProvider<Person, PersonLookupInfo>
|
2014-01-28 19:37:01 +01:00
|
|
|
{
|
2020-08-17 21:10:02 +02:00
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
2020-09-24 19:49:35 +02:00
|
|
|
private readonly TmdbClientManager _tmdbClientManager;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
2022-04-15 19:27:38 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes a new instance of the <see cref="TmdbPersonProvider"/> class.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="httpClientFactory">The <see cref="IHttpClientFactory"/>.</param>
|
|
|
|
/// <param name="tmdbClientManager">The <see cref="TmdbClientManager"/>.</param>
|
2020-09-24 19:49:35 +02:00
|
|
|
public TmdbPersonProvider(IHttpClientFactory httpClientFactory, TmdbClientManager tmdbClientManager)
|
2014-01-28 19:37:01 +01:00
|
|
|
{
|
2020-08-17 21:10:02 +02:00
|
|
|
_httpClientFactory = httpClientFactory;
|
2020-09-24 19:49:35 +02:00
|
|
|
_tmdbClientManager = tmdbClientManager;
|
2014-01-28 19:37:01 +01:00
|
|
|
}
|
|
|
|
|
2022-04-15 19:27:38 +02:00
|
|
|
/// <inheritdoc />
|
2019-08-18 13:20:52 +02:00
|
|
|
public string Name => TmdbUtils.ProviderName;
|
2014-02-07 04:10:13 +01:00
|
|
|
|
2022-04-15 19:27:38 +02:00
|
|
|
/// <inheritdoc />
|
2014-02-20 05:53:15 +01:00
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(PersonLookupInfo searchInfo, CancellationToken cancellationToken)
|
2014-02-19 06:21:03 +01:00
|
|
|
{
|
2021-04-20 08:59:15 +02:00
|
|
|
if (searchInfo.TryGetProviderId(MetadataProvider.Tmdb, out var personTmdbId))
|
2014-02-19 06:21:03 +01:00
|
|
|
{
|
2021-05-12 14:48:45 +02:00
|
|
|
var personResult = await _tmdbClientManager.GetPersonAsync(int.Parse(personTmdbId, CultureInfo.InvariantCulture), searchInfo.MetadataLanguage, cancellationToken).ConfigureAwait(false);
|
2014-02-19 06:21:03 +01:00
|
|
|
|
2022-12-05 15:01:13 +01:00
|
|
|
if (personResult is not null)
|
2014-02-19 06:21:03 +01:00
|
|
|
{
|
2020-09-24 19:49:35 +02:00
|
|
|
var result = new RemoteSearchResult
|
|
|
|
{
|
|
|
|
Name = personResult.Name,
|
|
|
|
SearchProviderName = Name,
|
|
|
|
Overview = personResult.Biography
|
|
|
|
};
|
2015-12-22 17:38:08 +01:00
|
|
|
|
2022-12-05 15:01:13 +01:00
|
|
|
if (personResult.Images?.Profiles is not null && personResult.Images.Profiles.Count > 0)
|
2020-09-24 19:49:35 +02:00
|
|
|
{
|
|
|
|
result.ImageUrl = _tmdbClientManager.GetProfileUrl(personResult.Images.Profiles[0].FilePath);
|
|
|
|
}
|
2014-02-19 06:21:03 +01:00
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
result.SetProviderId(MetadataProvider.Tmdb, personResult.Id.ToString(CultureInfo.InvariantCulture));
|
2021-04-20 08:59:15 +02:00
|
|
|
if (!string.IsNullOrEmpty(personResult.ExternalIds.ImdbId))
|
|
|
|
{
|
|
|
|
result.SetProviderId(MetadataProvider.Imdb, personResult.ExternalIds.ImdbId);
|
|
|
|
}
|
2014-02-19 06:21:03 +01:00
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
return new[] { result };
|
|
|
|
}
|
2014-02-19 06:21:03 +01:00
|
|
|
}
|
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
var personSearchResult = await _tmdbClientManager.SearchPersonAsync(searchInfo.Name, cancellationToken).ConfigureAwait(false);
|
2014-02-19 06:21:03 +01:00
|
|
|
|
2023-03-01 00:44:57 +01:00
|
|
|
var remoteSearchResults = new RemoteSearchResult[personSearchResult.Count];
|
2020-09-24 19:49:35 +02:00
|
|
|
for (var i = 0; i < personSearchResult.Count; i++)
|
2020-08-17 21:59:29 +02:00
|
|
|
{
|
2020-09-24 19:49:35 +02:00
|
|
|
var person = personSearchResult[i];
|
|
|
|
var remoteSearchResult = new RemoteSearchResult
|
|
|
|
{
|
|
|
|
SearchProviderName = Name,
|
|
|
|
Name = person.Name,
|
|
|
|
ImageUrl = _tmdbClientManager.GetProfileUrl(person.ProfilePath)
|
|
|
|
};
|
2014-02-19 06:21:03 +01:00
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
remoteSearchResult.SetProviderId(MetadataProvider.Tmdb, person.Id.ToString(CultureInfo.InvariantCulture));
|
2023-03-01 00:44:57 +01:00
|
|
|
remoteSearchResults[i] = remoteSearchResult;
|
2020-09-24 19:49:35 +02:00
|
|
|
}
|
2014-02-19 06:21:03 +01:00
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
return remoteSearchResults;
|
2014-02-19 06:21:03 +01:00
|
|
|
}
|
|
|
|
|
2022-04-15 19:27:38 +02:00
|
|
|
/// <inheritdoc />
|
2021-07-23 05:16:38 +02:00
|
|
|
public async Task<MetadataResult<Person>> GetMetadata(PersonLookupInfo info, CancellationToken cancellationToken)
|
2014-01-28 19:37:01 +01:00
|
|
|
{
|
2021-07-23 05:16:38 +02:00
|
|
|
var personTmdbId = Convert.ToInt32(info.GetProviderId(MetadataProvider.Tmdb), CultureInfo.InvariantCulture);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
|
|
// We don't already have an Id, need to fetch it
|
2020-09-24 19:49:35 +02:00
|
|
|
if (personTmdbId <= 0)
|
2014-01-28 19:37:01 +01:00
|
|
|
{
|
2021-07-23 05:16:38 +02:00
|
|
|
var personSearchResults = await _tmdbClientManager.SearchPersonAsync(info.Name, cancellationToken).ConfigureAwait(false);
|
2020-09-24 19:49:35 +02:00
|
|
|
if (personSearchResults.Count > 0)
|
|
|
|
{
|
|
|
|
personTmdbId = personSearchResults[0].Id;
|
|
|
|
}
|
2014-01-28 19:37:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var result = new MetadataResult<Person>();
|
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
if (personTmdbId > 0)
|
2014-01-28 19:37:01 +01:00
|
|
|
{
|
2021-07-23 05:16:38 +02:00
|
|
|
var person = await _tmdbClientManager.GetPersonAsync(personTmdbId, info.MetadataLanguage, cancellationToken).ConfigureAwait(false);
|
2023-02-25 17:20:53 +01:00
|
|
|
if (person is null)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
|
|
result.HasMetadata = true;
|
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
var item = new Person
|
2016-10-09 09:18:43 +02:00
|
|
|
{
|
2020-09-24 19:49:35 +02:00
|
|
|
// Take name from incoming info, don't rename the person
|
|
|
|
// TODO: This should go in PersonMetadataService, not each person provider
|
2021-07-23 05:16:38 +02:00
|
|
|
Name = info.Name,
|
2020-09-24 19:49:35 +02:00
|
|
|
HomePageUrl = person.Homepage,
|
|
|
|
Overview = person.Biography,
|
|
|
|
PremiereDate = person.Birthday?.ToUniversalTime(),
|
|
|
|
EndDate = person.Deathday?.ToUniversalTime()
|
|
|
|
};
|
2014-01-28 19:37:01 +01:00
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
if (!string.IsNullOrWhiteSpace(person.PlaceOfBirth))
|
2014-01-28 19:37:01 +01:00
|
|
|
{
|
2020-09-24 19:49:35 +02:00
|
|
|
item.ProductionLocations = new[] { person.PlaceOfBirth };
|
2014-01-28 19:37:01 +01:00
|
|
|
}
|
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
item.SetProviderId(MetadataProvider.Tmdb, person.Id.ToString(CultureInfo.InvariantCulture));
|
2014-01-28 19:37:01 +01:00
|
|
|
|
2020-09-24 19:49:35 +02:00
|
|
|
if (!string.IsNullOrEmpty(person.ImdbId))
|
2014-01-28 19:37:01 +01:00
|
|
|
{
|
2020-09-24 19:49:35 +02:00
|
|
|
item.SetProviderId(MetadataProvider.Imdb, person.ImdbId);
|
2014-01-28 19:37:01 +01:00
|
|
|
}
|
|
|
|
|
2014-01-30 22:23:54 +01:00
|
|
|
result.HasMetadata = true;
|
2014-01-28 19:37:01 +01:00
|
|
|
result.Item = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-04-15 19:27:38 +02:00
|
|
|
/// <inheritdoc />
|
2020-08-17 21:10:02 +02:00
|
|
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
2014-02-20 05:53:15 +01:00
|
|
|
{
|
2020-08-31 19:05:21 +02:00
|
|
|
return _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken);
|
2014-02-20 05:53:15 +01:00
|
|
|
}
|
2014-01-28 19:37:01 +01:00
|
|
|
}
|
|
|
|
}
|