FanArtTVProvider.cs 6.7 KB

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