FanArtMovieProvider.cs 12 KB

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