FanArtTVProvider.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.Controller.Providers.Movies;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.Net;
  10. using System;
  11. using System.IO;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Xml;
  15. namespace MediaBrowser.Controller.Providers.TV
  16. {
  17. class FanArtTvProvider : FanartBaseProvider
  18. {
  19. protected string FanArtBaseUrl = "http://api.fanart.tv/webservice/series/{0}/{1}/xml/all/1/1";
  20. /// <summary>
  21. /// Gets the HTTP client.
  22. /// </summary>
  23. /// <value>The HTTP client.</value>
  24. protected IHttpClient HttpClient { get; private set; }
  25. private readonly IProviderManager _providerManager;
  26. public FanArtTvProvider(IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
  27. : base(logManager, configurationManager)
  28. {
  29. if (httpClient == null)
  30. {
  31. throw new ArgumentNullException("httpClient");
  32. }
  33. HttpClient = httpClient;
  34. _providerManager = providerManager;
  35. }
  36. public override bool Supports(BaseItem item)
  37. {
  38. return item is Series;
  39. }
  40. protected override bool ShouldFetch(BaseItem item, BaseProviderInfo providerInfo)
  41. {
  42. if (item.DontFetchMeta || string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Tvdb))) return false; //nothing to do
  43. var artExists = item.ResolveArgs.ContainsMetaFileByName(ART_FILE);
  44. var logoExists = item.ResolveArgs.ContainsMetaFileByName(LOGO_FILE);
  45. var thumbExists = item.ResolveArgs.ContainsMetaFileByName(THUMB_FILE);
  46. return (!artExists && ConfigurationManager.Configuration.DownloadSeriesImages.Art)
  47. || (!logoExists && ConfigurationManager.Configuration.DownloadSeriesImages.Logo)
  48. || (!thumbExists && ConfigurationManager.Configuration.DownloadSeriesImages.Thumb);
  49. }
  50. public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
  51. {
  52. cancellationToken.ThrowIfCancellationRequested();
  53. var series = (Series)item;
  54. if (ShouldFetch(series, series.ProviderData.GetValueOrDefault(Id, new BaseProviderInfo { ProviderId = Id })))
  55. {
  56. string language = ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower();
  57. string url = string.Format(FanArtBaseUrl, APIKey, series.GetProviderId(MetadataProviders.Tvdb));
  58. var doc = new XmlDocument();
  59. try
  60. {
  61. using (var xml = await HttpClient.Get(url, FanArtResourcePool, cancellationToken).ConfigureAwait(false))
  62. {
  63. doc.Load(xml);
  64. }
  65. }
  66. catch (HttpException)
  67. {
  68. }
  69. cancellationToken.ThrowIfCancellationRequested();
  70. if (doc.HasChildNodes)
  71. {
  72. string path;
  73. if (ConfigurationManager.Configuration.DownloadSeriesImages.Logo && !series.ResolveArgs.ContainsMetaFileByName(LOGO_FILE))
  74. {
  75. var node = doc.SelectSingleNode("//fanart/series/clearlogos/clearlogo[@lang = \"" + language + "\"]/@url") ??
  76. doc.SelectSingleNode("//fanart/series/clearlogos/clearlogo/@url");
  77. path = node != null ? node.Value : null;
  78. if (!string.IsNullOrEmpty(path))
  79. {
  80. Logger.Debug("FanArtProvider getting ClearLogo for " + series.Name);
  81. try
  82. {
  83. series.SetImage(ImageType.Logo, await _providerManager.DownloadAndSaveImage(series, path, LOGO_FILE, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
  84. }
  85. catch (HttpException)
  86. {
  87. }
  88. catch (IOException)
  89. {
  90. }
  91. }
  92. }
  93. cancellationToken.ThrowIfCancellationRequested();
  94. if (ConfigurationManager.Configuration.DownloadSeriesImages.Art && !series.ResolveArgs.ContainsMetaFileByName(ART_FILE))
  95. {
  96. var node = doc.SelectSingleNode("//fanart/series/cleararts/clearart[@lang = \"" + language + "\"]/@url") ??
  97. doc.SelectSingleNode("//fanart/series/cleararts/clearart/@url");
  98. path = node != null ? node.Value : null;
  99. if (!string.IsNullOrEmpty(path))
  100. {
  101. Logger.Debug("FanArtProvider getting ClearArt for " + series.Name);
  102. try
  103. {
  104. series.SetImage(ImageType.Art, await _providerManager.DownloadAndSaveImage(series, path, ART_FILE, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
  105. }
  106. catch (HttpException)
  107. {
  108. }
  109. catch (IOException)
  110. {
  111. }
  112. }
  113. }
  114. cancellationToken.ThrowIfCancellationRequested();
  115. if (ConfigurationManager.Configuration.DownloadSeriesImages.Thumb && !series.ResolveArgs.ContainsMetaFileByName(THUMB_FILE))
  116. {
  117. var node = doc.SelectSingleNode("//fanart/series/tvthumbs/tvthumb[@lang = \"" + language + "\"]/@url") ??
  118. doc.SelectSingleNode("//fanart/series/tvthumbs/tvthumb/@url");
  119. path = node != null ? node.Value : null;
  120. if (!string.IsNullOrEmpty(path))
  121. {
  122. Logger.Debug("FanArtProvider getting ThumbArt for " + series.Name);
  123. try
  124. {
  125. series.SetImage(ImageType.Disc, await _providerManager.DownloadAndSaveImage(series, path, THUMB_FILE, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
  126. }
  127. catch (HttpException)
  128. {
  129. }
  130. catch (IOException)
  131. {
  132. }
  133. }
  134. }
  135. }
  136. }
  137. SetLastRefreshed(series, DateTime.UtcNow);
  138. return true;
  139. }
  140. }
  141. }