FanArtTVProvider.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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.TV;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.Net;
  9. using System;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Xml;
  13. namespace MediaBrowser.Controller.Providers.TV
  14. {
  15. class FanArtTvProvider : FanartBaseProvider
  16. {
  17. protected string FanArtBaseUrl = "http://api.fanart.tv/webservice/series/{0}/{1}/xml/all/1/1";
  18. /// <summary>
  19. /// Gets the HTTP client.
  20. /// </summary>
  21. /// <value>The HTTP client.</value>
  22. protected IHttpClient HttpClient { get; private set; }
  23. private readonly IProviderManager _providerManager;
  24. public FanArtTvProvider(IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
  25. : base(logManager, configurationManager)
  26. {
  27. if (httpClient == null)
  28. {
  29. throw new ArgumentNullException("httpClient");
  30. }
  31. HttpClient = httpClient;
  32. _providerManager = providerManager;
  33. }
  34. public override bool Supports(BaseItem item)
  35. {
  36. return item is Series;
  37. }
  38. /// <summary>
  39. /// Needses the refresh internal.
  40. /// </summary>
  41. /// <param name="item">The item.</param>
  42. /// <param name="providerInfo">The provider info.</param>
  43. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  44. protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
  45. {
  46. if (string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Tvdb)))
  47. {
  48. return false;
  49. }
  50. if (!ConfigurationManager.Configuration.DownloadSeriesImages.Art &&
  51. !ConfigurationManager.Configuration.DownloadSeriesImages.Logo &&
  52. !ConfigurationManager.Configuration.DownloadSeriesImages.Thumb &&
  53. !ConfigurationManager.Configuration.DownloadSeriesImages.Backdrops &&
  54. !ConfigurationManager.Configuration.DownloadSeriesImages.Banner)
  55. {
  56. return false;
  57. }
  58. if (providerInfo.Data != GetComparisonData(item.GetProviderId(MetadataProviders.Tvdb)))
  59. {
  60. return true;
  61. }
  62. return base.NeedsRefreshInternal(item, providerInfo);
  63. }
  64. /// <summary>
  65. /// Gets the comparison data.
  66. /// </summary>
  67. /// <returns>Guid.</returns>
  68. private Guid GetComparisonData(string id)
  69. {
  70. return string.IsNullOrEmpty(id) ? Guid.Empty : id.GetMD5();
  71. }
  72. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  73. {
  74. cancellationToken.ThrowIfCancellationRequested();
  75. var status = ProviderRefreshStatus.Success;
  76. BaseProviderInfo data;
  77. if (!item.ProviderData.TryGetValue(Id, out data))
  78. {
  79. data = new BaseProviderInfo();
  80. item.ProviderData[Id] = data;
  81. }
  82. var series = (Series)item;
  83. string language = ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower();
  84. string url = string.Format(FanArtBaseUrl, APIKey, series.GetProviderId(MetadataProviders.Tvdb));
  85. var doc = new XmlDocument();
  86. using (var xml = await HttpClient.Get(new HttpRequestOptions
  87. {
  88. Url = url,
  89. ResourcePool = FanArtResourcePool,
  90. CancellationToken = cancellationToken,
  91. EnableResponseCache = true
  92. }).ConfigureAwait(false))
  93. {
  94. doc.Load(xml);
  95. }
  96. cancellationToken.ThrowIfCancellationRequested();
  97. if (doc.HasChildNodes)
  98. {
  99. string path;
  100. var hd = ConfigurationManager.Configuration.DownloadHDFanArt ? "hdtv" : "clear";
  101. if (ConfigurationManager.Configuration.DownloadSeriesImages.Logo && !series.ResolveArgs.ContainsMetaFileByName(LOGO_FILE))
  102. {
  103. var node = doc.SelectSingleNode("//fanart/series/" + hd + "logos/" + hd + "logo[@lang = \"" + language + "\"]/@url") ??
  104. doc.SelectSingleNode("//fanart/series/clearlogos/clearlogo[@lang = \"" + language + "\"]/@url") ??
  105. doc.SelectSingleNode("//fanart/series/" + hd + "logos/" + hd + "logo/@url") ??
  106. doc.SelectSingleNode("//fanart/series/clearlogos/clearlogo/@url");
  107. path = node != null ? node.Value : null;
  108. if (!string.IsNullOrEmpty(path))
  109. {
  110. Logger.Debug("FanArtProvider getting ClearLogo for " + series.Name);
  111. try
  112. {
  113. series.SetImage(ImageType.Logo, await _providerManager.DownloadAndSaveImage(series, path, LOGO_FILE, ConfigurationManager.Configuration.SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
  114. }
  115. catch (HttpException)
  116. {
  117. status = ProviderRefreshStatus.CompletedWithErrors;
  118. }
  119. }
  120. }
  121. cancellationToken.ThrowIfCancellationRequested();
  122. hd = ConfigurationManager.Configuration.DownloadHDFanArt ? "hd" : "";
  123. if (ConfigurationManager.Configuration.DownloadSeriesImages.Art && !series.ResolveArgs.ContainsMetaFileByName(ART_FILE))
  124. {
  125. var node = doc.SelectSingleNode("//fanart/series/" + hd + "cleararts/" + hd + "clearart[@lang = \"" + language + "\"]/@url") ??
  126. doc.SelectSingleNode("//fanart/series/cleararts/clearart[@lang = \"" + language + "\"]/@url") ??
  127. doc.SelectSingleNode("//fanart/series/" + hd + "cleararts/" + hd + "clearart/@url") ??
  128. doc.SelectSingleNode("//fanart/series/cleararts/clearart/@url");
  129. path = node != null ? node.Value : null;
  130. if (!string.IsNullOrEmpty(path))
  131. {
  132. Logger.Debug("FanArtProvider getting ClearArt for " + series.Name);
  133. try
  134. {
  135. series.SetImage(ImageType.Art, await _providerManager.DownloadAndSaveImage(series, path, ART_FILE, ConfigurationManager.Configuration.SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
  136. }
  137. catch (HttpException)
  138. {
  139. status = ProviderRefreshStatus.CompletedWithErrors;
  140. }
  141. }
  142. }
  143. cancellationToken.ThrowIfCancellationRequested();
  144. if (ConfigurationManager.Configuration.DownloadSeriesImages.Thumb && !series.ResolveArgs.ContainsMetaFileByName(THUMB_FILE))
  145. {
  146. var node = doc.SelectSingleNode("//fanart/series/tvthumbs/tvthumb[@lang = \"" + language + "\"]/@url") ??
  147. doc.SelectSingleNode("//fanart/series/tvthumbs/tvthumb/@url");
  148. path = node != null ? node.Value : null;
  149. if (!string.IsNullOrEmpty(path))
  150. {
  151. Logger.Debug("FanArtProvider getting ThumbArt for " + series.Name);
  152. try
  153. {
  154. series.SetImage(ImageType.Thumb, await _providerManager.DownloadAndSaveImage(series, path, THUMB_FILE, ConfigurationManager.Configuration.SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
  155. }
  156. catch (HttpException)
  157. {
  158. status = ProviderRefreshStatus.CompletedWithErrors;
  159. }
  160. }
  161. }
  162. if (ConfigurationManager.Configuration.DownloadSeriesImages.Banner && !series.ResolveArgs.ContainsMetaFileByName(BANNER_FILE))
  163. {
  164. var node = doc.SelectSingleNode("//fanart/series/tbbanners/tvbanner[@lang = \"" + language + "\"]/@url") ??
  165. doc.SelectSingleNode("//fanart/series/tbbanners/tvbanner/@url");
  166. path = node != null ? node.Value : null;
  167. if (!string.IsNullOrEmpty(path))
  168. {
  169. Logger.Debug("FanArtProvider getting banner for " + series.Name);
  170. try
  171. {
  172. series.SetImage(ImageType.Banner, await _providerManager.DownloadAndSaveImage(series, path, BANNER_FILE, ConfigurationManager.Configuration.SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
  173. }
  174. catch (HttpException)
  175. {
  176. status = ProviderRefreshStatus.CompletedWithErrors;
  177. }
  178. }
  179. }
  180. }
  181. data.Data = GetComparisonData(item.GetProviderId(MetadataProviders.Tvcom));
  182. SetLastRefreshed(series, DateTime.UtcNow, status);
  183. return true;
  184. }
  185. }
  186. }