MovieDbProvider.cs 45 KB

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