jellyfin/MediaBrowser.Providers/Plugins/Tmdb/People/TmdbPersonProvider.cs

139 lines
5.4 KiB
C#
Raw Normal View History

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