MovieDbProvider.cs 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Entities;
  4. using MediaBrowser.Controller.Entities.Movies;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.Serialization;
  9. using MediaBrowser.Providers.Savers;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Globalization;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Net;
  16. using System.Text.RegularExpressions;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. namespace MediaBrowser.Providers.Movies
  20. {
  21. /// <summary>
  22. /// Class MovieDbProvider
  23. /// </summary>
  24. public class MovieDbProvider : BaseMetadataProvider, IDisposable
  25. {
  26. protected static CultureInfo EnUs = new CultureInfo("en-US");
  27. protected readonly IProviderManager ProviderManager;
  28. /// <summary>
  29. /// The movie db
  30. /// </summary>
  31. internal readonly SemaphoreSlim MovieDbResourcePool = new SemaphoreSlim(1, 1);
  32. internal static MovieDbProvider Current { get; private set; }
  33. /// <summary>
  34. /// Gets the json serializer.
  35. /// </summary>
  36. /// <value>The json serializer.</value>
  37. protected IJsonSerializer JsonSerializer { get; private set; }
  38. /// <summary>
  39. /// Gets the HTTP client.
  40. /// </summary>
  41. /// <value>The HTTP client.</value>
  42. protected IHttpClient HttpClient { get; private set; }
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="MovieDbProvider" /> class.
  45. /// </summary>
  46. /// <param name="logManager">The log manager.</param>
  47. /// <param name="configurationManager">The configuration manager.</param>
  48. /// <param name="jsonSerializer">The json serializer.</param>
  49. /// <param name="httpClient">The HTTP client.</param>
  50. /// <param name="providerManager">The provider manager.</param>
  51. public MovieDbProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IHttpClient httpClient, IProviderManager providerManager)
  52. : base(logManager, configurationManager)
  53. {
  54. JsonSerializer = jsonSerializer;
  55. HttpClient = httpClient;
  56. ProviderManager = providerManager;
  57. Current = this;
  58. }
  59. /// <summary>
  60. /// Releases unmanaged and - optionally - managed resources.
  61. /// </summary>
  62. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  63. protected virtual void Dispose(bool dispose)
  64. {
  65. if (dispose)
  66. {
  67. MovieDbResourcePool.Dispose();
  68. }
  69. }
  70. /// <summary>
  71. /// Gets the priority.
  72. /// </summary>
  73. /// <value>The priority.</value>
  74. public override MetadataProviderPriority Priority
  75. {
  76. get { return MetadataProviderPriority.Third; }
  77. }
  78. /// <summary>
  79. /// Supportses the specified item.
  80. /// </summary>
  81. /// <param name="item">The item.</param>
  82. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  83. public override bool Supports(BaseItem item)
  84. {
  85. var trailer = item as Trailer;
  86. if (trailer != null)
  87. {
  88. return !trailer.IsLocalTrailer;
  89. }
  90. // Don't support local trailers
  91. return item is Movie || item is BoxSet || item is MusicVideo;
  92. }
  93. /// <summary>
  94. /// Gets a value indicating whether [requires internet].
  95. /// </summary>
  96. /// <value><c>true</c> if [requires internet]; otherwise, <c>false</c>.</value>
  97. public override bool RequiresInternet
  98. {
  99. get
  100. {
  101. return true;
  102. }
  103. }
  104. protected override bool RefreshOnVersionChange
  105. {
  106. get
  107. {
  108. return true;
  109. }
  110. }
  111. protected override string ProviderVersion
  112. {
  113. get
  114. {
  115. return "2";
  116. }
  117. }
  118. /// <summary>
  119. /// The _TMDB settings task
  120. /// </summary>
  121. private TmdbSettingsResult _tmdbSettings;
  122. private readonly SemaphoreSlim _tmdbSettingsSemaphore = new SemaphoreSlim(1, 1);
  123. /// <summary>
  124. /// Gets the TMDB settings.
  125. /// </summary>
  126. /// <returns>Task{TmdbSettingsResult}.</returns>
  127. internal async Task<TmdbSettingsResult> GetTmdbSettings(CancellationToken cancellationToken)
  128. {
  129. if (_tmdbSettings != null)
  130. {
  131. return _tmdbSettings;
  132. }
  133. await _tmdbSettingsSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  134. try
  135. {
  136. // Check again in case it got populated while we were waiting.
  137. if (_tmdbSettings != null)
  138. {
  139. return _tmdbSettings;
  140. }
  141. using (var json = await GetMovieDbResponse(new HttpRequestOptions
  142. {
  143. Url = string.Format(TmdbConfigUrl, ApiKey),
  144. CancellationToken = cancellationToken,
  145. AcceptHeader = AcceptHeader
  146. }).ConfigureAwait(false))
  147. {
  148. _tmdbSettings = JsonSerializer.DeserializeFromStream<TmdbSettingsResult>(json);
  149. return _tmdbSettings;
  150. }
  151. }
  152. finally
  153. {
  154. _tmdbSettingsSemaphore.Release();
  155. }
  156. }
  157. private const string TmdbConfigUrl = "http://api.themoviedb.org/3/configuration?api_key={0}";
  158. private const string Search3 = @"http://api.themoviedb.org/3/search/{3}?api_key={1}&query={0}&language={2}";
  159. private const string GetMovieInfo3 = @"http://api.themoviedb.org/3/movie/{0}?api_key={1}&language={2}&append_to_response=casts,releases,images,keywords,trailers";
  160. private const string GetBoxSetInfo3 = @"http://api.themoviedb.org/3/collection/{0}?api_key={1}&language={2}&append_to_response=images";
  161. internal static string ApiKey = "f6bd687ffa63cd282b6ff2c6877f2669";
  162. internal static string AcceptHeader = "application/json,image/*";
  163. static readonly Regex[] NameMatches = new[] {
  164. new Regex(@"(?<name>.*)\((?<year>\d{4})\)"), // matches "My Movie (2001)" and gives us the name and the year
  165. new Regex(@"(?<name>.*)") // last resort matches the whole string as the name
  166. };
  167. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  168. {
  169. if (HasAltMeta(item))
  170. return false;
  171. // Boxsets require two passes because we need the children to be refreshed
  172. if (item is BoxSet && string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.TmdbCollection)))
  173. {
  174. return true;
  175. }
  176. return base.NeedsRefreshInternal(item, providerInfo);
  177. }
  178. /// <summary>
  179. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  180. /// </summary>
  181. /// <param name="item">The item.</param>
  182. /// <param name="force">if set to <c>true</c> [force].</param>
  183. /// <param name="cancellationToken">The cancellation token</param>
  184. /// <returns>Task{System.Boolean}.</returns>
  185. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  186. {
  187. cancellationToken.ThrowIfCancellationRequested();
  188. await FetchMovieData(item, cancellationToken).ConfigureAwait(false);
  189. SetLastRefreshed(item, DateTime.UtcNow);
  190. return true;
  191. }
  192. /// <summary>
  193. /// Determines whether [has alt meta] [the specified item].
  194. /// </summary>
  195. /// <param name="item">The item.</param>
  196. /// <returns><c>true</c> if [has alt meta] [the specified item]; otherwise, <c>false</c>.</returns>
  197. internal static bool HasAltMeta(BaseItem item)
  198. {
  199. if (item is BoxSet)
  200. {
  201. return item.LocationType == LocationType.FileSystem && item.ResolveArgs.ContainsMetaFileByName("collection.xml");
  202. }
  203. var path = MovieXmlSaver.GetMovieSavePath(item);
  204. if (item.LocationType == LocationType.FileSystem)
  205. {
  206. // If mixed with multiple movies in one folder, resolve args won't have the file system children
  207. return item.ResolveArgs.ContainsMetaFileByName(Path.GetFileName(path)) || File.Exists(path);
  208. }
  209. return false;
  210. }
  211. /// <summary>
  212. /// Fetches the movie data.
  213. /// </summary>
  214. /// <param name="item">The item.</param>
  215. /// <param name="cancellationToken"></param>
  216. /// <returns>Task.</returns>
  217. private async Task FetchMovieData(BaseItem item, CancellationToken cancellationToken)
  218. {
  219. var id = item.GetProviderId(MetadataProviders.Tmdb);
  220. if (string.IsNullOrEmpty(id))
  221. {
  222. id = item.GetProviderId(MetadataProviders.Imdb);
  223. }
  224. if (string.IsNullOrEmpty(id))
  225. {
  226. id = await FindId(item, cancellationToken).ConfigureAwait(false);
  227. }
  228. if (!string.IsNullOrEmpty(id))
  229. {
  230. Logger.Debug("MovieDbProvider - getting movie info with id: " + id);
  231. cancellationToken.ThrowIfCancellationRequested();
  232. await FetchMovieData(item, id, cancellationToken).ConfigureAwait(false);
  233. }
  234. else
  235. {
  236. Logger.Info("MovieDBProvider could not find " + item.Name + ". Check name on themoviedb.org.");
  237. }
  238. }
  239. /// <summary>
  240. /// Parses the name.
  241. /// </summary>
  242. /// <param name="name">The name.</param>
  243. /// <param name="justName">Name of the just.</param>
  244. /// <param name="year">The year.</param>
  245. protected void ParseName(string name, out string justName, out int? year)
  246. {
  247. justName = null;
  248. year = null;
  249. foreach (var re in NameMatches)
  250. {
  251. Match m = re.Match(name);
  252. if (m.Success)
  253. {
  254. justName = m.Groups["name"].Value.Trim();
  255. string y = m.Groups["year"] != null ? m.Groups["year"].Value : null;
  256. int temp;
  257. year = Int32.TryParse(y, out temp) ? temp : (int?)null;
  258. break;
  259. }
  260. }
  261. }
  262. /// <summary>
  263. /// Finds the id.
  264. /// </summary>
  265. /// <param name="item">The item.</param>
  266. /// <param name="cancellationToken">The cancellation token</param>
  267. /// <returns>Task{System.String}.</returns>
  268. public async Task<string> FindId(BaseItem item, CancellationToken cancellationToken)
  269. {
  270. int? yearInName;
  271. string name = item.Name;
  272. ParseName(name, out name, out yearInName);
  273. var year = item.ProductionYear ?? yearInName;
  274. Logger.Info("MovieDbProvider: Finding id for item: " + name);
  275. string language = ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower();
  276. //if we are a boxset - look at our first child
  277. var boxset = item as BoxSet;
  278. if (boxset != null)
  279. {
  280. // See if any movies have a collection id already
  281. var collId = boxset.Children.Concat(boxset.GetLinkedChildren()).OfType<Video>()
  282. .Select(i => i.GetProviderId(MetadataProviders.TmdbCollection))
  283. .FirstOrDefault(i => i != null);
  284. if (collId != null) return collId;
  285. }
  286. //nope - search for it
  287. var searchType = item is BoxSet ? "collection" : "movie";
  288. var id = await AttemptFindId(name, searchType, year, language, cancellationToken).ConfigureAwait(false);
  289. if (id == null)
  290. {
  291. //try in english if wasn't before
  292. if (language != "en")
  293. {
  294. id = await AttemptFindId(name, searchType, year, "en", cancellationToken).ConfigureAwait(false);
  295. }
  296. else
  297. {
  298. // try with dot and _ turned to space
  299. var originalName = name;
  300. name = name.Replace(",", " ");
  301. name = name.Replace(".", " ");
  302. name = name.Replace("_", " ");
  303. name = name.Replace("-", "");
  304. // Search again if the new name is different
  305. if (!string.Equals(name, originalName))
  306. {
  307. id = await AttemptFindId(name, searchType, year, language, cancellationToken).ConfigureAwait(false);
  308. if (id == null && language != "en")
  309. {
  310. //one more time, in english
  311. id = await AttemptFindId(name, searchType, year, "en", cancellationToken).ConfigureAwait(false);
  312. }
  313. }
  314. if (id == null && item.LocationType == LocationType.FileSystem)
  315. {
  316. //last resort - try using the actual folder name
  317. var pathName = Path.GetFileName(item.ResolveArgs.Path);
  318. // Only search if it's a name we haven't already tried.
  319. if (!string.Equals(pathName, name, StringComparison.OrdinalIgnoreCase)
  320. && !string.Equals(pathName, originalName, StringComparison.OrdinalIgnoreCase))
  321. {
  322. id = await AttemptFindId(pathName, searchType, year, "en", cancellationToken).ConfigureAwait(false);
  323. }
  324. }
  325. }
  326. }
  327. return id;
  328. }
  329. /// <summary>
  330. /// Attempts the find id.
  331. /// </summary>
  332. /// <param name="name">The name.</param>
  333. /// <param name="type">movie or collection</param>
  334. /// <param name="year">The year.</param>
  335. /// <param name="language">The language.</param>
  336. /// <param name="cancellationToken">The cancellation token</param>
  337. /// <returns>Task{System.String}.</returns>
  338. private async Task<string> AttemptFindId(string name, string type, int? year, string language, CancellationToken cancellationToken)
  339. {
  340. string url3 = string.Format(Search3, UrlEncode(name), ApiKey, language, type);
  341. TmdbMovieSearchResults searchResult = null;
  342. using (Stream json = await GetMovieDbResponse(new HttpRequestOptions
  343. {
  344. Url = url3,
  345. CancellationToken = cancellationToken,
  346. AcceptHeader = AcceptHeader
  347. }).ConfigureAwait(false))
  348. {
  349. searchResult = JsonSerializer.DeserializeFromStream<TmdbMovieSearchResults>(json);
  350. }
  351. if (searchResult != null)
  352. {
  353. foreach (var possible in searchResult.results)
  354. {
  355. string matchedName = possible.title ?? possible.name;
  356. string id = possible.id.ToString(CultureInfo.InvariantCulture);
  357. if (matchedName != null)
  358. {
  359. Logger.Debug("Match " + matchedName + " for " + name);
  360. if (year != null)
  361. {
  362. DateTime r;
  363. //These dates are always in this exact format
  364. if (DateTime.TryParseExact(possible.release_date, "yyyy-MM-dd", EnUs, DateTimeStyles.None, out r))
  365. {
  366. if (Math.Abs(r.Year - year.Value) > 1) // allow a 1 year tolerance on release date
  367. {
  368. Logger.Debug("Result " + matchedName + " released on " + r + " did not match year " + year);
  369. continue;
  370. }
  371. }
  372. }
  373. //matched name and year
  374. return id;
  375. }
  376. }
  377. }
  378. return null;
  379. }
  380. /// <summary>
  381. /// URLs the encode.
  382. /// </summary>
  383. /// <param name="name">The name.</param>
  384. /// <returns>System.String.</returns>
  385. private static string UrlEncode(string name)
  386. {
  387. return WebUtility.UrlEncode(name);
  388. }
  389. /// <summary>
  390. /// Fetches the movie data.
  391. /// </summary>
  392. /// <param name="item">The item.</param>
  393. /// <param name="id">The id.</param>
  394. /// <param name="cancellationToken">The cancellation token</param>
  395. /// <returns>Task.</returns>
  396. protected async Task FetchMovieData(BaseItem item, string id, CancellationToken cancellationToken)
  397. {
  398. cancellationToken.ThrowIfCancellationRequested();
  399. if (String.IsNullOrEmpty(id))
  400. {
  401. Logger.Info("MoviedbProvider: Ignoring " + item.Name + " because ID forced blank.");
  402. return;
  403. }
  404. item.SetProviderId(MetadataProviders.Tmdb, id);
  405. var mainResult = await FetchMainResult(item, id, cancellationToken).ConfigureAwait(false);
  406. if (mainResult == null) return;
  407. ProcessMainInfo(item, mainResult);
  408. }
  409. /// <summary>
  410. /// Fetches the main result.
  411. /// </summary>
  412. /// <param name="item">The item.</param>
  413. /// <param name="id">The id.</param>
  414. /// <param name="cancellationToken">The cancellation token</param>
  415. /// <returns>Task{CompleteMovieData}.</returns>
  416. protected async Task<CompleteMovieData> FetchMainResult(BaseItem item, string id, CancellationToken cancellationToken)
  417. {
  418. var baseUrl = item is BoxSet ? GetBoxSetInfo3 : GetMovieInfo3;
  419. string url = string.Format(baseUrl, id, ApiKey, ConfigurationManager.Configuration.PreferredMetadataLanguage);
  420. CompleteMovieData mainResult;
  421. cancellationToken.ThrowIfCancellationRequested();
  422. using (var json = await GetMovieDbResponse(new HttpRequestOptions
  423. {
  424. Url = url,
  425. CancellationToken = cancellationToken,
  426. AcceptHeader = AcceptHeader
  427. }).ConfigureAwait(false))
  428. {
  429. mainResult = JsonSerializer.DeserializeFromStream<CompleteMovieData>(json);
  430. }
  431. cancellationToken.ThrowIfCancellationRequested();
  432. if (mainResult != null && string.IsNullOrEmpty(mainResult.overview))
  433. {
  434. if (ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower() != "en")
  435. {
  436. Logger.Info("MovieDbProvider couldn't find meta for language " + ConfigurationManager.Configuration.PreferredMetadataLanguage + ". Trying English...");
  437. url = string.Format(baseUrl, id, ApiKey, "en");
  438. using (Stream json = await GetMovieDbResponse(new HttpRequestOptions
  439. {
  440. Url = url,
  441. CancellationToken = cancellationToken,
  442. AcceptHeader = AcceptHeader
  443. }).ConfigureAwait(false))
  444. {
  445. mainResult = JsonSerializer.DeserializeFromStream<CompleteMovieData>(json);
  446. }
  447. if (String.IsNullOrEmpty(mainResult.overview))
  448. {
  449. Logger.Error("MovieDbProvider - Unable to find information for " + item.Name + " (id:" + id + ")");
  450. return null;
  451. }
  452. }
  453. }
  454. return mainResult;
  455. }
  456. /// <summary>
  457. /// Processes the main info.
  458. /// </summary>
  459. /// <param name="movie">The movie.</param>
  460. /// <param name="movieData">The movie data.</param>
  461. protected virtual void ProcessMainInfo(BaseItem movie, CompleteMovieData movieData)
  462. {
  463. if (movie != null && movieData != null)
  464. {
  465. if (!movie.LockedFields.Contains(MetadataFields.Name))
  466. {
  467. movie.Name = movieData.title ?? movieData.original_title ?? movieData.name ?? movie.Name;
  468. }
  469. if (!movie.LockedFields.Contains(MetadataFields.Overview))
  470. {
  471. movie.Overview = WebUtility.HtmlDecode(movieData.overview);
  472. movie.Overview = movie.Overview != null ? movie.Overview.Replace("\n\n", "\n") : null;
  473. }
  474. movie.HomePageUrl = movieData.homepage;
  475. movie.Budget = movieData.budget;
  476. movie.Revenue = movieData.revenue;
  477. if (!string.IsNullOrEmpty(movieData.tagline))
  478. {
  479. movie.Taglines.Clear();
  480. movie.AddTagline(movieData.tagline);
  481. }
  482. movie.SetProviderId(MetadataProviders.Imdb, movieData.imdb_id);
  483. if (movieData.belongs_to_collection != null)
  484. {
  485. movie.SetProviderId(MetadataProviders.TmdbCollection, movieData.belongs_to_collection.id.ToString(CultureInfo.InvariantCulture));
  486. }
  487. else
  488. {
  489. movie.SetProviderId(MetadataProviders.TmdbCollection, null); // clear out any old entry
  490. }
  491. float rating;
  492. string voteAvg = movieData.vote_average.ToString(CultureInfo.InvariantCulture);
  493. //tmdb appears to have unified their numbers to always report "7.3" regardless of country
  494. // so I removed the culture-specific processing here because it was not working for other countries -ebr
  495. if (float.TryParse(voteAvg, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out rating))
  496. movie.CommunityRating = rating;
  497. movie.VoteCount = movieData.vote_count;
  498. //release date and certification are retrieved based on configured country and we fall back on US if not there and to minimun release date if still no match
  499. if (movieData.releases != null && movieData.releases.countries != null)
  500. {
  501. var ourRelease = movieData.releases.countries.FirstOrDefault(c => c.iso_3166_1.Equals(ConfigurationManager.Configuration.MetadataCountryCode, StringComparison.OrdinalIgnoreCase)) ?? new Country();
  502. var usRelease = movieData.releases.countries.FirstOrDefault(c => c.iso_3166_1.Equals("US", StringComparison.OrdinalIgnoreCase)) ?? new Country();
  503. var minimunRelease = movieData.releases.countries.OrderBy(c => c.release_date).FirstOrDefault() ?? new Country();
  504. if (!movie.LockedFields.Contains(MetadataFields.OfficialRating))
  505. {
  506. var ratingPrefix = ConfigurationManager.Configuration.MetadataCountryCode.Equals("us", StringComparison.OrdinalIgnoreCase) ? "" : ConfigurationManager.Configuration.MetadataCountryCode + "-";
  507. movie.OfficialRating = !string.IsNullOrEmpty(ourRelease.certification)
  508. ? ratingPrefix + ourRelease.certification
  509. : !string.IsNullOrEmpty(usRelease.certification)
  510. ? usRelease.certification
  511. : !string.IsNullOrEmpty(minimunRelease.certification)
  512. ? minimunRelease.iso_3166_1 + "-" + minimunRelease.certification
  513. : null;
  514. }
  515. if (ourRelease.release_date != default(DateTime))
  516. {
  517. if (ourRelease.release_date.Year != 1)
  518. {
  519. movie.PremiereDate = ourRelease.release_date.ToUniversalTime();
  520. movie.ProductionYear = ourRelease.release_date.Year;
  521. }
  522. }
  523. else if (usRelease.release_date != default(DateTime))
  524. {
  525. if (usRelease.release_date.Year != 1)
  526. {
  527. movie.PremiereDate = usRelease.release_date.ToUniversalTime();
  528. movie.ProductionYear = usRelease.release_date.Year;
  529. }
  530. }
  531. else if (minimunRelease.release_date != default(DateTime))
  532. {
  533. if (minimunRelease.release_date.Year != 1)
  534. {
  535. movie.PremiereDate = minimunRelease.release_date.ToUniversalTime();
  536. movie.ProductionYear = minimunRelease.release_date.Year;
  537. }
  538. }
  539. }
  540. else
  541. {
  542. if (movieData.release_date.Year != 1)
  543. {
  544. //no specific country release info at all
  545. movie.PremiereDate = movieData.release_date.ToUniversalTime();
  546. movie.ProductionYear = movieData.release_date.Year;
  547. }
  548. }
  549. //if that didn't find a rating and we are a boxset, use the one from our first child
  550. if (movie.OfficialRating == null && movie is BoxSet && !movie.LockedFields.Contains(MetadataFields.OfficialRating))
  551. {
  552. var boxset = movie as BoxSet;
  553. Logger.Info("MovieDbProvider - Using rating of first child of boxset...");
  554. var firstChild = boxset.Children.Concat(boxset.GetLinkedChildren()).FirstOrDefault();
  555. boxset.OfficialRating = firstChild != null ? firstChild.OfficialRating : null;
  556. }
  557. if (movieData.runtime > 0)
  558. movie.OriginalRunTimeTicks = TimeSpan.FromMinutes(movieData.runtime).Ticks;
  559. //studios
  560. if (movieData.production_companies != null && !movie.LockedFields.Contains(MetadataFields.Studios))
  561. {
  562. movie.Studios.Clear();
  563. foreach (var studio in movieData.production_companies.Select(c => c.name))
  564. {
  565. movie.AddStudio(studio);
  566. }
  567. }
  568. //genres
  569. if (movieData.genres != null && !movie.LockedFields.Contains(MetadataFields.Genres))
  570. {
  571. movie.Genres.Clear();
  572. foreach (var genre in movieData.genres.Select(g => g.name))
  573. {
  574. movie.AddGenre(genre);
  575. }
  576. }
  577. if (!movie.LockedFields.Contains(MetadataFields.Cast))
  578. {
  579. movie.People.Clear();
  580. //Actors, Directors, Writers - all in People
  581. //actors come from cast
  582. if (movieData.casts != null && movieData.casts.cast != null)
  583. {
  584. foreach (var actor in movieData.casts.cast.OrderBy(a => a.order)) movie.AddPerson(new PersonInfo { Name = actor.name.Trim(), Role = actor.character, Type = PersonType.Actor });
  585. }
  586. //and the rest from crew
  587. if (movieData.casts != null && movieData.casts.crew != null)
  588. {
  589. foreach (var person in movieData.casts.crew) movie.AddPerson(new PersonInfo { Name = person.name.Trim(), Role = person.job, Type = person.department });
  590. }
  591. }
  592. if (movieData.keywords != null && movieData.keywords.keywords != null && !movie.LockedFields.Contains(MetadataFields.Tags))
  593. {
  594. movie.Tags = movieData.keywords.keywords.Select(i => i.name).ToList();
  595. }
  596. if (movieData.trailers != null && movieData.trailers.youtube != null &&
  597. movieData.trailers.youtube.Count > 0)
  598. {
  599. movie.RemoteTrailers = movieData.trailers.youtube.Select(i => new MediaUrl
  600. {
  601. Url = string.Format("http://www.youtube.com/watch?v={0}", i.source),
  602. IsDirectLink = false,
  603. Name = i.name,
  604. VideoSize = string.Equals("hd", i.size, StringComparison.OrdinalIgnoreCase) ? VideoSize.HighDefinition : VideoSize.StandardDefinition
  605. }).ToList();
  606. }
  607. }
  608. }
  609. private DateTime _lastRequestDate = DateTime.MinValue;
  610. /// <summary>
  611. /// Gets the movie db response.
  612. /// </summary>
  613. internal async Task<Stream> GetMovieDbResponse(HttpRequestOptions options)
  614. {
  615. var cancellationToken = options.CancellationToken;
  616. await MovieDbResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  617. try
  618. {
  619. // Limit to three requests per second
  620. var diff = 340 - (DateTime.Now - _lastRequestDate).TotalMilliseconds;
  621. if (diff > 0)
  622. {
  623. await Task.Delay(Convert.ToInt32(diff), cancellationToken).ConfigureAwait(false);
  624. }
  625. _lastRequestDate = DateTime.Now;
  626. return await HttpClient.Get(options).ConfigureAwait(false);
  627. }
  628. finally
  629. {
  630. _lastRequestDate = DateTime.Now;
  631. MovieDbResourcePool.Release();
  632. }
  633. }
  634. #region Result Objects
  635. /// <summary>
  636. /// Class TmdbTitle
  637. /// </summary>
  638. protected class TmdbTitle
  639. {
  640. /// <summary>
  641. /// Gets or sets the iso_3166_1.
  642. /// </summary>
  643. /// <value>The iso_3166_1.</value>
  644. public string iso_3166_1 { get; set; }
  645. /// <summary>
  646. /// Gets or sets the title.
  647. /// </summary>
  648. /// <value>The title.</value>
  649. public string title { get; set; }
  650. }
  651. /// <summary>
  652. /// Class TmdbAltTitleResults
  653. /// </summary>
  654. protected class TmdbAltTitleResults
  655. {
  656. /// <summary>
  657. /// Gets or sets the id.
  658. /// </summary>
  659. /// <value>The id.</value>
  660. public int id { get; set; }
  661. /// <summary>
  662. /// Gets or sets the titles.
  663. /// </summary>
  664. /// <value>The titles.</value>
  665. public List<TmdbTitle> titles { get; set; }
  666. }
  667. /// <summary>
  668. /// Class TmdbMovieSearchResult
  669. /// </summary>
  670. protected class TmdbMovieSearchResult
  671. {
  672. /// <summary>
  673. /// Gets or sets a value indicating whether this <see cref="TmdbMovieSearchResult" /> is adult.
  674. /// </summary>
  675. /// <value><c>true</c> if adult; otherwise, <c>false</c>.</value>
  676. public bool adult { get; set; }
  677. /// <summary>
  678. /// Gets or sets the backdrop_path.
  679. /// </summary>
  680. /// <value>The backdrop_path.</value>
  681. public string backdrop_path { get; set; }
  682. /// <summary>
  683. /// Gets or sets the id.
  684. /// </summary>
  685. /// <value>The id.</value>
  686. public int id { get; set; }
  687. /// <summary>
  688. /// Gets or sets the original_title.
  689. /// </summary>
  690. /// <value>The original_title.</value>
  691. public string original_title { get; set; }
  692. /// <summary>
  693. /// Gets or sets the release_date.
  694. /// </summary>
  695. /// <value>The release_date.</value>
  696. public string release_date { get; set; }
  697. /// <summary>
  698. /// Gets or sets the poster_path.
  699. /// </summary>
  700. /// <value>The poster_path.</value>
  701. public string poster_path { get; set; }
  702. /// <summary>
  703. /// Gets or sets the popularity.
  704. /// </summary>
  705. /// <value>The popularity.</value>
  706. public double popularity { get; set; }
  707. /// <summary>
  708. /// Gets or sets the title.
  709. /// </summary>
  710. /// <value>The title.</value>
  711. public string title { get; set; }
  712. /// <summary>
  713. /// Gets or sets the vote_average.
  714. /// </summary>
  715. /// <value>The vote_average.</value>
  716. public double vote_average { get; set; }
  717. /// <summary>
  718. /// For collection search results
  719. /// </summary>
  720. public string name { get; set; }
  721. /// <summary>
  722. /// Gets or sets the vote_count.
  723. /// </summary>
  724. /// <value>The vote_count.</value>
  725. public int vote_count { get; set; }
  726. }
  727. /// <summary>
  728. /// Class TmdbMovieSearchResults
  729. /// </summary>
  730. protected class TmdbMovieSearchResults
  731. {
  732. /// <summary>
  733. /// Gets or sets the page.
  734. /// </summary>
  735. /// <value>The page.</value>
  736. public int page { get; set; }
  737. /// <summary>
  738. /// Gets or sets the results.
  739. /// </summary>
  740. /// <value>The results.</value>
  741. public List<TmdbMovieSearchResult> results { get; set; }
  742. /// <summary>
  743. /// Gets or sets the total_pages.
  744. /// </summary>
  745. /// <value>The total_pages.</value>
  746. public int total_pages { get; set; }
  747. /// <summary>
  748. /// Gets or sets the total_results.
  749. /// </summary>
  750. /// <value>The total_results.</value>
  751. public int total_results { get; set; }
  752. }
  753. protected class BelongsToCollection
  754. {
  755. public int id { get; set; }
  756. public string name { get; set; }
  757. public string poster_path { get; set; }
  758. public string backdrop_path { get; set; }
  759. }
  760. protected class GenreItem
  761. {
  762. public int id { get; set; }
  763. public string name { get; set; }
  764. }
  765. protected class ProductionCompany
  766. {
  767. public string name { get; set; }
  768. public int id { get; set; }
  769. }
  770. protected class ProductionCountry
  771. {
  772. public string iso_3166_1 { get; set; }
  773. public string name { get; set; }
  774. }
  775. protected class SpokenLanguage
  776. {
  777. public string iso_639_1 { get; set; }
  778. public string name { get; set; }
  779. }
  780. protected class Cast
  781. {
  782. public int id { get; set; }
  783. public string name { get; set; }
  784. public string character { get; set; }
  785. public int order { get; set; }
  786. public int cast_id { get; set; }
  787. public string profile_path { get; set; }
  788. }
  789. protected class Crew
  790. {
  791. public int id { get; set; }
  792. public string name { get; set; }
  793. public string department { get; set; }
  794. public string job { get; set; }
  795. public string profile_path { get; set; }
  796. }
  797. protected class Casts
  798. {
  799. public List<Cast> cast { get; set; }
  800. public List<Crew> crew { get; set; }
  801. }
  802. protected class Country
  803. {
  804. public string iso_3166_1 { get; set; }
  805. public string certification { get; set; }
  806. public DateTime release_date { get; set; }
  807. }
  808. protected class Releases
  809. {
  810. public List<Country> countries { get; set; }
  811. }
  812. protected class Keyword
  813. {
  814. public int id { get; set; }
  815. public string name { get; set; }
  816. }
  817. protected class Keywords
  818. {
  819. public List<Keyword> keywords { get; set; }
  820. }
  821. protected class CompleteMovieData
  822. {
  823. public bool adult { get; set; }
  824. public string backdrop_path { get; set; }
  825. public BelongsToCollection belongs_to_collection { get; set; }
  826. public int budget { get; set; }
  827. public List<GenreItem> genres { get; set; }
  828. public string homepage { get; set; }
  829. public int id { get; set; }
  830. public string imdb_id { get; set; }
  831. public string name { get; set; }
  832. public string original_title { get; set; }
  833. public string overview { get; set; }
  834. public double popularity { get; set; }
  835. public string poster_path { get; set; }
  836. public List<ProductionCompany> production_companies { get; set; }
  837. public List<ProductionCountry> production_countries { get; set; }
  838. public DateTime release_date { get; set; }
  839. public int revenue { get; set; }
  840. public int runtime { get; set; }
  841. public List<SpokenLanguage> spoken_languages { get; set; }
  842. public string status { get; set; }
  843. public string tagline { get; set; }
  844. public string title { get; set; }
  845. public double vote_average { get; set; }
  846. public int vote_count { get; set; }
  847. public Casts casts { get; set; }
  848. public Releases releases { get; set; }
  849. public Keywords keywords { get; set; }
  850. public Trailers trailers { get; set; }
  851. }
  852. public class Trailers
  853. {
  854. public List<Youtube> youtube { get; set; }
  855. }
  856. public class Youtube
  857. {
  858. public string name { get; set; }
  859. public string size { get; set; }
  860. public string source { get; set; }
  861. }
  862. public class TmdbImageSettings
  863. {
  864. public List<string> backdrop_sizes { get; set; }
  865. public string base_url { get; set; }
  866. public List<string> poster_sizes { get; set; }
  867. public List<string> profile_sizes { get; set; }
  868. }
  869. public class TmdbSettingsResult
  870. {
  871. public TmdbImageSettings images { get; set; }
  872. }
  873. #endregion
  874. public void Dispose()
  875. {
  876. Dispose(true);
  877. }
  878. }
  879. }