From ab490d7467967c40435828bb4fc6c0066955930c Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sat, 2 Nov 2013 10:19:24 -0400 Subject: [PATCH] #605 - Add manual image selection for Series --- MediaBrowser.Providers/TV/FanArtTVProvider.cs | 105 +++++++----------- 1 file changed, 40 insertions(+), 65 deletions(-) diff --git a/MediaBrowser.Providers/TV/FanArtTVProvider.cs b/MediaBrowser.Providers/TV/FanArtTVProvider.cs index 0578887adf..31f8f9cb4a 100644 --- a/MediaBrowser.Providers/TV/FanArtTVProvider.cs +++ b/MediaBrowser.Providers/TV/FanArtTVProvider.cs @@ -8,12 +8,14 @@ using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Providers; using System; +using System.Collections.Generic; using System.Globalization; using System.IO; +using System.Linq; using System.Threading; using System.Threading.Tasks; -using System.Xml; namespace MediaBrowser.Providers.TV { @@ -173,10 +175,9 @@ namespace MediaBrowser.Providers.TV await DownloadSeriesXml(seriesId, cancellationToken).ConfigureAwait(false); } - if (File.Exists(xmlPath)) - { - await FetchFromXml(item, xmlPath, cancellationToken).ConfigureAwait(false); - } + var images = await _providerManager.GetAvailableRemoteImages(item, cancellationToken, ManualFanartSeriesImageProvider.ProviderName).ConfigureAwait(false); + + await FetchFromXml(item, images.ToList(), cancellationToken).ConfigureAwait(false); } SetLastRefreshed(item, DateTime.UtcNow); @@ -188,27 +189,20 @@ namespace MediaBrowser.Providers.TV /// Fetches from XML. /// /// The item. - /// The XML file path. + /// The images. /// The cancellation token. /// Task. - private async Task FetchFromXml(BaseItem item, string xmlFilePath, CancellationToken cancellationToken) + private async Task FetchFromXml(BaseItem item, List images, CancellationToken cancellationToken) { - var doc = new XmlDocument(); - doc.Load(xmlFilePath); - cancellationToken.ThrowIfCancellationRequested(); - var language = ConfigurationManager.Configuration.PreferredMetadataLanguage.ToLower(); - if (ConfigurationManager.Configuration.DownloadSeriesImages.Primary && !item.HasImage(ImageType.Primary)) { - var node = doc.SelectSingleNode("//fanart/series/tvposters/tvposter[@lang = \"" + language + "\"]/@url") ?? - doc.SelectSingleNode("//fanart/series/tvposters/tvposter/@url"); - var path = node != null ? node.Value : null; - if (!string.IsNullOrEmpty(path)) + var image = images.FirstOrDefault(i => i.Type == ImageType.Primary); + + if (image != null) { - await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Primary, null, cancellationToken) - .ConfigureAwait(false); + await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Primary, null, cancellationToken).ConfigureAwait(false); } } @@ -216,15 +210,11 @@ namespace MediaBrowser.Providers.TV if (ConfigurationManager.Configuration.DownloadSeriesImages.Logo && !item.HasImage(ImageType.Logo)) { - var node = doc.SelectSingleNode("//fanart/series/hdtvlogos/hdtvlogo[@lang = \"" + language + "\"]/@url") ?? - doc.SelectSingleNode("//fanart/series/clearlogos/clearlogo[@lang = \"" + language + "\"]/@url") ?? - doc.SelectSingleNode("//fanart/series/hdtvlogos/hdtvlogo/@url") ?? - doc.SelectSingleNode("//fanart/series/clearlogos/clearlogo/@url"); - var path = node != null ? node.Value : null; - if (!string.IsNullOrEmpty(path)) + var image = images.FirstOrDefault(i => i.Type == ImageType.Logo); + + if (image != null) { - await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Logo, null, cancellationToken) - .ConfigureAwait(false); + await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Logo, null, cancellationToken).ConfigureAwait(false); } } @@ -232,15 +222,11 @@ namespace MediaBrowser.Providers.TV if (ConfigurationManager.Configuration.DownloadSeriesImages.Art && !item.HasImage(ImageType.Art)) { - var node = doc.SelectSingleNode("//fanart/series/hdcleararts/hdclearart[@lang = \"" + language + "\"]/@url") ?? - doc.SelectSingleNode("//fanart/series/cleararts/clearart[@lang = \"" + language + "\"]/@url") ?? - doc.SelectSingleNode("//fanart/series/hdcleararts/hdclearart/@url") ?? - doc.SelectSingleNode("//fanart/series/cleararts/clearart/@url"); - var path = node != null ? node.Value : null; - if (!string.IsNullOrEmpty(path)) + var image = images.FirstOrDefault(i => i.Type == ImageType.Art); + + if (image != null) { - await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Art, null, cancellationToken) - .ConfigureAwait(false); + await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Art, null, cancellationToken).ConfigureAwait(false); } } @@ -248,53 +234,42 @@ namespace MediaBrowser.Providers.TV if (ConfigurationManager.Configuration.DownloadSeriesImages.Thumb && !item.HasImage(ImageType.Thumb)) { - var node = doc.SelectSingleNode("//fanart/series/tvthumbs/tvthumb[@lang = \"" + language + "\"]/@url") ?? - doc.SelectSingleNode("//fanart/series/tvthumbs/tvthumb/@url"); - var path = node != null ? node.Value : null; - if (!string.IsNullOrEmpty(path)) + var image = images.FirstOrDefault(i => i.Type == ImageType.Thumb); + + if (image != null) { - await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Thumb, null, cancellationToken) - .ConfigureAwait(false); + await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Thumb, null, cancellationToken).ConfigureAwait(false); } } + cancellationToken.ThrowIfCancellationRequested(); + if (ConfigurationManager.Configuration.DownloadSeriesImages.Banner && !item.HasImage(ImageType.Banner)) { - var node = doc.SelectSingleNode("//fanart/series/tbbanners/tvbanner[@lang = \"" + language + "\"]/@url") ?? - doc.SelectSingleNode("//fanart/series/tbbanners/tvbanner/@url"); - var path = node != null ? node.Value : null; - if (!string.IsNullOrEmpty(path)) + var image = images.FirstOrDefault(i => i.Type == ImageType.Banner); + + if (image != null) { - await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Banner, null, cancellationToken) - .ConfigureAwait(false); + await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Banner, null, cancellationToken).ConfigureAwait(false); } } - var backdropLimit = ConfigurationManager.Configuration.MaxBackdrops; + cancellationToken.ThrowIfCancellationRequested(); - if (ConfigurationManager.Configuration.DownloadMovieImages.Backdrops && item.BackdropImagePaths.Count < backdropLimit) + var backdropLimit = ConfigurationManager.Configuration.MaxBackdrops; + if (ConfigurationManager.Configuration.DownloadSeriesImages.Backdrops && + item.BackdropImagePaths.Count < backdropLimit) { - var nodes = doc.SelectNodes("//fanart/series/showbackgrounds//@url"); + var numBackdrops = item.BackdropImagePaths.Count; - if (nodes != null) + foreach (var image in images.Where(i => i.Type == ImageType.Backdrop)) { - var numBackdrops = item.BackdropImagePaths.Count; - - foreach (XmlNode node in nodes) - { - var path = node.Value; - - if (!string.IsNullOrEmpty(path) && !item.ContainsImageWithSourceUrl(path)) - { - await _providerManager.SaveImage(item, path, FanArtResourcePool, ImageType.Backdrop, numBackdrops, cancellationToken) - .ConfigureAwait(false); - - numBackdrops++; + await _providerManager.SaveImage(item, image.Url, FanArtResourcePool, ImageType.Backdrop, numBackdrops, cancellationToken) + .ConfigureAwait(false); - if (item.BackdropImagePaths.Count >= backdropLimit) break; - } - } + numBackdrops++; + if (item.BackdropImagePaths.Count >= backdropLimit) break; } }