MovieDbProvider.cs 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.Configuration;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Entities.Movies;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.Serialization;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Net;
  15. using System.Text;
  16. using System.Text.RegularExpressions;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. namespace MediaBrowser.Controller.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. private 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.Second; }
  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;
  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. /// <summary>
  105. /// If we save locally, refresh if they delete something
  106. /// </summary>
  107. protected override bool RefreshOnFileSystemStampChange
  108. {
  109. get
  110. {
  111. return ConfigurationManager.Configuration.SaveLocalMeta;
  112. }
  113. }
  114. protected override bool RefreshOnVersionChange
  115. {
  116. get
  117. {
  118. return true;
  119. }
  120. }
  121. protected override string ProviderVersion
  122. {
  123. get
  124. {
  125. return "2";
  126. }
  127. }
  128. /// <summary>
  129. /// The _TMDB settings task
  130. /// </summary>
  131. private TmdbSettingsResult _tmdbSettings;
  132. private readonly SemaphoreSlim _tmdbSettingsSemaphore = new SemaphoreSlim(1, 1);
  133. /// <summary>
  134. /// Gets the TMDB settings.
  135. /// </summary>
  136. /// <returns>Task{TmdbSettingsResult}.</returns>
  137. internal async Task<TmdbSettingsResult> GetTmdbSettings(CancellationToken cancellationToken)
  138. {
  139. if (_tmdbSettings != null)
  140. {
  141. return _tmdbSettings;
  142. }
  143. await _tmdbSettingsSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
  144. // Check again in case it got populated while we were waiting.
  145. if (_tmdbSettings != null)
  146. {
  147. _tmdbSettingsSemaphore.Release();
  148. return _tmdbSettings;
  149. }
  150. try
  151. {
  152. using (var json = await GetMovieDbResponse(new HttpRequestOptions
  153. {
  154. Url = string.Format(TmdbConfigUrl, ApiKey),
  155. CancellationToken = cancellationToken,
  156. AcceptHeader = AcceptHeader
  157. }).ConfigureAwait(false))
  158. {
  159. _tmdbSettings = JsonSerializer.DeserializeFromStream<TmdbSettingsResult>(json);
  160. return _tmdbSettings;
  161. }
  162. }
  163. finally
  164. {
  165. _tmdbSettingsSemaphore.Release();
  166. }
  167. }
  168. /// <summary>
  169. /// The json provider
  170. /// </summary>
  171. protected MovieProviderFromJson JsonProvider;
  172. /// <summary>
  173. /// Sets the persisted last refresh date on the item for this provider.
  174. /// </summary>
  175. /// <param name="item">The item.</param>
  176. /// <param name="value">The value.</param>
  177. /// <param name="providerVersion">The provider version.</param>
  178. /// <param name="status">The status.</param>
  179. public override void SetLastRefreshed(BaseItem item, DateTime value, string providerVersion, ProviderRefreshStatus status = ProviderRefreshStatus.Success)
  180. {
  181. base.SetLastRefreshed(item, value, providerVersion, status);
  182. if (ConfigurationManager.Configuration.SaveLocalMeta && item.LocationType == LocationType.FileSystem)
  183. {
  184. //in addition to ours, we need to set the last refreshed time for the local data provider
  185. //so it won't see the new files we download and process them all over again
  186. if (JsonProvider == null) JsonProvider = new MovieProviderFromJson(LogManager, ConfigurationManager, JsonSerializer, HttpClient, ProviderManager);
  187. BaseProviderInfo data;
  188. if (!item.ProviderData.TryGetValue(JsonProvider.Id, out data))
  189. {
  190. data = new BaseProviderInfo();
  191. }
  192. data.LastRefreshed = value;
  193. item.ProviderData[JsonProvider.Id] = data;
  194. }
  195. }
  196. private const string TmdbConfigUrl = "http://api.themoviedb.org/3/configuration?api_key={0}";
  197. private const string Search3 = @"http://api.themoviedb.org/3/search/movie?api_key={1}&query={0}&language={2}";
  198. private const string AltTitleSearch = @"http://api.themoviedb.org/3/movie/{0}/alternative_titles?api_key={1}&country={2}";
  199. private const string GetMovieInfo3 = @"http://api.themoviedb.org/3/movie/{0}?api_key={1}&language={2}&append_to_response=casts,releases,images,keywords";
  200. private const string GetBoxSetInfo3 = @"http://api.themoviedb.org/3/collection/{0}?api_key={1}&language={2}&append_to_response=images";
  201. internal static string ApiKey = "f6bd687ffa63cd282b6ff2c6877f2669";
  202. internal static string AcceptHeader = "application/json,image/*";
  203. static readonly Regex[] NameMatches = new[] {
  204. new Regex(@"(?<name>.*)\((?<year>\d{4})\)"), // matches "My Movie (2001)" and gives us the name and the year
  205. new Regex(@"(?<name>.*)") // last resort matches the whole string as the name
  206. };
  207. public const string LocalMetaFileName = "tmdb3.json";
  208. public const string AltMetaFileName = "movie.xml";
  209. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  210. {
  211. if (HasAltMeta(item))
  212. return false; //never refresh if has meta from other source
  213. if (item.LocationType == LocationType.FileSystem &&
  214. ConfigurationManager.Configuration.SaveLocalMeta &&
  215. HasFileSystemStampChanged(item, providerInfo))
  216. {
  217. //If they deleted something from file system, chances are, this item was mis-identified the first time
  218. item.SetProviderId(MetadataProviders.Tmdb, null);
  219. Logger.Debug("MovieProvider reports file system stamp change...");
  220. return true;
  221. }
  222. return base.NeedsRefreshInternal(item, providerInfo);
  223. }
  224. /// <summary>
  225. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  226. /// </summary>
  227. /// <param name="item">The item.</param>
  228. /// <param name="force">if set to <c>true</c> [force].</param>
  229. /// <param name="cancellationToken">The cancellation token</param>
  230. /// <returns>Task{System.Boolean}.</returns>
  231. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  232. {
  233. if (HasAltMeta(item))
  234. {
  235. Logger.Info("MovieDbProvider - Not fetching because 3rd party meta exists for " + item.Name);
  236. SetLastRefreshed(item, DateTime.UtcNow);
  237. return true;
  238. }
  239. cancellationToken.ThrowIfCancellationRequested();
  240. await FetchMovieData(item, cancellationToken).ConfigureAwait(false);
  241. SetLastRefreshed(item, DateTime.UtcNow);
  242. return true;
  243. }
  244. /// <summary>
  245. /// Determines whether [has local meta] [the specified item].
  246. /// </summary>
  247. /// <param name="item">The item.</param>
  248. /// <returns><c>true</c> if [has local meta] [the specified item]; otherwise, <c>false</c>.</returns>
  249. private bool HasLocalMeta(BaseItem item)
  250. {
  251. //need at least the xml and folder.jpg/png or a movie.xml put in by someone else
  252. return item.LocationType == LocationType.FileSystem && item.ResolveArgs.ContainsMetaFileByName(LocalMetaFileName);
  253. }
  254. /// <summary>
  255. /// Determines whether [has alt meta] [the specified item].
  256. /// </summary>
  257. /// <param name="item">The item.</param>
  258. /// <returns><c>true</c> if [has alt meta] [the specified item]; otherwise, <c>false</c>.</returns>
  259. private bool HasAltMeta(BaseItem item)
  260. {
  261. return item.LocationType == LocationType.FileSystem && item.ResolveArgs.ContainsMetaFileByName(AltMetaFileName);
  262. }
  263. /// <summary>
  264. /// Fetches the movie data.
  265. /// </summary>
  266. /// <param name="item">The item.</param>
  267. /// <param name="cancellationToken"></param>
  268. /// <returns>Task.</returns>
  269. private async Task FetchMovieData(BaseItem item, CancellationToken cancellationToken)
  270. {
  271. string id = item.GetProviderId(MetadataProviders.Tmdb) ?? await FindId(item, item.ProductionYear, cancellationToken).ConfigureAwait(false);
  272. if (id != null)
  273. {
  274. Logger.Debug("MovieDbProvider - getting movie info with id: " + id);
  275. cancellationToken.ThrowIfCancellationRequested();
  276. await FetchMovieData(item, id, cancellationToken).ConfigureAwait(false);
  277. }
  278. else
  279. {
  280. Logger.Info("MovieDBProvider could not find " + item.Name + ". Check name on themoviedb.org.");
  281. }
  282. }
  283. /// <summary>
  284. /// Parses the name.
  285. /// </summary>
  286. /// <param name="name">The name.</param>
  287. /// <param name="justName">Name of the just.</param>
  288. /// <param name="year">The year.</param>
  289. protected void ParseName(string name, out string justName, out int? year)
  290. {
  291. justName = null;
  292. year = null;
  293. foreach (var re in NameMatches)
  294. {
  295. Match m = re.Match(name);
  296. if (m.Success)
  297. {
  298. justName = m.Groups["name"].Value.Trim();
  299. string y = m.Groups["year"] != null ? m.Groups["year"].Value : null;
  300. int temp;
  301. year = Int32.TryParse(y, out temp) ? temp : (int?)null;
  302. break;
  303. }
  304. }
  305. }
  306. /// <summary>
  307. /// Finds the id.
  308. /// </summary>
  309. /// <param name="item">The item.</param>
  310. /// <param name="productionYear">The production year.</param>
  311. /// <param name="cancellationToken">The cancellation token</param>
  312. /// <returns>Task{System.String}.</returns>
  313. public async Task<string> FindId(BaseItem item, int? productionYear, CancellationToken cancellationToken)
  314. {
  315. string id = null;
  316. if (item.LocationType == LocationType.FileSystem)
  317. {
  318. string justName = item.Path != null ? item.Path.Substring(item.Path.LastIndexOf(Path.DirectorySeparatorChar)) : string.Empty;
  319. id = justName.GetAttributeValue("tmdbid");
  320. if (id != null)
  321. {
  322. Logger.Debug("Using tmdb id specified in path.");
  323. return id;
  324. }
  325. }
  326. int? year;
  327. string name = item.Name;
  328. ParseName(name, out name, out year);
  329. if (year == null && productionYear != null)
  330. {
  331. year = productionYear;
  332. }
  333. Logger.Info("MovieDbProvider: Finding id for movie: " + name);
  334. string language = ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower();
  335. //if we are a boxset - look at our first child
  336. var boxset = item as BoxSet;
  337. if (boxset != null)
  338. {
  339. var firstChild = boxset.Children.FirstOrDefault();
  340. if (firstChild != null)
  341. {
  342. Logger.Debug("MovieDbProvider - Attempting to find boxset ID from: " + firstChild.Name);
  343. string childName;
  344. int? childYear;
  345. ParseName(firstChild.Name, out childName, out childYear);
  346. id = await GetBoxsetIdFromMovie(childName, childYear, language, cancellationToken).ConfigureAwait(false);
  347. if (id != null)
  348. {
  349. Logger.Info("MovieDbProvider - Found Boxset ID: " + id);
  350. }
  351. }
  352. return id;
  353. }
  354. //nope - search for it
  355. id = await AttemptFindId(name, year, language, cancellationToken).ConfigureAwait(false);
  356. if (id == null)
  357. {
  358. //try in english if wasn't before
  359. if (language != "en")
  360. {
  361. id = await AttemptFindId(name, year, "en", cancellationToken).ConfigureAwait(false);
  362. }
  363. else
  364. {
  365. // try with dot and _ turned to space
  366. var originalName = name;
  367. name = name.Replace(",", " ");
  368. name = name.Replace(".", " ");
  369. name = name.Replace("_", " ");
  370. name = name.Replace("-", "");
  371. // Search again if the new name is different
  372. if (!string.Equals(name, originalName))
  373. {
  374. id = await AttemptFindId(name, year, language, cancellationToken).ConfigureAwait(false);
  375. if (id == null && language != "en")
  376. {
  377. //one more time, in english
  378. id = await AttemptFindId(name, year, "en", cancellationToken).ConfigureAwait(false);
  379. }
  380. }
  381. if (id == null && item.LocationType == LocationType.FileSystem)
  382. {
  383. //last resort - try using the actual folder name
  384. var pathName = Path.GetFileName(item.ResolveArgs.Path);
  385. // Only search if it's a name we haven't already tried.
  386. if (!string.Equals(pathName, name, StringComparison.OrdinalIgnoreCase)
  387. && !string.Equals(pathName, originalName, StringComparison.OrdinalIgnoreCase))
  388. {
  389. id = await AttemptFindId(pathName, year, "en", cancellationToken).ConfigureAwait(false);
  390. }
  391. }
  392. }
  393. }
  394. return id;
  395. }
  396. /// <summary>
  397. /// Attempts the find id.
  398. /// </summary>
  399. /// <param name="name">The name.</param>
  400. /// <param name="year">The year.</param>
  401. /// <param name="language">The language.</param>
  402. /// <param name="cancellationToken">The cancellation token</param>
  403. /// <returns>Task{System.String}.</returns>
  404. public virtual async Task<string> AttemptFindId(string name, int? year, string language, CancellationToken cancellationToken)
  405. {
  406. string url3 = string.Format(Search3, UrlEncode(name), ApiKey, language);
  407. TmdbMovieSearchResults searchResult = null;
  408. using (Stream json = await GetMovieDbResponse(new HttpRequestOptions
  409. {
  410. Url = url3,
  411. CancellationToken = cancellationToken,
  412. AcceptHeader = AcceptHeader
  413. }).ConfigureAwait(false))
  414. {
  415. searchResult = JsonSerializer.DeserializeFromStream<TmdbMovieSearchResults>(json);
  416. }
  417. if (searchResult == null || searchResult.results.Count == 0)
  418. {
  419. //try replacing numbers
  420. foreach (var pair in ReplaceStartNumbers)
  421. {
  422. if (name.StartsWith(pair.Key))
  423. {
  424. name = name.Remove(0, pair.Key.Length);
  425. name = pair.Value + name;
  426. }
  427. }
  428. foreach (var pair in ReplaceEndNumbers)
  429. {
  430. if (name.EndsWith(pair.Key))
  431. {
  432. name = name.Remove(name.IndexOf(pair.Key), pair.Key.Length);
  433. name = name + pair.Value;
  434. }
  435. }
  436. Logger.Info("MovieDBProvider - No results. Trying replacement numbers: " + name);
  437. url3 = string.Format(Search3, UrlEncode(name), ApiKey, language);
  438. using (var json = await GetMovieDbResponse(new HttpRequestOptions
  439. {
  440. Url = url3,
  441. CancellationToken = cancellationToken,
  442. AcceptHeader = AcceptHeader
  443. }).ConfigureAwait(false))
  444. {
  445. searchResult = JsonSerializer.DeserializeFromStream<TmdbMovieSearchResults>(json);
  446. }
  447. }
  448. if (searchResult != null)
  449. {
  450. string compName = GetComparableName(name, Logger);
  451. foreach (var possible in searchResult.results)
  452. {
  453. string matchedName = null;
  454. string id = possible.id.ToString(CultureInfo.InvariantCulture);
  455. string n = possible.title;
  456. if (GetComparableName(n, Logger) == compName)
  457. {
  458. matchedName = n;
  459. }
  460. else
  461. {
  462. n = possible.original_title;
  463. if (GetComparableName(n, Logger) == compName)
  464. {
  465. matchedName = n;
  466. }
  467. }
  468. Logger.Debug("MovieDbProvider - " + compName + " didn't match " + n);
  469. //if main title matches we don't have to look for alternatives
  470. if (matchedName == null)
  471. {
  472. //that title didn't match - look for alternatives
  473. url3 = string.Format(AltTitleSearch, id, ApiKey, ConfigurationManager.Configuration.MetadataCountryCode);
  474. using (var json = await GetMovieDbResponse(new HttpRequestOptions
  475. {
  476. Url = url3,
  477. CancellationToken = cancellationToken,
  478. AcceptHeader = AcceptHeader
  479. }).ConfigureAwait(false))
  480. {
  481. var response = JsonSerializer.DeserializeFromStream<TmdbAltTitleResults>(json);
  482. if (response != null && response.titles != null)
  483. {
  484. foreach (var title in response.titles)
  485. {
  486. var t = GetComparableName(title.title, Logger);
  487. if (t == compName)
  488. {
  489. Logger.Debug("MovieDbProvider - " + compName +
  490. " matched " + t);
  491. matchedName = t;
  492. break;
  493. }
  494. Logger.Debug("MovieDbProvider - " + compName +
  495. " did not match " + t);
  496. }
  497. }
  498. }
  499. }
  500. if (matchedName != null)
  501. {
  502. Logger.Debug("Match " + matchedName + " for " + name);
  503. if (year != null)
  504. {
  505. DateTime r;
  506. //These dates are always in this exact format
  507. if (DateTime.TryParseExact(possible.release_date, "yyyy-MM-dd", EnUs, DateTimeStyles.None, out r))
  508. {
  509. if (Math.Abs(r.Year - year.Value) > 1) // allow a 1 year tolerance on release date
  510. {
  511. Logger.Debug("Result " + matchedName + " released on " + r + " did not match year " + year);
  512. continue;
  513. }
  514. }
  515. }
  516. //matched name and year
  517. return id;
  518. }
  519. }
  520. }
  521. return null;
  522. }
  523. /// <summary>
  524. /// URLs the encode.
  525. /// </summary>
  526. /// <param name="name">The name.</param>
  527. /// <returns>System.String.</returns>
  528. private static string UrlEncode(string name)
  529. {
  530. return WebUtility.UrlEncode(name);
  531. }
  532. /// <summary>
  533. /// Gets the boxset id from movie.
  534. /// </summary>
  535. /// <param name="name">The name.</param>
  536. /// <param name="year">The year.</param>
  537. /// <param name="language">The language.</param>
  538. /// <param name="cancellationToken">The cancellation token</param>
  539. /// <returns>Task{System.String}.</returns>
  540. protected async Task<string> GetBoxsetIdFromMovie(string name, int? year, string language, CancellationToken cancellationToken)
  541. {
  542. string id = null;
  543. string childId = await AttemptFindId(name, year, language, cancellationToken).ConfigureAwait(false);
  544. if (childId != null)
  545. {
  546. string url = string.Format(GetMovieInfo3, childId, ApiKey, language);
  547. using (Stream json = await GetMovieDbResponse(new HttpRequestOptions
  548. {
  549. Url = url,
  550. CancellationToken = cancellationToken,
  551. AcceptHeader = AcceptHeader
  552. }).ConfigureAwait(false))
  553. {
  554. var movieResult = JsonSerializer.DeserializeFromStream<CompleteMovieData>(json);
  555. if (movieResult != null && movieResult.belongs_to_collection != null)
  556. {
  557. id = movieResult.belongs_to_collection.id.ToString(CultureInfo.InvariantCulture);
  558. }
  559. else
  560. {
  561. Logger.Error("Unable to obtain boxset id.");
  562. }
  563. }
  564. }
  565. return id;
  566. }
  567. /// <summary>
  568. /// Fetches the movie data.
  569. /// </summary>
  570. /// <param name="item">The item.</param>
  571. /// <param name="id">The id.</param>
  572. /// <param name="cancellationToken">The cancellation token</param>
  573. /// <returns>Task.</returns>
  574. protected async Task FetchMovieData(BaseItem item, string id, CancellationToken cancellationToken)
  575. {
  576. cancellationToken.ThrowIfCancellationRequested();
  577. if (String.IsNullOrEmpty(id))
  578. {
  579. Logger.Info("MoviedbProvider: Ignoring " + item.Name + " because ID forced blank.");
  580. return;
  581. }
  582. if (item.GetProviderId(MetadataProviders.Tmdb) == null) item.SetProviderId(MetadataProviders.Tmdb, id);
  583. var mainResult = await FetchMainResult(item, id, cancellationToken).ConfigureAwait(false);
  584. if (mainResult == null) return;
  585. ProcessMainInfo(item, mainResult);
  586. cancellationToken.ThrowIfCancellationRequested();
  587. //and save locally
  588. if (ConfigurationManager.Configuration.SaveLocalMeta && item.LocationType == LocationType.FileSystem)
  589. {
  590. var ms = new MemoryStream();
  591. JsonSerializer.SerializeToStream(mainResult, ms);
  592. cancellationToken.ThrowIfCancellationRequested();
  593. await ProviderManager.SaveToLibraryFilesystem(item, Path.Combine(item.MetaLocation, LocalMetaFileName), ms, cancellationToken).ConfigureAwait(false);
  594. }
  595. }
  596. /// <summary>
  597. /// Fetches the main result.
  598. /// </summary>
  599. /// <param name="item">The item.</param>
  600. /// <param name="id">The id.</param>
  601. /// <param name="cancellationToken">The cancellation token</param>
  602. /// <returns>Task{CompleteMovieData}.</returns>
  603. protected async Task<CompleteMovieData> FetchMainResult(BaseItem item, string id, CancellationToken cancellationToken)
  604. {
  605. var baseUrl = item is BoxSet ? GetBoxSetInfo3 : GetMovieInfo3;
  606. string url = string.Format(baseUrl, id, ApiKey, ConfigurationManager.Configuration.PreferredMetadataLanguage);
  607. CompleteMovieData mainResult;
  608. cancellationToken.ThrowIfCancellationRequested();
  609. using (var json = await GetMovieDbResponse(new HttpRequestOptions
  610. {
  611. Url = url,
  612. CancellationToken = cancellationToken,
  613. AcceptHeader = AcceptHeader
  614. }).ConfigureAwait(false))
  615. {
  616. mainResult = JsonSerializer.DeserializeFromStream<CompleteMovieData>(json);
  617. }
  618. cancellationToken.ThrowIfCancellationRequested();
  619. if (mainResult != null && string.IsNullOrEmpty(mainResult.overview))
  620. {
  621. if (ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower() != "en")
  622. {
  623. Logger.Info("MovieDbProvider couldn't find meta for language " + ConfigurationManager.Configuration.PreferredMetadataLanguage + ". Trying English...");
  624. url = string.Format(baseUrl, id, ApiKey, "en");
  625. using (Stream json = await GetMovieDbResponse(new HttpRequestOptions
  626. {
  627. Url = url,
  628. CancellationToken = cancellationToken,
  629. AcceptHeader = AcceptHeader
  630. }).ConfigureAwait(false))
  631. {
  632. mainResult = JsonSerializer.DeserializeFromStream<CompleteMovieData>(json);
  633. }
  634. if (String.IsNullOrEmpty(mainResult.overview))
  635. {
  636. Logger.Error("MovieDbProvider - Unable to find information for " + item.Name + " (id:" + id + ")");
  637. return null;
  638. }
  639. }
  640. }
  641. return mainResult;
  642. }
  643. /// <summary>
  644. /// Processes the main info.
  645. /// </summary>
  646. /// <param name="movie">The movie.</param>
  647. /// <param name="movieData">The movie data.</param>
  648. protected virtual void ProcessMainInfo(BaseItem movie, CompleteMovieData movieData)
  649. {
  650. if (movie != null && movieData != null)
  651. {
  652. movie.Name = movieData.title ?? movieData.original_title ?? movie.Name;
  653. movie.Overview = movieData.overview;
  654. movie.Overview = movie.Overview != null ? movie.Overview.Replace("\n\n", "\n") : null;
  655. movie.HomePageUrl = movieData.homepage;
  656. movie.Budget = movieData.budget;
  657. movie.Revenue = movieData.revenue;
  658. if (!string.IsNullOrEmpty(movieData.tagline))
  659. {
  660. movie.Taglines.Clear();
  661. movie.AddTagline(movieData.tagline);
  662. }
  663. movie.SetProviderId(MetadataProviders.Imdb, movieData.imdb_id);
  664. if (movieData.belongs_to_collection != null)
  665. {
  666. movie.SetProviderId(MetadataProviders.TmdbCollection, movieData.belongs_to_collection.id.ToString(CultureInfo.InvariantCulture));
  667. }
  668. float rating;
  669. string voteAvg = movieData.vote_average.ToString(CultureInfo.InvariantCulture);
  670. //tmdb appears to have unified their numbers to always report "7.3" regardless of country
  671. // so I removed the culture-specific processing here because it was not working for other countries -ebr
  672. if (float.TryParse(voteAvg, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out rating))
  673. movie.CommunityRating = rating;
  674. //release date and certification are retrieved based on configured country and we fall back on US if not there
  675. if (movieData.releases != null && movieData.releases.countries != null)
  676. {
  677. var ourRelease = movieData.releases.countries.FirstOrDefault(c => c.iso_3166_1.Equals(ConfigurationManager.Configuration.MetadataCountryCode, StringComparison.OrdinalIgnoreCase)) ?? new Country();
  678. var usRelease = movieData.releases.countries.FirstOrDefault(c => c.iso_3166_1.Equals("US", StringComparison.OrdinalIgnoreCase)) ?? new Country();
  679. movie.OfficialRating = ourRelease.certification ?? usRelease.certification;
  680. if (ourRelease.release_date > new DateTime(1900, 1, 1))
  681. {
  682. if (ourRelease.release_date.Year != 1)
  683. {
  684. movie.PremiereDate = ourRelease.release_date.ToUniversalTime();
  685. movie.ProductionYear = ourRelease.release_date.Year;
  686. }
  687. }
  688. else
  689. {
  690. if (usRelease.release_date.Year != 1)
  691. {
  692. movie.PremiereDate = usRelease.release_date.ToUniversalTime();
  693. movie.ProductionYear = usRelease.release_date.Year;
  694. }
  695. }
  696. }
  697. else
  698. {
  699. if (movieData.release_date.Year != 1)
  700. {
  701. //no specific country release info at all
  702. movie.PremiereDate = movieData.release_date.ToUniversalTime();
  703. movie.ProductionYear = movieData.release_date.Year;
  704. }
  705. }
  706. //if that didn't find a rating and we are a boxset, use the one from our first child
  707. if (movie.OfficialRating == null && movie is BoxSet)
  708. {
  709. var boxset = movie as BoxSet;
  710. Logger.Info("MovieDbProvider - Using rating of first child of boxset...");
  711. var firstChild = boxset.Children.FirstOrDefault();
  712. boxset.OfficialRating = firstChild != null ? firstChild.OfficialRating : null;
  713. }
  714. if (movieData.runtime > 0)
  715. movie.OriginalRunTimeTicks = TimeSpan.FromMinutes(movieData.runtime).Ticks;
  716. //studios
  717. if (movieData.production_companies != null)
  718. {
  719. movie.Studios.Clear();
  720. foreach (var studio in movieData.production_companies.Select(c => c.name))
  721. {
  722. movie.AddStudio(studio);
  723. }
  724. }
  725. //genres
  726. if (movieData.genres != null)
  727. {
  728. movie.Genres.Clear();
  729. foreach (var genre in movieData.genres.Select(g => g.name))
  730. {
  731. movie.AddGenre(genre);
  732. }
  733. }
  734. movie.People.Clear();
  735. movie.Tags.Clear();
  736. //Actors, Directors, Writers - all in People
  737. //actors come from cast
  738. if (movieData.casts != null && movieData.casts.cast != null)
  739. {
  740. foreach (var actor in movieData.casts.cast.OrderBy(a => a.order)) movie.AddPerson(new PersonInfo { Name = actor.name, Role = actor.character, Type = PersonType.Actor });
  741. }
  742. //and the rest from crew
  743. if (movieData.casts != null && movieData.casts.crew != null)
  744. {
  745. foreach (var person in movieData.casts.crew) movie.AddPerson(new PersonInfo { Name = person.name, Role = person.job, Type = person.department });
  746. }
  747. if (movieData.keywords != null && movieData.keywords.keywords != null)
  748. {
  749. movie.Tags = movieData.keywords.keywords.Select(i => i.name).ToList();
  750. }
  751. }
  752. }
  753. private DateTime _lastRequestDate = DateTime.MinValue;
  754. /// <summary>
  755. /// Gets the movie db response.
  756. /// </summary>
  757. internal async Task<Stream> GetMovieDbResponse(HttpRequestOptions options)
  758. {
  759. var cancellationToken = options.CancellationToken;
  760. await _movieDbResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  761. try
  762. {
  763. // Limit to three requests per second
  764. var diff = 340 - (DateTime.Now - _lastRequestDate).TotalMilliseconds;
  765. if (diff > 0)
  766. {
  767. await Task.Delay(Convert.ToInt32(diff), cancellationToken).ConfigureAwait(false);
  768. }
  769. _lastRequestDate = DateTime.Now;
  770. return await HttpClient.Get(options).ConfigureAwait(false);
  771. }
  772. finally
  773. {
  774. _lastRequestDate = DateTime.Now;
  775. _movieDbResourcePool.Release();
  776. }
  777. }
  778. /// <summary>
  779. /// The remove
  780. /// </summary>
  781. const string remove = "\"'!`?";
  782. // "Face/Off" support.
  783. /// <summary>
  784. /// The spacers
  785. /// </summary>
  786. const string spacers = "/,.:;\\(){}[]+-_=–*"; // (there are not actually two - in the they are different char codes)
  787. /// <summary>
  788. /// The replace start numbers
  789. /// </summary>
  790. static readonly Dictionary<string, string> ReplaceStartNumbers = new Dictionary<string, string> {
  791. {"1 ","one "},
  792. {"2 ","two "},
  793. {"3 ","three "},
  794. {"4 ","four "},
  795. {"5 ","five "},
  796. {"6 ","six "},
  797. {"7 ","seven "},
  798. {"8 ","eight "},
  799. {"9 ","nine "},
  800. {"10 ","ten "},
  801. {"11 ","eleven "},
  802. {"12 ","twelve "},
  803. {"13 ","thirteen "},
  804. {"100 ","one hundred "},
  805. {"101 ","one hundred one "}
  806. };
  807. /// <summary>
  808. /// The replace end numbers
  809. /// </summary>
  810. static readonly Dictionary<string, string> ReplaceEndNumbers = new Dictionary<string, string> {
  811. {" 1"," i"},
  812. {" 2"," ii"},
  813. {" 3"," iii"},
  814. {" 4"," iv"},
  815. {" 5"," v"},
  816. {" 6"," vi"},
  817. {" 7"," vii"},
  818. {" 8"," viii"},
  819. {" 9"," ix"},
  820. {" 10"," x"}
  821. };
  822. /// <summary>
  823. /// Gets the name of the comparable.
  824. /// </summary>
  825. /// <param name="name">The name.</param>
  826. /// <param name="logger">The logger.</param>
  827. /// <returns>System.String.</returns>
  828. internal static string GetComparableName(string name, ILogger logger)
  829. {
  830. name = name.ToLower();
  831. name = name.Replace("á", "a");
  832. name = name.Replace("é", "e");
  833. name = name.Replace("í", "i");
  834. name = name.Replace("ó", "o");
  835. name = name.Replace("ú", "u");
  836. name = name.Replace("ü", "u");
  837. name = name.Replace("ñ", "n");
  838. foreach (var pair in ReplaceStartNumbers)
  839. {
  840. if (name.StartsWith(pair.Key))
  841. {
  842. name = name.Remove(0, pair.Key.Length);
  843. name = pair.Value + name;
  844. logger.Info("MovieDbProvider - Replaced Start Numbers: " + name);
  845. }
  846. }
  847. foreach (var pair in ReplaceEndNumbers)
  848. {
  849. if (name.EndsWith(pair.Key))
  850. {
  851. name = name.Remove(name.IndexOf(pair.Key), pair.Key.Length);
  852. name = name + pair.Value;
  853. logger.Info("MovieDbProvider - Replaced End Numbers: " + name);
  854. }
  855. }
  856. name = name.Normalize(NormalizationForm.FormKD);
  857. var sb = new StringBuilder();
  858. foreach (var c in name)
  859. {
  860. if (c >= 0x2B0 && c <= 0x0333)
  861. {
  862. // skip char modifier and diacritics
  863. }
  864. else if (remove.IndexOf(c) > -1)
  865. {
  866. // skip chars we are removing
  867. }
  868. else if (spacers.IndexOf(c) > -1)
  869. {
  870. sb.Append(" ");
  871. }
  872. else if (c == '&')
  873. {
  874. sb.Append(" and ");
  875. }
  876. else
  877. {
  878. sb.Append(c);
  879. }
  880. }
  881. name = sb.ToString();
  882. name = name.Replace(", the", "");
  883. name = name.Replace(" the ", " ");
  884. name = name.Replace("the ", "");
  885. string prev_name;
  886. do
  887. {
  888. prev_name = name;
  889. name = name.Replace(" ", " ");
  890. } while (name.Length != prev_name.Length);
  891. return name.Trim();
  892. }
  893. #region Result Objects
  894. /// <summary>
  895. /// Class TmdbTitle
  896. /// </summary>
  897. protected class TmdbTitle
  898. {
  899. /// <summary>
  900. /// Gets or sets the iso_3166_1.
  901. /// </summary>
  902. /// <value>The iso_3166_1.</value>
  903. public string iso_3166_1 { get; set; }
  904. /// <summary>
  905. /// Gets or sets the title.
  906. /// </summary>
  907. /// <value>The title.</value>
  908. public string title { get; set; }
  909. }
  910. /// <summary>
  911. /// Class TmdbAltTitleResults
  912. /// </summary>
  913. protected class TmdbAltTitleResults
  914. {
  915. /// <summary>
  916. /// Gets or sets the id.
  917. /// </summary>
  918. /// <value>The id.</value>
  919. public int id { get; set; }
  920. /// <summary>
  921. /// Gets or sets the titles.
  922. /// </summary>
  923. /// <value>The titles.</value>
  924. public List<TmdbTitle> titles { get; set; }
  925. }
  926. /// <summary>
  927. /// Class TmdbMovieSearchResult
  928. /// </summary>
  929. protected class TmdbMovieSearchResult
  930. {
  931. /// <summary>
  932. /// Gets or sets a value indicating whether this <see cref="TmdbMovieSearchResult" /> is adult.
  933. /// </summary>
  934. /// <value><c>true</c> if adult; otherwise, <c>false</c>.</value>
  935. public bool adult { get; set; }
  936. /// <summary>
  937. /// Gets or sets the backdrop_path.
  938. /// </summary>
  939. /// <value>The backdrop_path.</value>
  940. public string backdrop_path { get; set; }
  941. /// <summary>
  942. /// Gets or sets the id.
  943. /// </summary>
  944. /// <value>The id.</value>
  945. public int id { get; set; }
  946. /// <summary>
  947. /// Gets or sets the original_title.
  948. /// </summary>
  949. /// <value>The original_title.</value>
  950. public string original_title { get; set; }
  951. /// <summary>
  952. /// Gets or sets the release_date.
  953. /// </summary>
  954. /// <value>The release_date.</value>
  955. public string release_date { get; set; }
  956. /// <summary>
  957. /// Gets or sets the poster_path.
  958. /// </summary>
  959. /// <value>The poster_path.</value>
  960. public string poster_path { get; set; }
  961. /// <summary>
  962. /// Gets or sets the popularity.
  963. /// </summary>
  964. /// <value>The popularity.</value>
  965. public double popularity { get; set; }
  966. /// <summary>
  967. /// Gets or sets the title.
  968. /// </summary>
  969. /// <value>The title.</value>
  970. public string title { get; set; }
  971. /// <summary>
  972. /// Gets or sets the vote_average.
  973. /// </summary>
  974. /// <value>The vote_average.</value>
  975. public double vote_average { get; set; }
  976. /// <summary>
  977. /// Gets or sets the vote_count.
  978. /// </summary>
  979. /// <value>The vote_count.</value>
  980. public int vote_count { get; set; }
  981. }
  982. /// <summary>
  983. /// Class TmdbMovieSearchResults
  984. /// </summary>
  985. protected class TmdbMovieSearchResults
  986. {
  987. /// <summary>
  988. /// Gets or sets the page.
  989. /// </summary>
  990. /// <value>The page.</value>
  991. public int page { get; set; }
  992. /// <summary>
  993. /// Gets or sets the results.
  994. /// </summary>
  995. /// <value>The results.</value>
  996. public List<TmdbMovieSearchResult> results { get; set; }
  997. /// <summary>
  998. /// Gets or sets the total_pages.
  999. /// </summary>
  1000. /// <value>The total_pages.</value>
  1001. public int total_pages { get; set; }
  1002. /// <summary>
  1003. /// Gets or sets the total_results.
  1004. /// </summary>
  1005. /// <value>The total_results.</value>
  1006. public int total_results { get; set; }
  1007. }
  1008. protected class BelongsToCollection
  1009. {
  1010. public int id { get; set; }
  1011. public string name { get; set; }
  1012. public string poster_path { get; set; }
  1013. public string backdrop_path { get; set; }
  1014. }
  1015. protected class GenreItem
  1016. {
  1017. public int id { get; set; }
  1018. public string name { get; set; }
  1019. }
  1020. protected class ProductionCompany
  1021. {
  1022. public string name { get; set; }
  1023. public int id { get; set; }
  1024. }
  1025. protected class ProductionCountry
  1026. {
  1027. public string iso_3166_1 { get; set; }
  1028. public string name { get; set; }
  1029. }
  1030. protected class SpokenLanguage
  1031. {
  1032. public string iso_639_1 { get; set; }
  1033. public string name { get; set; }
  1034. }
  1035. protected class Cast
  1036. {
  1037. public int id { get; set; }
  1038. public string name { get; set; }
  1039. public string character { get; set; }
  1040. public int order { get; set; }
  1041. public int cast_id { get; set; }
  1042. public string profile_path { get; set; }
  1043. }
  1044. protected class Crew
  1045. {
  1046. public int id { get; set; }
  1047. public string name { get; set; }
  1048. public string department { get; set; }
  1049. public string job { get; set; }
  1050. public string profile_path { get; set; }
  1051. }
  1052. protected class Casts
  1053. {
  1054. public List<Cast> cast { get; set; }
  1055. public List<Crew> crew { get; set; }
  1056. }
  1057. protected class Country
  1058. {
  1059. public string iso_3166_1 { get; set; }
  1060. public string certification { get; set; }
  1061. public DateTime release_date { get; set; }
  1062. }
  1063. protected class Releases
  1064. {
  1065. public List<Country> countries { get; set; }
  1066. }
  1067. protected class Keyword
  1068. {
  1069. public int id { get; set; }
  1070. public string name { get; set; }
  1071. }
  1072. protected class Keywords
  1073. {
  1074. public List<Keyword> keywords { get; set; }
  1075. }
  1076. protected class CompleteMovieData
  1077. {
  1078. public bool adult { get; set; }
  1079. public string backdrop_path { get; set; }
  1080. public BelongsToCollection belongs_to_collection { get; set; }
  1081. public int budget { get; set; }
  1082. public List<GenreItem> genres { get; set; }
  1083. public string homepage { get; set; }
  1084. public int id { get; set; }
  1085. public string imdb_id { get; set; }
  1086. public string original_title { get; set; }
  1087. public string overview { get; set; }
  1088. public double popularity { get; set; }
  1089. public string poster_path { get; set; }
  1090. public List<ProductionCompany> production_companies { get; set; }
  1091. public List<ProductionCountry> production_countries { get; set; }
  1092. public DateTime release_date { get; set; }
  1093. public int revenue { get; set; }
  1094. public int runtime { get; set; }
  1095. public List<SpokenLanguage> spoken_languages { get; set; }
  1096. public string status { get; set; }
  1097. public string tagline { get; set; }
  1098. public string title { get; set; }
  1099. public double vote_average { get; set; }
  1100. public int vote_count { get; set; }
  1101. public Casts casts { get; set; }
  1102. public Releases releases { get; set; }
  1103. public Keywords keywords { get; set; }
  1104. }
  1105. public class TmdbImageSettings
  1106. {
  1107. public List<string> backdrop_sizes { get; set; }
  1108. public string base_url { get; set; }
  1109. public List<string> poster_sizes { get; set; }
  1110. public List<string> profile_sizes { get; set; }
  1111. }
  1112. public class TmdbSettingsResult
  1113. {
  1114. public TmdbImageSettings images { get; set; }
  1115. }
  1116. #endregion
  1117. public void Dispose()
  1118. {
  1119. Dispose(true);
  1120. }
  1121. }
  1122. }