Browse Source

fixes #174 - Support all fanart tv images

Luke Pulverenti 12 years ago
parent
commit
a0c5d27e85

+ 1 - 0
MediaBrowser.Controller/MediaBrowser.Controller.csproj

@@ -74,6 +74,7 @@
     <Compile Include="Entities\Audio\MusicAlbumDisc.cs" />
     <Compile Include="Library\ILibraryPrescanTask.cs" />
     <Compile Include="Providers\Movies\MovieDbImagesProvider.cs" />
+    <Compile Include="Providers\TV\FanArtSeasonProvider.cs" />
     <Compile Include="Providers\TV\TvdbPrescanTask.cs" />
     <Compile Include="Providers\TV\TvdbSeriesImageProvider.cs" />
     <Compile Include="Session\ISessionManager.cs" />

+ 1 - 2
MediaBrowser.Controller/Providers/Movies/FanArtMovieProvider.cs

@@ -179,8 +179,7 @@ namespace MediaBrowser.Controller.Providers.Movies
             {
                 Url = url,
                 ResourcePool = FanArtResourcePool,
-                CancellationToken = cancellationToken,
-                EnableResponseCache = true
+                CancellationToken = cancellationToken
 
             }).ConfigureAwait(false))
             {

+ 1 - 2
MediaBrowser.Controller/Providers/Movies/OpenMovieDatabaseProvider.cs

@@ -159,8 +159,7 @@ namespace MediaBrowser.Controller.Providers.Movies
             {
                 Url = url,
                 ResourcePool = _resourcePool,
-                CancellationToken = cancellationToken,
-                EnableResponseCache = true
+                CancellationToken = cancellationToken
 
             }).ConfigureAwait(false))
             {

+ 4 - 6
MediaBrowser.Controller/Providers/Music/FanArtAlbumProvider.cs

@@ -142,8 +142,7 @@ namespace MediaBrowser.Controller.Providers.Music
             {
                 Url = url,
                 ResourcePool = FanArtResourcePool,
-                CancellationToken = cancellationToken,
-                EnableResponseCache = true
+                CancellationToken = cancellationToken
 
             }).ConfigureAwait(false))
             {
@@ -154,7 +153,7 @@ namespace MediaBrowser.Controller.Providers.Music
 
             if (doc.HasChildNodes)
             {
-                if (ConfigurationManager.Configuration.DownloadMusicAlbumImages.Disc && !item.ResolveArgs.ContainsMetaFileByName(DiscFile))
+                if (ConfigurationManager.Configuration.DownloadMusicAlbumImages.Disc && !item.HasImage(ImageType.Disc))
                 {
                     var node = doc.SelectSingleNode("//fanart/music/albums/album/cdart/@url");
 
@@ -167,7 +166,7 @@ namespace MediaBrowser.Controller.Providers.Music
                     }
                 }
 
-                if (ConfigurationManager.Configuration.DownloadMusicAlbumImages.Primary && !item.ResolveArgs.ContainsMetaFileByName(PrimaryFile))
+                if (ConfigurationManager.Configuration.DownloadMusicAlbumImages.Primary && !item.HasImage(ImageType.Primary))
                 {
                     var node = doc.SelectSingleNode("//fanart/music/albums/album/albumcover/@url");
 
@@ -220,8 +219,7 @@ namespace MediaBrowser.Controller.Providers.Music
                 {
                     Url = url,
                     CancellationToken = cancellationToken,
-                    UserAgent = Environment.MachineName,
-                    EnableResponseCache = true
+                    UserAgent = Environment.MachineName
 
                 }).ConfigureAwait(false))
                 {

+ 4 - 6
MediaBrowser.Controller/Providers/Music/FanArtArtistProvider.cs

@@ -4,7 +4,6 @@ using MediaBrowser.Controller.Entities;
 using MediaBrowser.Controller.Entities.Audio;
 using MediaBrowser.Model.Entities;
 using MediaBrowser.Model.Logging;
-using MediaBrowser.Model.Net;
 using System;
 using System.Collections.Generic;
 using System.Globalization;
@@ -123,8 +122,7 @@ namespace MediaBrowser.Controller.Providers.Music
             {
                 Url = url,
                 ResourcePool = FanArtResourcePool,
-                CancellationToken = cancellationToken,
-                EnableResponseCache = true
+                CancellationToken = cancellationToken
 
             }).ConfigureAwait(false))
             {
@@ -176,7 +174,7 @@ namespace MediaBrowser.Controller.Providers.Music
 
                 cancellationToken.ThrowIfCancellationRequested();
 
-                if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Art && !item.ResolveArgs.ContainsMetaFileByName(ArtFile))
+                if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Art && !item.HasImage(ImageType.Art))
                 {
                     var node =
                         doc.SelectSingleNode("//fanart/music/musicarts/" + hd + "musicart/@url") ??
@@ -190,7 +188,7 @@ namespace MediaBrowser.Controller.Providers.Music
                 }
                 cancellationToken.ThrowIfCancellationRequested();
 
