2014-01-28 19:37:01 +01:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
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
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
using MediaBrowser.Providers.Movies;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
2014-02-19 06:21:03 +01:00
|
|
|
|
using System.Linq;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.People
|
|
|
|
|
{
|
2014-02-07 04:10:13 +01:00
|
|
|
|
public class MovieDbPersonProvider : IRemoteMetadataProvider<Person, PersonLookupInfo>
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
|
|
|
|
const string DataFileName = "info.json";
|
2014-02-19 06:21:03 +01:00
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
internal static MovieDbPersonProvider Current { get; private set; }
|
2014-02-19 06:21:03 +01:00
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
private readonly IJsonSerializer _jsonSerializer;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
private readonly IServerConfigurationManager _configurationManager;
|
2014-03-01 23:34:27 +01:00
|
|
|
|
private readonly IHttpClient _httpClient;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
2014-03-01 23:34:27 +01:00
|
|
|
|
public MovieDbPersonProvider(IFileSystem fileSystem, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IHttpClient httpClient)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
|
_configurationManager = configurationManager;
|
|
|
|
|
_jsonSerializer = jsonSerializer;
|
2014-03-01 23:34:27 +01:00
|
|
|
|
_httpClient = httpClient;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
Current = this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "TheMovieDb"; }
|
|
|
|
|
}
|
2014-02-07 04:10:13 +01:00
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
var tmdbId = searchInfo.GetProviderId(MetadataProviders.Tmdb);
|
|
|
|
|
|
|
|
|
|
var tmdbSettings = await MovieDbProvider.Current.GetTmdbSettings(cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var tmdbImageUrl = tmdbSettings.images.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>();
|
|
|
|
|
|
2014-02-20 05:53:15 +01:00
|
|
|
|
var result = new RemoteSearchResult
|
2014-02-19 06:21:03 +01:00
|
|
|
|
{
|
2014-02-20 05:53:15 +01:00
|
|
|
|
Name = info.name,
|
2014-02-19 06:21:03 +01:00
|
|
|
|
|
2014-03-01 23:34:27 +01:00
|
|
|
|
SearchProviderName = Name,
|
|
|
|
|
|
2014-02-19 06:21:03 +01:00
|
|
|
|
ImageUrl = images.Count == 0 ? null : (tmdbImageUrl + images[0].file_path)
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-20 05:53:15 +01:00
|
|
|
|
result.SetProviderId(MetadataProviders.Tmdb, info.id.ToString(_usCulture));
|
|
|
|
|
result.SetProviderId(MetadataProviders.Imdb, info.imdb_id.ToString(_usCulture));
|
2014-02-19 06:21:03 +01:00
|
|
|
|
|
|
|
|
|
return new[] { result };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var url = string.Format(@"http://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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-20 05:53:15 +01:00
|
|
|
|
private RemoteSearchResult GetSearchResult(PersonSearchResult i, string baseImageUrl)
|
2014-02-19 06:21:03 +01:00
|
|
|
|
{
|
2014-02-20 05:53:15 +01:00
|
|
|
|
var result = new RemoteSearchResult
|
2014-02-19 06:21:03 +01:00
|
|
|
|
{
|
2014-03-01 23:34:27 +01:00
|
|
|
|
SearchProviderName = Name,
|
|
|
|
|
|
2014-02-20 05:53:15 +01:00
|
|
|
|
Name = i.Name,
|
2014-02-19 06:21:03 +01:00
|
|
|
|
|
|
|
|
|
ImageUrl = string.IsNullOrEmpty(i.Profile_Path) ? null : (baseImageUrl + i.Profile_Path)
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-20 05:53:15 +01:00
|
|
|
|
result.SetProviderId(MetadataProviders.Tmdb, i.Id.ToString(_usCulture));
|
2014-02-19 06:21:03 +01:00
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-07 04:10:13 +01:00
|
|
|
|
public async Task<MetadataResult<Person>> GetMetadata(PersonLookupInfo id, CancellationToken cancellationToken)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
|
|
|
|
var tmdbId = id.GetProviderId(MetadataProviders.Tmdb);
|
|
|
|
|
|
|
|
|
|
// We don't already have an Id, need to fetch it
|
|
|
|
|
if (string.IsNullOrEmpty(tmdbId))
|
|
|
|
|
{
|
2014-02-19 06:21:03 +01:00
|
|
|
|
tmdbId = await GetTmdbId(id, cancellationToken).ConfigureAwait(false);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = new MetadataResult<Person>();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(tmdbId))
|
|
|
|
|
{
|
|
|
|
|
await EnsurePersonInfo(tmdbId, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
item.PlaceOfBirth = 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);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 22:23:54 +01:00
|
|
|
|
result.HasMetadata = true;
|
2014-01-28 19:37:01 +01:00
|
|
|
|
result.Item = item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the TMDB id.
|
|
|
|
|
/// </summary>
|
2014-02-19 06:21:03 +01:00
|
|
|
|
/// <param name="info">The information.</param>
|
2014-01-28 19:37:01 +01:00
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{System.String}.</returns>
|
2014-02-19 06:21:03 +01:00
|
|
|
|
private async Task<string> GetTmdbId(PersonLookupInfo info, CancellationToken cancellationToken)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
2014-02-19 06:21:03 +01:00
|
|
|
|
var results = await GetSearchResults(info, cancellationToken).ConfigureAwait(false);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
2014-02-20 05:53:15 +01:00
|
|
|
|
return results.Select(i => i.GetProviderId(MetadataProviders.Tmdb)).FirstOrDefault();
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal async Task EnsurePersonInfo(string id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var dataFilePath = GetPersonDataFilePath(_configurationManager.ApplicationPaths, id);
|
|
|
|
|
|
|
|
|
|
var fileInfo = _fileSystem.GetFileSystemInfo(dataFilePath);
|
|
|
|
|
|
2014-10-07 01:58:46 +02:00
|
|
|
|
if (fileInfo.Exists && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 3)
|
2014-01-28 19:37:01 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-10 19:39:41 +01:00
|
|
|
|
var url = string.Format(@"http://api.themoviedb.org/3/person/{1}?api_key={0}&append_to_response=credits,images,external_ids", MovieDbProvider.ApiKey, id);
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
|
|
|
|
using (var json = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
AcceptHeader = MovieDbProvider.AcceptHeader
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
|
|
|
|
{
|
2015-09-13 23:32:02 +02:00
|
|
|
|
_fileSystem.CreateDirectory(Path.GetDirectoryName(dataFilePath));
|
2014-01-28 19:37:01 +01:00
|
|
|
|
|
|
|
|
|
using (var fs = _fileSystem.GetFileStream(dataFilePath, FileMode.Create, FileAccess.Write, FileShare.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)
|
|
|
|
|
{
|
2014-03-01 05:13:44 +01:00
|
|
|
|
return Path.Combine(appPaths.CachePath, "tmdb-people");
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#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; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-10 19:39:41 +01:00
|
|
|
|
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; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-28 19:37:01 +01:00
|
|
|
|
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; }
|
2014-02-10 19:39:41 +01:00
|
|
|
|
public ExternalIds external_ids { get; set; }
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2014-02-20 05:53:15 +01:00
|
|
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-03-01 23:34:27 +01:00
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = MovieDbProvider.Current.MovieDbResourcePool
|
|
|
|
|
});
|
2014-02-20 05:53:15 +01:00
|
|
|
|
}
|
2014-01-28 19:37:01 +01:00
|
|
|
|
}
|
|
|
|
|
}
|