MovieDbPersonProvider.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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.Library;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Model.Entities;
  10. using MediaBrowser.Model.Logging;
  11. using MediaBrowser.Model.Serialization;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Globalization;
  15. using System.IO;
  16. using System.Net;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. namespace MediaBrowser.Providers.Movies
  20. {
  21. /// <summary>
  22. /// Class TmdbPersonProvider
  23. /// </summary>
  24. public class MovieDbPersonProvider : BaseMetadataProvider
  25. {
  26. protected readonly IProviderManager ProviderManager;
  27. internal static MovieDbPersonProvider Current { get; private set; }
  28. const string DataFileName = "info.json";
  29. private readonly IFileSystem _fileSystem;
  30. public MovieDbPersonProvider(IJsonSerializer jsonSerializer, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager, IFileSystem fileSystem)
  31. : base(logManager, configurationManager)
  32. {
  33. if (jsonSerializer == null)
  34. {
  35. throw new ArgumentNullException("jsonSerializer");
  36. }
  37. JsonSerializer = jsonSerializer;
  38. ProviderManager = providerManager;
  39. _fileSystem = fileSystem;
  40. Current = this;
  41. }
  42. /// <summary>
  43. /// Gets the json serializer.
  44. /// </summary>
  45. /// <value>The json serializer.</value>
  46. protected IJsonSerializer JsonSerializer { get; private set; }
  47. /// <summary>
  48. /// Supportses the specified item.
  49. /// </summary>
  50. /// <param name="item">The item.</param>
  51. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  52. public override bool Supports(BaseItem item)
  53. {
  54. return item is Person;
  55. }
  56. protected override bool RefreshOnVersionChange
  57. {
  58. get
  59. {
  60. return true;
  61. }
  62. }
  63. protected override string ProviderVersion
  64. {
  65. get
  66. {
  67. return "3";
  68. }
  69. }
  70. public override ItemUpdateType ItemUpdateType
  71. {
  72. get
  73. {
  74. return ItemUpdateType.MetadataDownload;
  75. }
  76. }
  77. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  78. {
  79. if (HasAltMeta(item) && !ConfigurationManager.Configuration.EnableTmdbUpdates)
  80. return false;
  81. return base.NeedsRefreshInternal(item, providerInfo);
  82. }
  83. protected override bool NeedsRefreshBasedOnCompareDate(BaseItem item, BaseProviderInfo providerInfo)
  84. {
  85. var provderId = item.GetProviderId(MetadataProviders.Tmdb);
  86. if (!string.IsNullOrEmpty(provderId))
  87. {
  88. // Process images
  89. var path = GetPersonDataPath(ConfigurationManager.ApplicationPaths, provderId);
  90. var file = Path.Combine(path, DataFileName);
  91. var fileInfo = new FileInfo(file);
  92. if (fileInfo.Exists)
  93. {
  94. return _fileSystem.GetLastWriteTimeUtc(fileInfo) > providerInfo.LastRefreshed;
  95. }
  96. return true;
  97. }
  98. return base.NeedsRefreshBasedOnCompareDate(item, providerInfo);
  99. }
  100. internal static string GetPersonDataPath(IApplicationPaths appPaths, string tmdbId)
  101. {
  102. var letter = tmdbId.GetMD5().ToString().Substring(0, 1);
  103. var seriesDataPath = Path.Combine(GetPersonsDataPath(appPaths), letter, tmdbId);
  104. return seriesDataPath;
  105. }
  106. internal static string GetPersonDataFilePath(IApplicationPaths appPaths, string tmdbId)
  107. {
  108. var letter = tmdbId.GetMD5().ToString().Substring(0, 1);
  109. var seriesDataPath = Path.Combine(GetPersonsDataPath(appPaths), letter, tmdbId);
  110. return Path.Combine(seriesDataPath, DataFileName);
  111. }
  112. internal static string GetPersonsDataPath(IApplicationPaths appPaths)
  113. {
  114. var dataPath = Path.Combine(appPaths.DataPath, "tmdb-people");
  115. return dataPath;
  116. }
  117. private bool HasAltMeta(BaseItem item)
  118. {
  119. return item.LocationType == LocationType.FileSystem && item.ResolveArgs.ContainsMetaFileByName("person.xml");
  120. }
  121. /// <summary>
  122. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  123. /// </summary>
  124. /// <param name="item">The item.</param>
  125. /// <param name="force">if set to <c>true</c> [force].</param>
  126. /// <param name="cancellationToken">The cancellation token.</param>
  127. /// <returns>Task{System.Boolean}.</returns>
  128. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  129. {
  130. cancellationToken.ThrowIfCancellationRequested();
  131. var person = (Person)item;
  132. var id = person.GetProviderId(MetadataProviders.Tmdb);
  133. // We don't already have an Id, need to fetch it
  134. if (string.IsNullOrEmpty(id))
  135. {
  136. id = await GetTmdbId(item, cancellationToken).ConfigureAwait(false);
  137. }
  138. cancellationToken.ThrowIfCancellationRequested();
  139. if (!string.IsNullOrEmpty(id))
  140. {
  141. await FetchInfo(person, id, force, cancellationToken).ConfigureAwait(false);
  142. }
  143. SetLastRefreshed(item, DateTime.UtcNow);
  144. return true;
  145. }
  146. /// <summary>
  147. /// Gets the priority.
  148. /// </summary>
  149. /// <value>The priority.</value>
  150. public override MetadataProviderPriority Priority
  151. {
  152. get { return MetadataProviderPriority.Second; }
  153. }
  154. /// <summary>
  155. /// Gets a value indicating whether [requires internet].
  156. /// </summary>
  157. /// <value><c>true</c> if [requires internet]; otherwise, <c>false</c>.</value>
  158. public override bool RequiresInternet
  159. {
  160. get
  161. {
  162. return true;
  163. }
  164. }
  165. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  166. /// <summary>
  167. /// Gets the TMDB id.
  168. /// </summary>
  169. /// <param name="person">The person.</param>
  170. /// <param name="cancellationToken">The cancellation token.</param>
  171. /// <returns>Task{System.String}.</returns>
  172. private async Task<string> GetTmdbId(BaseItem person, CancellationToken cancellationToken)
  173. {
  174. string url = string.Format(@"http://api.themoviedb.org/3/search/person?api_key={1}&query={0}", WebUtility.UrlEncode(person.Name), MovieDbProvider.ApiKey);
  175. PersonSearchResults searchResult = null;
  176. using (var json = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
  177. {
  178. Url = url,
  179. CancellationToken = cancellationToken,
  180. AcceptHeader = MovieDbProvider.AcceptHeader
  181. }).ConfigureAwait(false))
  182. {
  183. searchResult = JsonSerializer.DeserializeFromStream<PersonSearchResults>(json);
  184. }
  185. return searchResult != null && searchResult.Total_Results > 0 ? searchResult.Results[0].Id.ToString(_usCulture) : null;
  186. }
  187. /// <summary>
  188. /// Fetches the info.
  189. /// </summary>
  190. /// <param name="person">The person.</param>
  191. /// <param name="id">The id.</param>
  192. /// <param name="isForcedRefresh">if set to <c>true</c> [is forced refresh].</param>
  193. /// <param name="cancellationToken">The cancellation token.</param>
  194. /// <returns>Task.</returns>
  195. private async Task FetchInfo(Person person, string id, bool isForcedRefresh, CancellationToken cancellationToken)
  196. {
  197. var dataFilePath = GetPersonDataFilePath(ConfigurationManager.ApplicationPaths, id);
  198. // Only download if not already there
  199. // The prescan task will take care of updates so we don't need to re-download here
  200. if (!File.Exists(dataFilePath))
  201. {
  202. await DownloadPersonInfo(id, cancellationToken).ConfigureAwait(false);
  203. }
  204. if (isForcedRefresh || ConfigurationManager.Configuration.EnableTmdbUpdates || !HasAltMeta(person))
  205. {
  206. var info = JsonSerializer.DeserializeFromFile<PersonResult>(dataFilePath);
  207. cancellationToken.ThrowIfCancellationRequested();
  208. ProcessInfo(person, info);
  209. }
  210. }
  211. internal async Task DownloadPersonInfo(string id, CancellationToken cancellationToken)
  212. {
  213. var personDataPath = GetPersonDataPath(ConfigurationManager.ApplicationPaths, id);
  214. var url = string.Format(@"http://api.themoviedb.org/3/person/{1}?api_key={0}&append_to_response=credits,images", MovieDbProvider.ApiKey, id);
  215. using (var json = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions
  216. {
  217. Url = url,
  218. CancellationToken = cancellationToken,
  219. AcceptHeader = MovieDbProvider.AcceptHeader
  220. }).ConfigureAwait(false))
  221. {
  222. Directory.CreateDirectory(personDataPath);
  223. using (var fs = _fileSystem.GetFileStream(Path.Combine(personDataPath, DataFileName), FileMode.Create, FileAccess.Write, FileShare.Read, true))
  224. {
  225. await json.CopyToAsync(fs).ConfigureAwait(false);
  226. }
  227. }
  228. }
  229. /// <summary>
  230. /// Processes the info.
  231. /// </summary>
  232. /// <param name="person">The person.</param>
  233. /// <param name="searchResult">The search result.</param>
  234. protected void ProcessInfo(Person person, PersonResult searchResult)
  235. {
  236. if (!person.LockedFields.Contains(MetadataFields.Overview))
  237. {
  238. person.Overview = searchResult.biography;
  239. }
  240. DateTime date;
  241. if (DateTime.TryParseExact(searchResult.birthday, "yyyy-MM-dd", new CultureInfo("en-US"), DateTimeStyles.None, out date))
  242. {
  243. person.PremiereDate = date.ToUniversalTime();
  244. }
  245. if (DateTime.TryParseExact(searchResult.deathday, "yyyy-MM-dd", new CultureInfo("en-US"), DateTimeStyles.None, out date))
  246. {
  247. person.EndDate = date.ToUniversalTime();
  248. }
  249. if (!string.IsNullOrEmpty(searchResult.homepage))
  250. {
  251. person.HomePageUrl = searchResult.homepage;
  252. }
  253. if (!person.LockedFields.Contains(MetadataFields.ProductionLocations))
  254. {
  255. if (!string.IsNullOrEmpty(searchResult.place_of_birth))
  256. {
  257. person.ProductionLocations = new List<string> { searchResult.place_of_birth };
  258. }
  259. }
  260. person.SetProviderId(MetadataProviders.Tmdb, searchResult.id.ToString(_usCulture));
  261. }
  262. #region Result Objects
  263. /// <summary>
  264. /// Class PersonSearchResult
  265. /// </summary>
  266. public class PersonSearchResult
  267. {
  268. /// <summary>
  269. /// Gets or sets a value indicating whether this <see cref="PersonSearchResult" /> is adult.
  270. /// </summary>
  271. /// <value><c>true</c> if adult; otherwise, <c>false</c>.</value>
  272. public bool Adult { get; set; }
  273. /// <summary>
  274. /// Gets or sets the id.
  275. /// </summary>
  276. /// <value>The id.</value>
  277. public int Id { get; set; }
  278. /// <summary>
  279. /// Gets or sets the name.
  280. /// </summary>
  281. /// <value>The name.</value>
  282. public string Name { get; set; }
  283. /// <summary>
  284. /// Gets or sets the profile_ path.
  285. /// </summary>
  286. /// <value>The profile_ path.</value>
  287. public string Profile_Path { get; set; }
  288. }
  289. /// <summary>
  290. /// Class PersonSearchResults
  291. /// </summary>
  292. public class PersonSearchResults
  293. {
  294. /// <summary>
  295. /// Gets or sets the page.
  296. /// </summary>
  297. /// <value>The page.</value>
  298. public int Page { get; set; }
  299. /// <summary>
  300. /// Gets or sets the results.
  301. /// </summary>
  302. /// <value>The results.</value>
  303. public List<PersonSearchResult> Results { get; set; }
  304. /// <summary>
  305. /// Gets or sets the total_ pages.
  306. /// </summary>
  307. /// <value>The total_ pages.</value>
  308. public int Total_Pages { get; set; }
  309. /// <summary>
  310. /// Gets or sets the total_ results.
  311. /// </summary>
  312. /// <value>The total_ results.</value>
  313. public int Total_Results { get; set; }
  314. }
  315. public class Cast
  316. {
  317. public int id { get; set; }
  318. public string title { get; set; }
  319. public string character { get; set; }
  320. public string original_title { get; set; }
  321. public string poster_path { get; set; }
  322. public string release_date { get; set; }
  323. public bool adult { get; set; }
  324. }
  325. public class Crew
  326. {
  327. public int id { get; set; }
  328. public string title { get; set; }
  329. public string original_title { get; set; }
  330. public string department { get; set; }
  331. public string job { get; set; }
  332. public string poster_path { get; set; }
  333. public string release_date { get; set; }
  334. public bool adult { get; set; }
  335. }
  336. public class Credits
  337. {
  338. public List<Cast> cast { get; set; }
  339. public List<Crew> crew { get; set; }
  340. }
  341. public class Profile
  342. {
  343. public string file_path { get; set; }
  344. public int width { get; set; }
  345. public int height { get; set; }
  346. public object iso_639_1 { get; set; }
  347. public double aspect_ratio { get; set; }
  348. }
  349. public class Images
  350. {
  351. public List<Profile> profiles { get; set; }
  352. }
  353. public class PersonResult
  354. {
  355. public bool adult { get; set; }
  356. public List<object> also_known_as { get; set; }
  357. public string biography { get; set; }
  358. public string birthday { get; set; }
  359. public string deathday { get; set; }
  360. public string homepage { get; set; }
  361. public int id { get; set; }
  362. public string imdb_id { get; set; }
  363. public string name { get; set; }
  364. public string place_of_birth { get; set; }
  365. public double popularity { get; set; }
  366. public string profile_path { get; set; }
  367. public Credits credits { get; set; }
  368. public Images images { get; set; }
  369. }
  370. #endregion
  371. }
  372. }