MovieDbProvider.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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.Localization;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Model.Entities;
  10. using MediaBrowser.Model.Logging;
  11. using MediaBrowser.Model.Providers;
  12. using MediaBrowser.Model.Serialization;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Globalization;
  16. using System.IO;
  17. using System.Linq;
  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 : IRemoteMetadataProvider<Movie, MovieInfo>, IDisposable, IHasOrder
  26. {
  27. internal readonly SemaphoreSlim MovieDbResourcePool = new SemaphoreSlim(1, 1);
  28. internal static MovieDbProvider Current { get; private set; }
  29. private readonly IJsonSerializer _jsonSerializer;
  30. private readonly IHttpClient _httpClient;
  31. private readonly IFileSystem _fileSystem;
  32. private readonly IServerConfigurationManager _configurationManager;
  33. private readonly ILogger _logger;
  34. private readonly ILocalizationManager _localization;
  35. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  36. public MovieDbProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, IFileSystem fileSystem, IServerConfigurationManager configurationManager, ILogger logger, ILocalizationManager localization)
  37. {
  38. _jsonSerializer = jsonSerializer;
  39. _httpClient = httpClient;
  40. _fileSystem = fileSystem;
  41. _configurationManager = configurationManager;
  42. _logger = logger;
  43. _localization = localization;
  44. Current = this;
  45. }
  46. public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(MovieInfo searchInfo, CancellationToken cancellationToken)
  47. {
  48. return GetMovieSearchResults(searchInfo, cancellationToken);
  49. }
  50. public async Task<IEnumerable<RemoteSearchResult>> GetMovieSearchResults(ItemLookupInfo searchInfo, CancellationToken cancellationToken)
  51. {
  52. var tmdbSettings = await GetTmdbSettings(cancellationToken).ConfigureAwait(false);
  53. var tmdbImageUrl = tmdbSettings.images.base_url + "original";
  54. var tmdbId = searchInfo.GetProviderId(MetadataProviders.Tmdb);
  55. if (!string.IsNullOrEmpty(tmdbId))
  56. {
  57. cancellationToken.ThrowIfCancellationRequested();
  58. await EnsureMovieInfo(tmdbId, searchInfo.MetadataLanguage, cancellationToken).ConfigureAwait(false);
  59. var dataFilePath = GetDataFilePath(tmdbId, searchInfo.MetadataLanguage);
  60. var obj = _jsonSerializer.DeserializeFromFile<CompleteMovieData>(dataFilePath);
  61. var remoteResult = new RemoteSearchResult
  62. {
  63. Name = obj.title ?? obj.original_title ?? obj.name,
  64. SearchProviderName = Name,
  65. ImageUrl = string.IsNullOrWhiteSpace(obj.poster_path) ? null : tmdbImageUrl + obj.poster_path
  66. };
  67. if (!string.IsNullOrWhiteSpace(obj.release_date))
  68. {
  69. DateTime r;
  70. // These dates are always in this exact format
  71. if (DateTime.TryParse(obj.release_date, _usCulture, DateTimeStyles.None, out r))
  72. {
  73. remoteResult.PremiereDate = r.ToUniversalTime();
  74. remoteResult.ProductionYear = remoteResult.PremiereDate.Value.Year;
  75. }
  76. }
  77. remoteResult.SetProviderId(MetadataProviders.Tmdb, obj.id.ToString(_usCulture));
  78. if (!string.IsNullOrWhiteSpace(obj.imdb_id))
  79. {
  80. remoteResult.SetProviderId(MetadataProviders.Imdb, obj.imdb_id);
  81. }
  82. return new[] { remoteResult };
  83. }
  84. return await new MovieDbSearch(_logger, _jsonSerializer).GetMovieSearchResults(searchInfo, cancellationToken).ConfigureAwait(false);
  85. }
  86. public Task<MetadataResult<Movie>> GetMetadata(MovieInfo info, CancellationToken cancellationToken)
  87. {
  88. return GetItemMetadata<Movie>(info, cancellationToken);
  89. }
  90. public Task<MetadataResult<T>> GetItemMetadata<T>(ItemLookupInfo id, CancellationToken cancellationToken)
  91. where T : Video, new()
  92. {
  93. var movieDb = new GenericMovieDbInfo<T>(_logger, _jsonSerializer);
  94. return movieDb.GetMetadata(id, cancellationToken);
  95. }
  96. public string Name
  97. {
  98. get { return "TheMovieDb"; }
  99. }
  100. /// <summary>
  101. /// Releases unmanaged and - optionally - managed resources.
  102. /// </summary>
  103. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  104. protected virtual void Dispose(bool dispose)
  105. {
  106. if (dispose)
  107. {
  108. MovieDbResourcePool.Dispose();
  109. }
  110. }
  111. /// <summary>
  112. /// The _TMDB settings task
  113. /// </summary>
  114. private TmdbSettingsResult _tmdbSettings;
  115. /// <summary>
  116. /// Gets the TMDB settings.
  117. /// </summary>
  118. /// <returns>Task{TmdbSettingsResult}.</returns>
  119. internal async Task<TmdbSettingsResult> GetTmdbSettings(CancellationToken cancellationToken)
  120. {
  121. if (_tmdbSettings != null)
  122. {
  123. return _tmdbSettings;
  124. }
  125. using (var json = await GetMovieDbResponse(new HttpRequestOptions
  126. {
  127. Url = string.Format(TmdbConfigUrl, ApiKey),
  128. CancellationToken = cancellationToken,
  129. AcceptHeader = AcceptHeader
  130. }).ConfigureAwait(false))
  131. {
  132. _tmdbSettings = _jsonSerializer.DeserializeFromStream<TmdbSettingsResult>(json);
  133. return _tmdbSettings;
  134. }
  135. }
  136. private const string TmdbConfigUrl = "http://api.themoviedb.org/3/configuration?api_key={0}";
  137. private const string GetMovieInfo3 = @"http://api.themoviedb.org/3/movie/{0}?api_key={1}&append_to_response=casts,releases,images,keywords,trailers";
  138. internal static string ApiKey = "f6bd687ffa63cd282b6ff2c6877f2669";
  139. internal static string AcceptHeader = "application/json,image/*";
  140. /// <summary>
  141. /// Gets the movie data path.
  142. /// </summary>
  143. /// <param name="appPaths">The app paths.</param>
  144. /// <param name="tmdbId">The TMDB id.</param>
  145. /// <returns>System.String.</returns>
  146. internal static string GetMovieDataPath(IApplicationPaths appPaths, string tmdbId)
  147. {
  148. var dataPath = GetMoviesDataPath(appPaths);
  149. return Path.Combine(dataPath, tmdbId);
  150. }
  151. internal static string GetMoviesDataPath(IApplicationPaths appPaths)
  152. {
  153. var dataPath = Path.Combine(appPaths.CachePath, "tmdb-movies");
  154. return dataPath;
  155. }
  156. /// <summary>
  157. /// Downloads the movie info.
  158. /// </summary>
  159. /// <param name="id">The id.</param>
  160. /// <param name="preferredMetadataLanguage">The preferred metadata language.</param>
  161. /// <param name="cancellationToken">The cancellation token.</param>
  162. /// <returns>Task.</returns>
  163. internal async Task DownloadMovieInfo(string id, string preferredMetadataLanguage, CancellationToken cancellationToken)
  164. {
  165. var mainResult = await FetchMainResult(id, preferredMetadataLanguage, cancellationToken).ConfigureAwait(false);
  166. if (mainResult == null) return;
  167. var dataFilePath = GetDataFilePath(id, preferredMetadataLanguage);
  168. Directory.CreateDirectory(Path.GetDirectoryName(dataFilePath));
  169. _jsonSerializer.SerializeToFile(mainResult, dataFilePath);
  170. }
  171. private readonly Task _cachedTask = Task.FromResult(true);
  172. internal Task EnsureMovieInfo(string tmdbId, string language, CancellationToken cancellationToken)
  173. {
  174. if (string.IsNullOrEmpty(tmdbId))
  175. {
  176. throw new ArgumentNullException("tmdbId");
  177. }
  178. if (string.IsNullOrEmpty(language))
  179. {
  180. throw new ArgumentNullException("language");
  181. }
  182. var path = GetDataFilePath(tmdbId, language);
  183. var fileInfo = _fileSystem.GetFileSystemInfo(path);
  184. if (fileInfo.Exists)
  185. {
  186. // If it's recent or automatic updates are enabled, don't re-download
  187. if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 7)
  188. {
  189. return _cachedTask;
  190. }
  191. }
  192. return DownloadMovieInfo(tmdbId, language, cancellationToken);
  193. }
  194. internal string GetDataFilePath(string tmdbId, string preferredLanguage)
  195. {
  196. if (string.IsNullOrEmpty(tmdbId))
  197. {
  198. throw new ArgumentNullException("tmdbId");
  199. }
  200. if (string.IsNullOrEmpty(preferredLanguage))
  201. {
  202. throw new ArgumentNullException("preferredLanguage");
  203. }
  204. var path = GetMovieDataPath(_configurationManager.ApplicationPaths, tmdbId);
  205. var filename = string.Format("all-{0}.json",
  206. preferredLanguage ?? string.Empty);
  207. return Path.Combine(path, filename);
  208. }
  209. /// <summary>
  210. /// Fetches the main result.
  211. /// </summary>
  212. /// <param name="id">The id.</param>
  213. /// <param name="language">The language.</param>
  214. /// <param name="cancellationToken">The cancellation token</param>
  215. /// <returns>Task{CompleteMovieData}.</returns>
  216. internal async Task<CompleteMovieData> FetchMainResult(string id, string language, CancellationToken cancellationToken)
  217. {
  218. var url = string.Format(GetMovieInfo3, id, ApiKey);
  219. var imageLanguages = _localization.GetCultures()
  220. .Select(i => i.TwoLetterISOLanguageName)
  221. .Distinct(StringComparer.OrdinalIgnoreCase)
  222. .ToList();
  223. imageLanguages.Add("null");
  224. if (!string.IsNullOrEmpty(language))
  225. {
  226. // If preferred language isn't english, get those images too
  227. if (!imageLanguages.Contains(language, StringComparer.OrdinalIgnoreCase))
  228. {
  229. imageLanguages.Add(language);
  230. }
  231. url += string.Format("&language={0}", language);
  232. }
  233. // Get images in english and with no language
  234. url += "&include_image_language=" + string.Join(",", imageLanguages.ToArray());
  235. CompleteMovieData mainResult;
  236. cancellationToken.ThrowIfCancellationRequested();
  237. using (var json = await GetMovieDbResponse(new HttpRequestOptions
  238. {
  239. Url = url,
  240. CancellationToken = cancellationToken,
  241. AcceptHeader = AcceptHeader
  242. }).ConfigureAwait(false))
  243. {
  244. mainResult = _jsonSerializer.DeserializeFromStream<CompleteMovieData>(json);
  245. }
  246. cancellationToken.ThrowIfCancellationRequested();
  247. if (mainResult != null && string.IsNullOrEmpty(mainResult.overview))
  248. {
  249. if (!string.IsNullOrEmpty(language) && !string.Equals(language, "en", StringComparison.OrdinalIgnoreCase))
  250. {
  251. _logger.Info("MovieDbProvider couldn't find meta for language " + language + ". Trying English...");
  252. url = string.Format(GetMovieInfo3, id, ApiKey) + "&include_image_language=en,null&language=en";
  253. using (var json = await GetMovieDbResponse(new HttpRequestOptions
  254. {
  255. Url = url,
  256. CancellationToken = cancellationToken,
  257. AcceptHeader = AcceptHeader
  258. }).ConfigureAwait(false))
  259. {
  260. mainResult = _jsonSerializer.DeserializeFromStream<CompleteMovieData>(json);
  261. }
  262. if (String.IsNullOrEmpty(mainResult.overview))
  263. {
  264. _logger.Error("MovieDbProvider - Unable to find information for (id:" + id + ")");
  265. return null;
  266. }
  267. }
  268. }
  269. return mainResult;
  270. }
  271. private DateTime _lastRequestDate = DateTime.MinValue;
  272. /// <summary>
  273. /// Gets the movie db response.
  274. /// </summary>
  275. internal async Task<Stream> GetMovieDbResponse(HttpRequestOptions options)
  276. {
  277. var cancellationToken = options.CancellationToken;
  278. await MovieDbResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  279. try
  280. {
  281. // Limit to three requests per second
  282. var diff = 340 - (DateTime.Now - _lastRequestDate).TotalMilliseconds;
  283. if (diff > 0)
  284. {
  285. await Task.Delay(Convert.ToInt32(diff), cancellationToken).ConfigureAwait(false);
  286. }
  287. _lastRequestDate = DateTime.Now;
  288. return await _httpClient.Get(options).ConfigureAwait(false);
  289. }
  290. finally
  291. {
  292. _lastRequestDate = DateTime.Now;
  293. MovieDbResourcePool.Release();
  294. }
  295. }
  296. public bool HasChanged(IHasMetadata item, DateTime date)
  297. {
  298. if (!_configurationManager.Configuration.EnableTmdbUpdates)
  299. {
  300. return false;
  301. }
  302. var tmdbId = item.GetProviderId(MetadataProviders.Tmdb);
  303. if (!String.IsNullOrEmpty(tmdbId))
  304. {
  305. // Process images
  306. var dataFilePath = GetDataFilePath(tmdbId, item.GetPreferredMetadataLanguage());
  307. var fileInfo = new FileInfo(dataFilePath);
  308. return !fileInfo.Exists || _fileSystem.GetLastWriteTimeUtc(fileInfo) > date;
  309. }
  310. return false;
  311. }
  312. public void Dispose()
  313. {
  314. Dispose(true);
  315. }
  316. /// <summary>
  317. /// Class TmdbTitle
  318. /// </summary>
  319. internal class TmdbTitle
  320. {
  321. /// <summary>
  322. /// Gets or sets the iso_3166_1.
  323. /// </summary>
  324. /// <value>The iso_3166_1.</value>
  325. public string iso_3166_1 { get; set; }
  326. /// <summary>
  327. /// Gets or sets the title.
  328. /// </summary>
  329. /// <value>The title.</value>
  330. public string title { get; set; }
  331. }
  332. /// <summary>
  333. /// Class TmdbAltTitleResults
  334. /// </summary>
  335. internal class TmdbAltTitleResults
  336. {
  337. /// <summary>
  338. /// Gets or sets the id.
  339. /// </summary>
  340. /// <value>The id.</value>
  341. public int id { get; set; }
  342. /// <summary>
  343. /// Gets or sets the titles.
  344. /// </summary>
  345. /// <value>The titles.</value>
  346. public List<TmdbTitle> titles { get; set; }
  347. }
  348. internal class BelongsToCollection
  349. {
  350. public int id { get; set; }
  351. public string name { get; set; }
  352. public string poster_path { get; set; }
  353. public string backdrop_path { get; set; }
  354. }
  355. internal class GenreItem
  356. {
  357. public int id { get; set; }
  358. public string name { get; set; }
  359. }
  360. internal class ProductionCompany
  361. {
  362. public string name { get; set; }
  363. public int id { get; set; }
  364. }
  365. internal class ProductionCountry
  366. {
  367. public string iso_3166_1 { get; set; }
  368. public string name { get; set; }
  369. }
  370. internal class SpokenLanguage
  371. {
  372. public string iso_639_1 { get; set; }
  373. public string name { get; set; }
  374. }
  375. internal class Cast
  376. {
  377. public int id { get; set; }
  378. public string name { get; set; }
  379. public string character { get; set; }
  380. public int order { get; set; }
  381. public int cast_id { get; set; }
  382. public string profile_path { get; set; }
  383. }
  384. internal class Crew
  385. {
  386. public int id { get; set; }
  387. public string name { get; set; }
  388. public string department { get; set; }
  389. public string job { get; set; }
  390. public string profile_path { get; set; }
  391. }
  392. internal class Casts
  393. {
  394. public List<Cast> cast { get; set; }
  395. public List<Crew> crew { get; set; }
  396. }
  397. internal class Country
  398. {
  399. public string iso_3166_1 { get; set; }
  400. public string certification { get; set; }
  401. public DateTime release_date { get; set; }
  402. }
  403. internal class Releases
  404. {
  405. public List<Country> countries { get; set; }
  406. }
  407. internal class Backdrop
  408. {
  409. public string file_path { get; set; }
  410. public int width { get; set; }
  411. public int height { get; set; }
  412. public object iso_639_1 { get; set; }
  413. public double aspect_ratio { get; set; }
  414. public double vote_average { get; set; }
  415. public int vote_count { get; set; }
  416. }
  417. internal class Poster
  418. {
  419. public string file_path { get; set; }
  420. public int width { get; set; }
  421. public int height { get; set; }
  422. public string iso_639_1 { get; set; }
  423. public double aspect_ratio { get; set; }
  424. public double vote_average { get; set; }
  425. public int vote_count { get; set; }
  426. }
  427. internal class Images
  428. {
  429. public List<Backdrop> backdrops { get; set; }
  430. public List<Poster> posters { get; set; }
  431. }
  432. internal class Keyword
  433. {
  434. public int id { get; set; }
  435. public string name { get; set; }
  436. }
  437. internal class Keywords
  438. {
  439. public List<Keyword> keywords { get; set; }
  440. }
  441. internal class Youtube
  442. {
  443. public string name { get; set; }
  444. public string size { get; set; }
  445. public string source { get; set; }
  446. }
  447. internal class Trailers
  448. {
  449. public List<object> quicktime { get; set; }
  450. public List<Youtube> youtube { get; set; }
  451. }
  452. internal class CompleteMovieData
  453. {
  454. public bool adult { get; set; }
  455. public string backdrop_path { get; set; }
  456. public BelongsToCollection belongs_to_collection { get; set; }
  457. public int budget { get; set; }
  458. public List<GenreItem> genres { get; set; }
  459. public string homepage { get; set; }
  460. public int id { get; set; }
  461. public string imdb_id { get; set; }
  462. public string original_title { get; set; }
  463. public string overview { get; set; }
  464. public double popularity { get; set; }
  465. public string poster_path { get; set; }
  466. public List<ProductionCompany> production_companies { get; set; }
  467. public List<ProductionCountry> production_countries { get; set; }
  468. public string release_date { get; set; }
  469. public int revenue { get; set; }
  470. public int runtime { get; set; }
  471. public List<SpokenLanguage> spoken_languages { get; set; }
  472. public string status { get; set; }
  473. public string tagline { get; set; }
  474. public string title { get; set; }
  475. public string name { get; set; }
  476. public double vote_average { get; set; }
  477. public int vote_count { get; set; }
  478. public Casts casts { get; set; }
  479. public Releases releases { get; set; }
  480. public Images images { get; set; }
  481. public Keywords keywords { get; set; }
  482. public Trailers trailers { get; set; }
  483. }
  484. public int Order
  485. {
  486. get
  487. {
  488. // After Omdb
  489. return 1;
  490. }
  491. }
  492. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  493. {
  494. return _httpClient.GetResponse(new HttpRequestOptions
  495. {
  496. CancellationToken = cancellationToken,
  497. Url = url,
  498. ResourcePool = MovieDbResourcePool
  499. });
  500. }
  501. }
  502. }