MovieDbProvider.cs 43 KB

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