FanArtMovieProvider.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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.Library;
  8. using MediaBrowser.Controller.Providers;
  9. using MediaBrowser.Model.Entities;
  10. using MediaBrowser.Model.Logging;
  11. using MediaBrowser.Model.Providers;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. namespace MediaBrowser.Providers.Movies
  19. {
  20. /// <summary>
  21. /// Class FanArtMovieProvider
  22. /// </summary>
  23. class FanArtMovieProvider : FanartBaseProvider
  24. {
  25. /// <summary>
  26. /// Gets the HTTP client.
  27. /// </summary>
  28. /// <value>The HTTP client.</value>
  29. protected IHttpClient HttpClient { get; private set; }
  30. /// <summary>
  31. /// The _provider manager
  32. /// </summary>
  33. private readonly IProviderManager _providerManager;
  34. internal static FanArtMovieProvider Current { get; private set; }
  35. private readonly IFileSystem _fileSystem;
  36. /// <summary>
  37. /// Initializes a new instance of the <see cref="FanArtMovieProvider" /> class.
  38. /// </summary>
  39. /// <param name="httpClient">The HTTP client.</param>
  40. /// <param name="logManager">The log manager.</param>
  41. /// <param name="configurationManager">The configuration manager.</param>
  42. /// <param name="providerManager">The provider manager.</param>
  43. /// <exception cref="System.ArgumentNullException">httpClient</exception>
  44. public FanArtMovieProvider(IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager, IFileSystem fileSystem)
  45. : base(logManager, configurationManager)
  46. {
  47. if (httpClient == null)
  48. {
  49. throw new ArgumentNullException("httpClient");
  50. }
  51. HttpClient = httpClient;
  52. _providerManager = providerManager;
  53. _fileSystem = fileSystem;
  54. Current = this;
  55. }
  56. public override ItemUpdateType ItemUpdateType
  57. {
  58. get
  59. {
  60. return ItemUpdateType.ImageUpdate;
  61. }
  62. }
  63. /// <summary>
  64. /// Gets a value indicating whether [refresh on version change].
  65. /// </summary>
  66. /// <value><c>true</c> if [refresh on version change]; otherwise, <c>false</c>.</value>
  67. protected override bool RefreshOnVersionChange
  68. {
  69. get
  70. {
  71. return true;
  72. }
  73. }
  74. /// <summary>
  75. /// Gets the provider version.
  76. /// </summary>
  77. /// <value>The provider version.</value>
  78. protected override string ProviderVersion
  79. {
  80. get
  81. {
  82. return "13";
  83. }
  84. }
  85. public override MetadataProviderPriority Priority
  86. {
  87. get
  88. {
  89. return MetadataProviderPriority.Fifth;
  90. }
  91. }
  92. /// <summary>
  93. /// The fan art base URL
  94. /// </summary>
  95. protected string FanArtBaseUrl = "http://api.fanart.tv/webservice/movie/{0}/{1}/xml/all/1/1";
  96. /// <summary>
  97. /// Supportses the specified item.
  98. /// </summary>
  99. /// <param name="item">The item.</param>
  100. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  101. public override bool Supports(BaseItem item)
  102. {
  103. var trailer = item as Trailer;
  104. if (trailer != null)
  105. {
  106. return !trailer.IsLocalTrailer;
  107. }
  108. return item is Movie || item is BoxSet || item is MusicVideo;
  109. }
  110. /// <summary>
  111. /// Needses the refresh internal.
  112. /// </summary>
  113. /// <param name="item">The item.</param>
  114. /// <param name="providerInfo">The provider info.</param>
  115. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  116. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  117. {
  118. if (string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Tmdb)))
  119. {
  120. return false;
  121. }
  122. return base.NeedsRefreshInternal(item, providerInfo);
  123. }
  124. protected override bool NeedsRefreshBasedOnCompareDate(BaseItem item, BaseProviderInfo providerInfo)
  125. {
  126. var id = item.GetProviderId(MetadataProviders.Tmdb);
  127. if (!string.IsNullOrEmpty(id))
  128. {
  129. // Process images
  130. var xmlPath = GetFanartXmlPath(id);
  131. var fileInfo = new FileInfo(xmlPath);
  132. return !fileInfo.Exists || _fileSystem.GetLastWriteTimeUtc(fileInfo) > providerInfo.LastRefreshed;
  133. }
  134. return base.NeedsRefreshBasedOnCompareDate(item, providerInfo);
  135. }
  136. /// <summary>
  137. /// Gets the movie data path.
  138. /// </summary>
  139. /// <param name="appPaths">The app paths.</param>
  140. /// <param name="tmdbId">The TMDB id.</param>
  141. /// <returns>System.String.</returns>
  142. internal static string GetMovieDataPath(IApplicationPaths appPaths, string tmdbId)
  143. {
  144. var dataPath = Path.Combine(GetMoviesDataPath(appPaths), tmdbId);
  145. return dataPath;
  146. }
  147. /// <summary>
  148. /// Gets the movie data path.
  149. /// </summary>
  150. /// <param name="appPaths">The app paths.</param>
  151. /// <returns>System.String.</returns>
  152. internal static string GetMoviesDataPath(IApplicationPaths appPaths)
  153. {
  154. var dataPath = Path.Combine(appPaths.DataPath, "fanart-movies");
  155. return dataPath;
  156. }
  157. /// <summary>
  158. /// Fetches metadata and returns true or false indicating if any work that requires persistence was done
  159. /// </summary>
  160. /// <param name="item">The item.</param>
  161. /// <param name="force">if set to <c>true</c> [force].</param>
  162. /// <param name="cancellationToken">The cancellation token.</param>
  163. /// <returns>Task{System.Boolean}.</returns>
  164. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  165. {
  166. cancellationToken.ThrowIfCancellationRequested();
  167. var movieId = item.GetProviderId(MetadataProviders.Tmdb);
  168. if (!string.IsNullOrEmpty(movieId))
  169. {
  170. var xmlPath = GetFanartXmlPath(movieId);
  171. // Only download the xml if it doesn't already exist. The prescan task will take care of getting updates
  172. if (!File.Exists(xmlPath))
  173. {
  174. await DownloadMovieXml(movieId, cancellationToken).ConfigureAwait(false);
  175. }
  176. var images = await _providerManager.GetAvailableRemoteImages(item, cancellationToken, ManualFanartMovieImageProvider.ProviderName).ConfigureAwait(false);
  177. await FetchImages(item, images.ToList(), cancellationToken).ConfigureAwait(false);
  178. }
  179. SetLastRefreshed(item, DateTime.UtcNow);
  180. return true;
  181. }
  182. public string GetFanartXmlPath(string tmdbId)
  183. {
  184. var movieDataPath = GetMovieDataPath(ConfigurationManager.ApplicationPaths, tmdbId);
  185. return Path.Combine(movieDataPath, "fanart.xml");
  186. }
  187. /// <summary>
  188. /// Downloads the movie XML.
  189. /// </summary>
  190. /// <param name="tmdbId">The TMDB id.</param>
  191. /// <param name="cancellationToken">The cancellation token.</param>
  192. /// <returns>Task.</returns>
  193. internal async Task DownloadMovieXml(string tmdbId, CancellationToken cancellationToken)
  194. {
  195. cancellationToken.ThrowIfCancellationRequested();
  196. var url = string.Format(FanArtBaseUrl, ApiKey, tmdbId);
  197. var xmlPath = GetFanartXmlPath(tmdbId);
  198. Directory.CreateDirectory(Path.GetDirectoryName(xmlPath));
  199. using (var response = await HttpClient.Get(new HttpRequestOptions
  200. {
  201. Url = url,
  202. ResourcePool = FanArtResourcePool,
  203. CancellationToken = cancellationToken
  204. }).ConfigureAwait(false))
  205. {
  206. using (var xmlFileStream = _fileSystem.GetFileStream(xmlPath, FileMode.Create, FileAccess.Write, FileShare.Read, true))
  207. {
  208. await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
  209. }
  210. }
  211. }
  212. private async Task FetchImages(BaseItem item, List<RemoteImageInfo> images, CancellationToken cancellationToken)
  213. {
  214. cancellationToken.ThrowIfCancellationRequested();
  215. if (ConfigurationManager.Configuration.DownloadMovieImages.Primary && !item.HasImage(ImageType.Primary))
  216. {
  217. var image = images.FirstOrDefault(i => i.Type == ImageType.Primary);
  218. if (image != null)
  219. {
  220. await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Primary, null, cancellationToken).ConfigureAwait(false);
  221. }
  222. }
  223. cancellationToken.ThrowIfCancellationRequested();
  224. if (ConfigurationManager.Configuration.DownloadMovieImages.Logo && !item.HasImage(ImageType.Logo))
  225. {
  226. var image = images.FirstOrDefault(i => i.Type == ImageType.Logo);
  227. if (image != null)
  228. {
  229. await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Logo, null, cancellationToken).ConfigureAwait(false);
  230. }
  231. }
  232. cancellationToken.ThrowIfCancellationRequested();
  233. if (ConfigurationManager.Configuration.DownloadMovieImages.Art && !item.HasImage(ImageType.Art))
  234. {
  235. var image = images.FirstOrDefault(i => i.Type == ImageType.Art);
  236. if (image != null)
  237. {
  238. await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Art, null, cancellationToken).ConfigureAwait(false);
  239. }
  240. }
  241. cancellationToken.ThrowIfCancellationRequested();
  242. if (ConfigurationManager.Configuration.DownloadMovieImages.Disc && !item.HasImage(ImageType.Disc))
  243. {
  244. var image = images.FirstOrDefault(i => i.Type == ImageType.Disc);
  245. if (image != null)
  246. {
  247. await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Disc, null, cancellationToken).ConfigureAwait(false);
  248. }
  249. }
  250. cancellationToken.ThrowIfCancellationRequested();
  251. if (ConfigurationManager.Configuration.DownloadMovieImages.Banner && !item.HasImage(ImageType.Banner))
  252. {
  253. var image = images.FirstOrDefault(i => i.Type == ImageType.Banner);
  254. if (image != null)
  255. {
  256. await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Banner, null, cancellationToken).ConfigureAwait(false);
  257. }
  258. }
  259. cancellationToken.ThrowIfCancellationRequested();
  260. if (ConfigurationManager.Configuration.DownloadMovieImages.Thumb && !item.HasImage(ImageType.Thumb))
  261. {
  262. var image = images.FirstOrDefault(i => i.Type == ImageType.Thumb);
  263. if (image != null)
  264. {
  265. await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Thumb, null, cancellationToken).ConfigureAwait(false);
  266. }
  267. }
  268. cancellationToken.ThrowIfCancellationRequested();
  269. var backdropLimit = ConfigurationManager.Configuration.MaxBackdrops;
  270. if (ConfigurationManager.Configuration.DownloadMovieImages.Backdrops &&
  271. item.BackdropImagePaths.Count < backdropLimit)
  272. {
  273. var numBackdrops = item.BackdropImagePaths.Count;
  274. foreach (var image in images.Where(i => i.Type == ImageType.Backdrop))
  275. {
  276. await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Backdrop, numBackdrops, cancellationToken)
  277. .ConfigureAwait(false);
  278. numBackdrops++;
  279. if (item.BackdropImagePaths.Count >= backdropLimit) break;
  280. }
  281. }
  282. }
  283. }
  284. }