From e19766b1b7e4735e18ebb0e997579f7671cc267c Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 29 Dec 2013 01:36:13 -0500 Subject: [PATCH] support manual downloading of studio images --- .../MediaBrowser.Providers.csproj | 6 +- .../Studios/StudioImageProvider.cs | 87 +++++------ .../Studios/StudiosManualImageProvider.cs | 135 ++++++++++++++++++ MediaBrowser.Providers/Studios/backdrops.txt | 0 .../Studios/{images.txt => posters.txt} | 5 + 5 files changed, 192 insertions(+), 41 deletions(-) create mode 100644 MediaBrowser.Providers/Studios/StudiosManualImageProvider.cs create mode 100644 MediaBrowser.Providers/Studios/backdrops.txt rename MediaBrowser.Providers/Studios/{images.txt => posters.txt} (99%) diff --git a/MediaBrowser.Providers/MediaBrowser.Providers.csproj b/MediaBrowser.Providers/MediaBrowser.Providers.csproj index c757a89cb..60a3ec163 100644 --- a/MediaBrowser.Providers/MediaBrowser.Providers.csproj +++ b/MediaBrowser.Providers/MediaBrowser.Providers.csproj @@ -121,6 +121,7 @@ + @@ -167,7 +168,10 @@ - + + + + diff --git a/MediaBrowser.Providers/Studios/StudioImageProvider.cs b/MediaBrowser.Providers/Studios/StudioImageProvider.cs index 28229fff5..7bb98a87c 100644 --- a/MediaBrowser.Providers/Studios/StudioImageProvider.cs +++ b/MediaBrowser.Providers/Studios/StudioImageProvider.cs @@ -4,10 +4,12 @@ using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Net; +using MediaBrowser.Model.Providers; using System; using System.Collections.Generic; -using System.IO; using System.Linq; +using System.Net; using System.Threading; using System.Threading.Tasks; @@ -47,7 +49,7 @@ namespace MediaBrowser.Providers.Studios protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo) { - if (!string.IsNullOrEmpty(item.PrimaryImagePath)) + if (!string.IsNullOrEmpty(item.PrimaryImagePath) && item.BackdropImagePaths.Count == 0) { return false; } @@ -67,65 +69,70 @@ namespace MediaBrowser.Providers.Studios { get { - return "1"; + return "3"; } } public override async Task FetchAsync(BaseItem item, bool force, BaseProviderInfo providerInfo, CancellationToken cancellationToken) { - if (string.IsNullOrEmpty(item.PrimaryImagePath)) + if (string.IsNullOrEmpty(item.PrimaryImagePath) || item.BackdropImagePaths.Count == 0) { - var list = GetAvailableImages(); + var images = await _providerManager.GetAvailableRemoteImages(item, cancellationToken, StudiosManualImageProvider.ProviderName).ConfigureAwait(false); - var match = FindMatch(item, list); - - if (!string.IsNullOrEmpty(match)) - { - var url = GetUrl(match); - - await _providerManager.SaveImage(item, url, _resourcePool, ImageType.Primary, null, cancellationToken).ConfigureAwait(false); - } + await DownloadImages(item, images.ToList(), cancellationToken).ConfigureAwait(false); } SetLastRefreshed(item, DateTime.UtcNow, providerInfo); return true; } - private string FindMatch(BaseItem item, IEnumerable images) + private async Task DownloadImages(BaseItem item, List images, CancellationToken cancellationToken) { - var name = GetComparableName(item.Name); - - return images.FirstOrDefault(i => string.Equals(name, GetComparableName(i), StringComparison.OrdinalIgnoreCase)); - } - - private string GetComparableName(string name) - { - return name.Replace(" ", string.Empty).Replace(".", string.Empty).Replace("&", string.Empty).Replace("!", string.Empty); - } - - private string GetUrl(string image) - { - return string.Format("https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/studios/{0}/folder.jpg", image); - } - - private IEnumerable GetAvailableImages() - { - var path = GetType().Namespace + ".images.txt"; - - using (var stream = GetType().Assembly.GetManifestResourceStream(path)) + if (!item.LockedFields.Contains(MetadataFields.Images)) { - using (var reader = new StreamReader(stream)) + cancellationToken.ThrowIfCancellationRequested(); + + if (!item.HasImage(ImageType.Primary)) { - var lines = new List(); + await SaveImage(item, images, ImageType.Primary, cancellationToken).ConfigureAwait(false); + } + } - while (!reader.EndOfStream) + if (!item.LockedFields.Contains(MetadataFields.Backdrops)) + { + cancellationToken.ThrowIfCancellationRequested(); + + if (item.BackdropImagePaths.Count == 0) + { + foreach (var image in images.Where(i => i.Type == ImageType.Backdrop)) { - var text = reader.ReadLine(); + await _providerManager.SaveImage(item, image.Url, _resourcePool, ImageType.Backdrop, null, cancellationToken) + .ConfigureAwait(false); - lines.Add(text); + break; } + } + } + } - return lines; + + private async Task SaveImage(BaseItem item, IEnumerable images, ImageType type, CancellationToken cancellationToken) + { + foreach (var image in images.Where(i => i.Type == type)) + { + try + { + await _providerManager.SaveImage(item, image.Url, _resourcePool, type, null, cancellationToken).ConfigureAwait(false); + break; + } + catch (HttpException ex) + { + // Sometimes fanart has bad url's in their xml + if (ex.StatusCode.HasValue && ex.StatusCode.Value == HttpStatusCode.NotFound) + { + continue; + } + break; } } } diff --git a/MediaBrowser.Providers/Studios/StudiosManualImageProvider.cs b/MediaBrowser.Providers/Studios/StudiosManualImageProvider.cs new file mode 100644 index 000000000..80ccbd6cb --- /dev/null +++ b/MediaBrowser.Providers/Studios/StudiosManualImageProvider.cs @@ -0,0 +1,135 @@ +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Providers; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Providers; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +namespace MediaBrowser.Providers.Studios +{ + public class StudiosManualImageProvider : IImageProvider + { + public string Name + { + get { return ProviderName; } + } + + public static string ProviderName + { + get { return "Media Browser"; } + } + + public bool Supports(IHasImages item) + { + return item is Studio; + } + + public Task> GetImages(IHasImages item, ImageType imageType, CancellationToken cancellationToken) + { + return GetImages(item, imageType == ImageType.Primary, imageType == ImageType.Backdrop, cancellationToken); + } + + public Task> GetAllImages(IHasImages item, CancellationToken cancellationToken) + { + return GetImages(item, true, true, cancellationToken); + } + + private Task> GetImages(IHasImages item, bool posters, bool backdrops, CancellationToken cancellationToken) + { + var list = new List(); + + if (posters) + { + list.Add(GetImage(item, "posters.txt", ImageType.Primary, "folder")); + } + + cancellationToken.ThrowIfCancellationRequested(); + + if (backdrops) + { + list.Add(GetImage(item, "backdrops.txt", ImageType.Backdrop, "backdrop")); + } + + return Task.FromResult(list.Where(i => i != null)); + } + + private RemoteImageInfo GetImage(IHasImages item, string filename, ImageType type, string remoteFilename) + { + var url = GetUrl(item, filename, remoteFilename); + + if (url != null) + { + return new RemoteImageInfo + { + ProviderName = Name, + Type = type, + Url = url + }; + } + + return null; + } + + private string GetUrl(IHasImages item, string listingFilename, string remoteFilename) + { + var list = GetAvailableImages(listingFilename); + + var match = FindMatch(item, list); + + if (!string.IsNullOrEmpty(match)) + { + return GetUrl(match, remoteFilename); + } + + return null; + } + + private string FindMatch(IHasImages item, IEnumerable images) + { + var name = GetComparableName(item.Name); + + return images.FirstOrDefault(i => string.Equals(name, GetComparableName(i), StringComparison.OrdinalIgnoreCase)); + } + + private string GetComparableName(string name) + { + return name.Replace(" ", string.Empty).Replace(".", string.Empty).Replace("&", string.Empty).Replace("!", string.Empty); + } + + private string GetUrl(string image, string filename) + { + return string.Format("https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/studios/{0}/{1}.jpg", image, filename); + } + + private IEnumerable GetAvailableImages(string filename) + { + var path = GetType().Namespace + "." + filename; + + using (var stream = GetType().Assembly.GetManifestResourceStream(path)) + { + using (var reader = new StreamReader(stream)) + { + var lines = new List(); + + while (!reader.EndOfStream) + { + var text = reader.ReadLine(); + + lines.Add(text); + } + + return lines; + } + } + } + + public int Priority + { + get { return 0; } + } + } +} diff --git a/MediaBrowser.Providers/Studios/backdrops.txt b/MediaBrowser.Providers/Studios/backdrops.txt new file mode 100644 index 000000000..e69de29bb diff --git a/MediaBrowser.Providers/Studios/images.txt b/MediaBrowser.Providers/Studios/posters.txt similarity index 99% rename from MediaBrowser.Providers/Studios/images.txt rename to MediaBrowser.Providers/Studios/posters.txt index 927ea30e1..0d3ad4611 100644 --- a/MediaBrowser.Providers/Studios/images.txt +++ b/MediaBrowser.Providers/Studios/posters.txt @@ -115,6 +115,7 @@ Digital Artists Digital Rights Group Digital Studios Discovery Channel +Discovery Distribber Diva DIY Network @@ -226,6 +227,7 @@ Indie Crush IndieFlix itsallinyourhands.tv ITV +ITV1 Janson Media Jim Henson Family TV K2 @@ -297,6 +299,7 @@ NBC Sports NBC Universal NBCU TV NCircle +Netflix New Renaissance NHL Nickelodeon @@ -386,6 +389,7 @@ SpaceRip SPEED Speed Racer Enterprises Spike +Spike TV Stand Up To Cancer Starz Strand Releasing @@ -453,6 +457,7 @@ Universal Television Univision unwrapped.tv USA +USA Network uStudio Vanguard Cinema Venevision