FanArtTVProvider.cs 11 KB

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