MovieDbPersonProvider.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Common.IO;
  4. using MediaBrowser.Common.Net;
  5. using MediaBrowser.Controller.Configuration;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Controller.Providers;
  8. using MediaBrowser.Model.Entities;
  9. using MediaBrowser.Model.Serialization;
  10. using MediaBrowser.Providers.Movies;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Globalization;
  14. using System.IO;
  15. using System.Net;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. namespace MediaBrowser.Providers.People
  19. {
  20. public class MovieDbPersonProvider : IRemoteMetadataProvider<Person>
  21. {
  22. const string DataFileName = "info.json";
  23. internal static MovieDbPersonProvider Current { get; private set; }
  24. private readonly IJsonSerializer _jsonSerializer;
  25. private readonly IFileSystem _fileSystem;
  26. private readonly IServerConfigurationManager _configurationManager;
  27. public MovieDbPersonProvider(IFileSystem fileSystem, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer)
  28. {
  29. _fileSystem = fileSystem;
  30. _configurationManager = configurationManager;
  31. _jsonSerializer = jsonSerializer;
  32. Current = this;
  33. }
  34. public string Name
  35. {
  36. get { return "TheMovieDb"; }
  37. }
  38. public async Task<MetadataResult<Person>> GetMetadata(ItemId id, CancellationToken cancellationToken)
  39. {
  40. var tmdbId = id.GetProviderId(MetadataProviders.Tmdb);
  41. // We don't already have an Id, need to fetch it
  42. if (string.IsNullOrEmpty(tmdbId))
  43. {
  44. tmdbId = await GetTmdbId(id.Name, cancellationToken).ConfigureAwait(false);
  45. }
  46. var result = new MetadataResult<Person>();
  47. if (!string.IsNullOrEmpty(tmdbId))
  48. {
  49. await EnsurePersonInfo(tmdbId, cancellationToken).ConfigureAwait(false);
  50. var dataFilePath = GetPersonDataFilePath(_configurationManager.ApplicationPaths, tmdbId);
  51. var info = _jsonSerializer.DeserializeFromFile<PersonResult>(dataFilePath);
  52. var item = new Person();
  53. result.HasMetadata = true;
  54. item.Name = info.name;
  55. item.HomePageUrl = info.homepage;
  56. item.PlaceOfBirth = info.place_of_birth;
  57. item.Overview = info.biography;
  58. DateTime date;
  59. if (DateTime.TryParseExact(info.birthday, "yyyy-MM-dd", new CultureInfo("en-US"), DateTimeStyles.None, out date))
  60. {
  61. item.PremiereDate = date.ToUniversalTime();
  62. }
  63. if (DateTime.TryParseExact(info.deathday, "yyyy-MM-dd", new CultureInfo("en-US"), DateTimeStyles.None, out date))
  64. {
  65. item.EndDate = date.ToUniversalTime();
  66. }
  67. item.SetProviderId(MetadataProviders.Tmdb, info.id.ToString(_usCulture));
  68. if (!string.IsNullOrEmpty(info.imdb_id))
  69. {
  70. item.SetProviderId(MetadataProviders.Imdb, info.imdb_id);
  71. }
  72. result.Item = item;
  73. }
  74. return result;
  75. }
  76. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  77. /// <summary>
  78. /// Gets the TMDB id.
  79. /// </summary>
  80. /// <param name="name">The name.</param>
  81. /// <param name="cancellationToken">The cancellation token.</param>
  82. /// <returns>Task{System.String}.</returns>
  83. private async Task<string> GetTmdbId(string name, CancellationToken cancellationToken)
  84. {
  85. string url = string.Format(@"http://api.themoviedb.org/3/search/person?api_key={1}&query={0}", WebUtility.UrlEncode(name), MovieDbProvider.ApiKey);
  86. PersonSearchResults searchResult = null;
  87. using (var json = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
  88. {
  89. Url = url,
  90. CancellationToken = cancellationToken,
  91. AcceptHeader = MovieDbProvider.AcceptHeader
  92. }).ConfigureAwait(false))
  93. {
  94. searchResult = _jsonSerializer.DeserializeFromStream<PersonSearchResults>(json);
  95. }
  96. return searchResult != null && searchResult.Total_Results > 0 ? searchResult.Results[0].Id.ToString(_usCulture) : null;
  97. }
  98. internal async Task EnsurePersonInfo(string id, CancellationToken cancellationToken)
  99. {
  100. var dataFilePath = GetPersonDataFilePath(_configurationManager.ApplicationPaths, id);
  101. var fileInfo = _fileSystem.GetFileSystemInfo(dataFilePath);
  102. if (fileInfo.Exists && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 7)
  103. {
  104. return;
  105. }
  106. var url = string.Format(@"http://api.themoviedb.org/3/person/{1}?api_key={0}&append_to_response=credits,images", MovieDbProvider.ApiKey, id);
  107. using (var json = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
  108. {
  109. Url = url,
  110. CancellationToken = cancellationToken,
  111. AcceptHeader = MovieDbProvider.AcceptHeader
  112. }).ConfigureAwait(false))
  113. {
  114. Directory.CreateDirectory(Path.GetDirectoryName(dataFilePath));
  115. using (var fs = _fileSystem.GetFileStream(dataFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true))
  116. {
  117. await json.CopyToAsync(fs).ConfigureAwait(false);
  118. }
  119. }
  120. }
  121. private static string GetPersonDataPath(IApplicationPaths appPaths, string tmdbId)
  122. {
  123. var letter = tmdbId.GetMD5().ToString().Substring(0, 1);
  124. return Path.Combine(GetPersonsDataPath(appPaths), letter, tmdbId);
  125. }
  126. internal static string GetPersonDataFilePath(IApplicationPaths appPaths, string tmdbId)
  127. {
  128. return Path.Combine(GetPersonDataPath(appPaths, tmdbId), DataFileName);
  129. }
  130. private static string GetPersonsDataPath(IApplicationPaths appPaths)
  131. {
  132. return Path.Combine(appPaths.DataPath, "tmdb-people");
  133. }
  134. #region Result Objects
  135. /// <summary>
  136. /// Class PersonSearchResult
  137. /// </summary>
  138. public class PersonSearchResult
  139. {
  140. /// <summary>
  141. /// Gets or sets a value indicating whether this <see cref="MovieDbPersonProvider.PersonSearchResult" /> is adult.
  142. /// </summary>
  143. /// <value><c>true</c> if adult; otherwise, <c>false</c>.</value>
  144. public bool Adult { get; set; }
  145. /// <summary>
  146. /// Gets or sets the id.
  147. /// </summary>
  148. /// <value>The id.</value>
  149. public int Id { get; set; }
  150. /// <summary>
  151. /// Gets or sets the name.
  152. /// </summary>
  153. /// <value>The name.</value>
  154. public string Name { get; set; }
  155. /// <summary>
  156. /// Gets or sets the profile_ path.
  157. /// </summary>
  158. /// <value>The profile_ path.</value>
  159. public string Profile_Path { get; set; }
  160. }
  161. /// <summary>
  162. /// Class PersonSearchResults
  163. /// </summary>
  164. public class PersonSearchResults
  165. {
  166. /// <summary>
  167. /// Gets or sets the page.
  168. /// </summary>
  169. /// <value>The page.</value>
  170. public int Page { get; set; }
  171. /// <summary>
  172. /// Gets or sets the results.
  173. /// </summary>
  174. /// <value>The results.</value>
  175. public List<MovieDbPersonProvider.PersonSearchResult> Results { get; set; }
  176. /// <summary>
  177. /// Gets or sets the total_ pages.
  178. /// </summary>
  179. /// <value>The total_ pages.</value>
  180. public int Total_Pages { get; set; }
  181. /// <summary>
  182. /// Gets or sets the total_ results.
  183. /// </summary>
  184. /// <value>The total_ results.</value>
  185. public int Total_Results { get; set; }
  186. }
  187. public class Cast
  188. {
  189. public int id { get; set; }
  190. public string title { get; set; }
  191. public string character { get; set; }
  192. public string original_title { get; set; }
  193. public string poster_path { get; set; }
  194. public string release_date { get; set; }
  195. public bool adult { get; set; }
  196. }
  197. public class Crew
  198. {
  199. public int id { get; set; }
  200. public string title { get; set; }
  201. public string original_title { get; set; }
  202. public string department { get; set; }
  203. public string job { get; set; }
  204. public string poster_path { get; set; }
  205. public string release_date { get; set; }
  206. public bool adult { get; set; }
  207. }
  208. public class Credits
  209. {
  210. public List<Cast> cast { get; set; }
  211. public List<Crew> crew { get; set; }
  212. }
  213. public class Profile
  214. {
  215. public string file_path { get; set; }
  216. public int width { get; set; }
  217. public int height { get; set; }
  218. public object iso_639_1 { get; set; }
  219. public double aspect_ratio { get; set; }
  220. }
  221. public class Images
  222. {
  223. public List<Profile> profiles { get; set; }
  224. }
  225. public class PersonResult
  226. {
  227. public bool adult { get; set; }
  228. public List<object> also_known_as { get; set; }
  229. public string biography { get; set; }
  230. public string birthday { get; set; }
  231. public string deathday { get; set; }
  232. public string homepage { get; set; }
  233. public int id { get; set; }
  234. public string imdb_id { get; set; }
  235. public string name { get; set; }
  236. public string place_of_birth { get; set; }
  237. public double popularity { get; set; }
  238. public string profile_path { get; set; }
  239. public Credits credits { get; set; }
  240. public Images images { get; set; }
  241. }
  242. #endregion
  243. }
  244. }