-                if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Banner && !item.ResolveArgs.ContainsMetaFileByName(BannerFile))
+                if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Banner && !item.HasImage(ImageType.Banner))
                 {
                     var node = doc.SelectSingleNode("//fanart/music/musicbanners/" + hd + "musicbanner/@url") ??
                                doc.SelectSingleNode("//fanart/music/musicbanners/musicbanner/@url");
@@ -205,7 +203,7 @@ namespace MediaBrowser.Controller.Providers.Music
                 cancellationToken.ThrowIfCancellationRequested();
 
                 // Artist thumbs are actually primary images (they are square/portrait)
-                if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Primary && !item.ResolveArgs.ContainsMetaFileByName(PrimaryFile))
+                if (ConfigurationManager.Configuration.DownloadMusicArtistImages.Primary && !item.HasImage(ImageType.Primary))
                 {
                     var node = doc.SelectSingleNode("//fanart/music/artistthumbs/artistthumb/@url");
                     path = node != null ? node.Value : null;

+ 209 - 0
MediaBrowser.Controller/Providers/TV/FanArtSeasonProvider.cs

@@ -0,0 +1,209 @@
+using MediaBrowser.Common.Extensions;
+using MediaBrowser.Controller.Configuration;
+using MediaBrowser.Controller.Entities;
+using MediaBrowser.Controller.Entities.TV;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Model.Entities;
+using MediaBrowser.Model.Logging;
+using System;
+using System.IO;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Xml;
+
+namespace MediaBrowser.Controller.Providers.TV
+{
+    class FanArtSeasonProvider : FanartBaseProvider
+    {
+        /// <summary>
+        /// The _provider manager
+        /// </summary>
+        private readonly IProviderManager _providerManager;
+        
+        public FanArtSeasonProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
+            : base(logManager, configurationManager)
+        {
+            _providerManager = providerManager;
+        }
+
+        public override bool Supports(BaseItem item)
+        {
+            return item is Season;
+        }
+
+        /// <summary>
+        /// Needses the refresh internal.
+        /// </summary>
+        /// <param name="item">The item.</param>
+        /// <param name="providerInfo">The provider info.</param>
+        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
+        protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
+        {
+            if (GetComparisonData(item) != providerInfo.Data)
+            {
+                return true;
+            }
+
+            return base.NeedsRefreshInternal(item, providerInfo);
+        }
+
+        /// <summary>
+        /// Gets the comparison data.
+        /// </summary>
+        /// <param name="item">The item.</param>
+        /// <returns>Guid.</returns>
+        private Guid GetComparisonData(BaseItem item)
+        {
+            var season = (Season)item;
+            var seriesId = season.Series != null ? season.Series.GetProviderId(MetadataProviders.Tvdb) : null;
+
+            if (!string.IsNullOrEmpty(seriesId))
+            {
+                // Process images
+                var imagesXmlPath = Path.Combine(FanArtTvProvider.GetSeriesDataPath(ConfigurationManager.ApplicationPaths, seriesId), "fanart.xml");
+
+                var imagesFileInfo = new FileInfo(imagesXmlPath);
+
+                return GetComparisonData(imagesFileInfo);
+            }
+
+            return Guid.Empty;
+        }
+
+        /// <summary>
+        /// Gets the comparison data.
+        /// </summary>
+        /// <param name="imagesFileInfo">The images file info.</param>
+        /// <returns>Guid.</returns>
+        private Guid GetComparisonData(FileInfo imagesFileInfo)
+        {
+            var date = imagesFileInfo.Exists ? imagesFileInfo.LastWriteTimeUtc : DateTime.MinValue;
+
+            var key = date.Ticks + imagesFileInfo.FullName;
+
+            return key.GetMD5();
+        }
+
+        public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
+        {
+            cancellationToken.ThrowIfCancellationRequested();
+
+            var season = (Season)item;
+
+            var seriesId = season.Series != null ? season.Series.GetProviderId(MetadataProviders.Tvdb) : null;
+
+            if (!string.IsNullOrEmpty(seriesId))
+            {
+                // Process images
+                var imagesXmlPath = Path.Combine(FanArtTvProvider.GetSeriesDataPath(ConfigurationManager.ApplicationPaths, seriesId), "fanart.xml");
+
+                var imagesFileInfo = new FileInfo(imagesXmlPath);
+
+                if (imagesFileInfo.Exists)
+                {
+                    if (!season.HasImage(ImageType.Primary) || !season.HasImage(ImageType.Banner) || season.BackdropImagePaths.Count == 0)
+                    {
+                        var xmlDoc = new XmlDocument();
+                        xmlDoc.Load(imagesXmlPath);
+
+                        await FetchImages(season, xmlDoc, cancellationToken).ConfigureAwait(false);
+                    }
+                }
+
+                BaseProviderInfo data;
+                if (!item.ProviderData.TryGetValue(Id, out data))
+                {
+                    data = new BaseProviderInfo();
+                    item.ProviderData[Id] = data;
+                }
+
+                data.Data = GetComparisonData(imagesFileInfo);
+
+                SetLastRefreshed(item, DateTime.UtcNow);
+                return true;
+            }
+
+            return false;
+        }
+
+        /// <summary>
+        /// Fetches the images.
+        /// </summary>
+        /// <param name="season">The season.</param>
+        /// <param name="doc">The doc.</param>
+        /// <param name="cancellationToken">The cancellation token.</param>
+        /// <returns>Task.</returns>
+        private async Task FetchImages(Season season, XmlDocument doc, CancellationToken cancellationToken)
+        {
+            var seasonNumber = season.IndexNumber ?? -1;
+
+            if (seasonNumber == -1)
+            {
+                return;
+            }
+
+            var language = ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower();
+            
+            if (ConfigurationManager.Configuration.DownloadSeasonImages.Thumb && !season.HasImage(ImageType.Thumb))
+            {
+                var node = doc.SelectSingleNode("//fanart/series/seasonthumbs/seasonthumb[@lang = \"" + language + "\"][@season = \"" + seasonNumber + "\"]/@url") ??
+                           doc.SelectSingleNode("//fanart/series/seasonthumbs/seasonthumb[@season = \"" + seasonNumber + "\"]/@url");
+                
+                var path = node != null ? node.Value : null;
+                
+                if (!string.IsNullOrEmpty(path))
+                {
+                    season.SetImage(ImageType.Thumb, await _providerManager.DownloadAndSaveImage(season, path, ThumbFile, ConfigurationManager.Configuration.SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
+                }
+            }
+        }
+
+        /// <summary>
+        /// Gets a value indicating whether [requires internet].
+        /// </summary>
+        /// <value><c>true</c> if [requires internet]; otherwise, <c>false</c>.</value>
+        public override bool RequiresInternet
+        {
+            get
+            {
+                return true;
+            }
+        }
+
+        /// <summary>
+        /// Returns true or false indicating if the provider should refresh when the contents of it's directory changes
+        /// </summary>
+        /// <value><c>true</c> if [refresh on file system stamp change]; otherwise, <c>false</c>.</value>
+        protected override bool RefreshOnFileSystemStampChange
+        {
+            get
+            {
+                return ConfigurationManager.Configuration.SaveLocalMeta;
+            }
+        }
+
+        /// <summary>
+        /// Gets a value indicating whether [refresh on version change].
+        /// </summary>
+        /// <value><c>true</c> if [refresh on version change]; otherwise, <c>false</c>.</value>
+        protected override bool RefreshOnVersionChange
+        {
+            get
+            {
+                return true;
+            }
+        }
+
+        /// <summary>
+        /// Gets the provider version.
+        /// </summary>
+        /// <value>The provider version.</value>
+        protected override string ProviderVersion
+        {
+            get
+            {
+                return "3";
+            }
+        }
+    }
+}

+ 77 - 11
MediaBrowser.Controller/Providers/TV/FanArtTVProvider.cs

@@ -1,5 +1,6 @@
-using System.Globalization;
+using MediaBrowser.Common.Configuration;
 using MediaBrowser.Common.Extensions;
+using MediaBrowser.Common.IO;
 using MediaBrowser.Common.Net;
 using MediaBrowser.Controller.Configuration;
 using MediaBrowser.Controller.Entities;
@@ -7,6 +8,8 @@ using MediaBrowser.Controller.Entities.TV;
 using MediaBrowser.Model.Entities;
 using MediaBrowser.Model.Logging;
 using System;
+using System.Globalization;
+using System.IO;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Xml;
@@ -80,6 +83,65 @@ namespace MediaBrowser.Controller.Providers.TV
             return string.IsNullOrEmpty(id) ? Guid.Empty : id.GetMD5();
         }
 
+        /// <summary>
+        /// Gets a value indicating whether [refresh on version change].
+        /// </summary>
+        /// <value><c>true</c> if [refresh on version change]; otherwise, <c>false</c>.</value>
+        protected override bool RefreshOnVersionChange
+        {
+            get
+            {
+                return true;
+            }
+        }
+
+        /// <summary>
+        /// Gets the provider version.
+        /// </summary>
+        /// <value>The provider version.</value>
+        protected override string ProviderVersion
+        {
+            get
+            {
+                return "1";
+            }
+        }
+
+        /// <summary>
+        /// Gets the series data path.
+        /// </summary>
+        /// <param name="appPaths">The app paths.</param>
+        /// <param name="seriesId">The series id.</param>
+        /// <returns>System.String.</returns>
+        internal static string GetSeriesDataPath(IApplicationPaths appPaths, string seriesId)
+        {
+            var seriesDataPath = Path.Combine(GetSeriesDataPath(appPaths), seriesId);
+
+            if (!Directory.Exists(seriesDataPath))
+            {
+                Directory.CreateDirectory(seriesDataPath);
+            }
+
+            return seriesDataPath;
+        }
+
+        /// <summary>
+        /// Gets the series data path.
+        /// </summary>
+        /// <param name="appPaths">The app paths.</param>
+        /// <returns>System.String.</returns>
+        internal static string GetSeriesDataPath(IApplicationPaths appPaths)
+        {
+            var dataPath = Path.Combine(appPaths.DataPath, "fanart-tv");
+
+            if (!Directory.Exists(dataPath))
+            {
+                Directory.CreateDirectory(dataPath);
+            }
+
+            return dataPath;
+        }
+        
         protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
         
         public override async Task<bool> FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken)
@@ -98,21 +160,29 @@ namespace MediaBrowser.Controller.Providers.TV
             var series = (Series)item;
 
             string language = ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower();
-            string url = string.Format(FanArtBaseUrl, ApiKey, series.GetProviderId(MetadataProviders.Tvdb));
-            var doc = new XmlDocument();
 
-            using (var xml = await HttpClient.Get(new HttpRequestOptions
+            var seriesId = series.GetProviderId(MetadataProviders.Tvdb);
+            string url = string.Format(FanArtBaseUrl, ApiKey, seriesId);
+
+            var xmlPath = Path.Combine(GetSeriesDataPath(ConfigurationManager.ApplicationPaths, seriesId), "fanart.xml");
+
+            using (var response = await HttpClient.Get(new HttpRequestOptions
             {
                 Url = url,
                 ResourcePool = FanArtResourcePool,
-                CancellationToken = cancellationToken,
-                EnableResponseCache = true
+                CancellationToken = cancellationToken
 
             }).ConfigureAwait(false))
             {
-                doc.Load(xml);
+                using (var xmlFileStream = new FileStream(xmlPath, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
+                {
+                    await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
+                }
             }
 
+            var doc = new XmlDocument();
+            doc.Load(xmlPath);
+
             cancellationToken.ThrowIfCancellationRequested();
 
             string path;
@@ -126,7 +196,6 @@ namespace MediaBrowser.Controller.Providers.TV
                 path = node != null ? node.Value : null;
                 if (!string.IsNullOrEmpty(path))
                 {
-                    Logger.Debug("FanArtProvider getting ClearLogo for " + series.Name);
                     series.SetImage(ImageType.Logo, await _providerManager.DownloadAndSaveImage(series, path, LogoFile, ConfigurationManager.Configuration.SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
                 }
             }
@@ -143,7 +212,6 @@ namespace MediaBrowser.Controller.Providers.TV
                 path = node != null ? node.Value : null;
                 if (!string.IsNullOrEmpty(path))
                 {
-                    Logger.Debug("FanArtProvider getting ClearArt for " + series.Name);
                     series.SetImage(ImageType.Art, await _providerManager.DownloadAndSaveImage(series, path, ArtFile, ConfigurationManager.Configuration.SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
                 }
             }
@@ -157,7 +225,6 @@ namespace MediaBrowser.Controller.Providers.TV
                 path = node != null ? node.Value : null;
                 if (!string.IsNullOrEmpty(path))
                 {
-                    Logger.Debug("FanArtProvider getting ThumbArt for " + series.Name);
                     series.SetImage(ImageType.Thumb, await _providerManager.DownloadAndSaveImage(series, path, ThumbFile, ConfigurationManager.Configuration.SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
                 }
             }
@@ -169,7 +236,6 @@ namespace MediaBrowser.Controller.Providers.TV
                 path = node != null ? node.Value : null;
                 if (!string.IsNullOrEmpty(path))
                 {
-                    Logger.Debug("FanArtProvider getting banner for " + series.Name);
                     series.SetImage(ImageType.Banner, await _providerManager.DownloadAndSaveImage(series, path, BannerFile, ConfigurationManager.Configuration.SaveLocalMeta, FanArtResourcePool, cancellationToken).ConfigureAwait(false));
                 }
             }

+ 2 - 15
MediaBrowser.Controller/Providers/TV/RemoteSeasonProvider.cs

@@ -1,5 +1,4 @@
 using MediaBrowser.Common.Extensions;
-using MediaBrowser.Common.Net;
 using MediaBrowser.Controller.Configuration;
 using MediaBrowser.Controller.Entities;
 using MediaBrowser.Controller.Entities.TV;
@@ -19,12 +18,6 @@ namespace MediaBrowser.Controller.Providers.TV
     /// </summary>
     class RemoteSeasonProvider : BaseMetadataProvider
     {
-        /// <summary>
-        /// Gets the HTTP client.
-        /// </summary>
-        /// <value>The HTTP client.</value>
-        protected IHttpClient HttpClient { get; private set; }
-
         /// <summary>
         /// The _provider manager
         /// </summary>
@@ -33,19 +26,13 @@ namespace MediaBrowser.Controller.Providers.TV
         /// <summary>
         /// Initializes a new instance of the <see cref="RemoteSeasonProvider"/> class.
         /// </summary>
-        /// <param name="httpClient">The HTTP client.</param>
         /// <param name="logManager">The log manager.</param>
         /// <param name="configurationManager">The configuration manager.</param>
         /// <param name="providerManager">The provider manager.</param>
         /// <exception cref="System.ArgumentNullException">httpClient</exception>
-        public RemoteSeasonProvider(IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
+        public RemoteSeasonProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager)
             : base(logManager, configurationManager)
         {
-            if (httpClient == null)
-            {
-                throw new ArgumentNullException("httpClient");
-            }
-            HttpClient = httpClient;
             _providerManager = providerManager;
         }
 
@@ -211,7 +198,7 @@ namespace MediaBrowser.Controller.Providers.TV
                 }
 
                 data.Data = GetComparisonData(imagesFileInfo);
-                
+
                 SetLastRefreshed(item, DateTime.UtcNow);
                 return true;
             }