diff --git a/MediaBrowser.Controller/Entities/Audio/Artist.cs b/MediaBrowser.Controller/Entities/Audio/Artist.cs index 5e14b0152..947ee1122 100644 --- a/MediaBrowser.Controller/Entities/Audio/Artist.cs +++ b/MediaBrowser.Controller/Entities/Audio/Artist.cs @@ -20,6 +20,7 @@ namespace MediaBrowser.Controller.Entities.Audio } public string LastFmImageUrl { get; set; } + public string LastFmImageSize { get; set; } /// /// Gets the user data key. diff --git a/MediaBrowser.Controller/Entities/Audio/MusicAlbum.cs b/MediaBrowser.Controller/Entities/Audio/MusicAlbum.cs index b96cd625f..cb64cfdfe 100644 --- a/MediaBrowser.Controller/Entities/Audio/MusicAlbum.cs +++ b/MediaBrowser.Controller/Entities/Audio/MusicAlbum.cs @@ -17,6 +17,7 @@ namespace MediaBrowser.Controller.Entities.Audio } public string LastFmImageUrl { get; set; } + public string LastFmImageSize { get; set; } /// /// Songs will group into us so don't also include us in the index diff --git a/MediaBrowser.Controller/Entities/Audio/MusicArtist.cs b/MediaBrowser.Controller/Entities/Audio/MusicArtist.cs index 75231ec6a..d791c92ae 100644 --- a/MediaBrowser.Controller/Entities/Audio/MusicArtist.cs +++ b/MediaBrowser.Controller/Entities/Audio/MusicArtist.cs @@ -11,6 +11,7 @@ namespace MediaBrowser.Controller.Entities.Audio /// /// The last fm image URL. public string LastFmImageUrl { get; set; } + public string LastFmImageSize { get; set; } /// /// Gets the user data key. diff --git a/MediaBrowser.Providers/Music/LastFmImageProvider.cs b/MediaBrowser.Providers/Music/LastFmImageProvider.cs index 5078326af..075f68b6a 100644 --- a/MediaBrowser.Providers/Music/LastFmImageProvider.cs +++ b/MediaBrowser.Providers/Music/LastFmImageProvider.cs @@ -4,7 +4,10 @@ using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; +using MediaBrowser.Model.Providers; using System; +using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -55,11 +58,6 @@ namespace MediaBrowser.Providers.Music return false; } - if (string.IsNullOrWhiteSpace(GetImageUrl(item))) - { - return false; - } - return base.NeedsRefreshInternal(item, providerInfo); } @@ -72,18 +70,34 @@ namespace MediaBrowser.Providers.Music /// Task{System.Boolean}. public override async Task FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken) { - var url = GetImageUrl(item); + var images = await _providerManager.GetAvailableRemoteImages(item, cancellationToken, ManualLastFmImageProvider.ProviderName).ConfigureAwait(false); - if (!string.IsNullOrWhiteSpace(url) && !item.HasImage(ImageType.Primary)) - { - await _providerManager.SaveImage(item, url, LastfmBaseProvider.LastfmResourcePool, ImageType.Primary, null, cancellationToken) - .ConfigureAwait(false); - } + await DownloadImages(item, images.ToList(), cancellationToken).ConfigureAwait(false); SetLastRefreshed(item, DateTime.UtcNow); + return true; } + private async Task DownloadImages(BaseItem item, List images, CancellationToken cancellationToken) + { + cancellationToken.ThrowIfCancellationRequested(); + + var configSetting = item is MusicAlbum + ? ConfigurationManager.Configuration.DownloadMusicAlbumImages + : ConfigurationManager.Configuration.DownloadMusicArtistImages; + + if (configSetting.Primary && !item.HasImage(ImageType.Primary)) + { + var image = images.FirstOrDefault(i => i.Type == ImageType.Primary); + + if (image != null) + { + await _providerManager.SaveImage(item, image.Url, LastfmBaseProvider.LastfmResourcePool, ImageType.Primary, null, cancellationToken).ConfigureAwait(false); + } + } + } + /// /// Gets the priority. /// @@ -92,36 +106,5 @@ namespace MediaBrowser.Providers.Music { get { return MetadataProviderPriority.Fifth; } } - - /// - /// Gets the image URL. - /// - /// The item. - /// System.String. - private string GetImageUrl(BaseItem item) - { - var musicArtist = item as MusicArtist; - - if (musicArtist != null) - { - return musicArtist.LastFmImageUrl; - } - - var artistByName = item as Artist; - - if (artistByName != null) - { - return artistByName.LastFmImageUrl; - } - - var album = item as MusicAlbum; - - if (album != null) - { - return album.LastFmImageUrl; - } - - return null; - } } } diff --git a/MediaBrowser.Providers/Music/LastfmAlbumProvider.cs b/MediaBrowser.Providers/Music/LastfmAlbumProvider.cs index 7192b5949..d0e807a86 100644 --- a/MediaBrowser.Providers/Music/LastfmAlbumProvider.cs +++ b/MediaBrowser.Providers/Music/LastfmAlbumProvider.cs @@ -46,7 +46,7 @@ namespace MediaBrowser.Providers.Music { get { - return "8"; + return "9"; } } diff --git a/MediaBrowser.Providers/Music/LastfmArtistProvider.cs b/MediaBrowser.Providers/Music/LastfmArtistProvider.cs index 7d83c3f6f..146992039 100644 --- a/MediaBrowser.Providers/Music/LastfmArtistProvider.cs +++ b/MediaBrowser.Providers/Music/LastfmArtistProvider.cs @@ -58,7 +58,7 @@ namespace MediaBrowser.Providers.Music { get { - return "8"; + return "9"; } } diff --git a/MediaBrowser.Providers/Music/LastfmHelper.cs b/MediaBrowser.Providers/Music/LastfmHelper.cs index ae849b418..f529fd44a 100644 --- a/MediaBrowser.Providers/Music/LastfmHelper.cs +++ b/MediaBrowser.Providers/Music/LastfmHelper.cs @@ -34,21 +34,27 @@ namespace MediaBrowser.Providers.Music var musicArtist = artist as MusicArtist; + string imageSize; + if (musicArtist != null) { - musicArtist.LastFmImageUrl = GetImageUrl(data); + musicArtist.LastFmImageUrl = GetImageUrl(data, out imageSize); + musicArtist.LastFmImageSize = imageSize; } var artistByName = artist as Artist; if (artistByName != null) { - artistByName.LastFmImageUrl = GetImageUrl(data); + artistByName.LastFmImageUrl = GetImageUrl(data, out imageSize); + artistByName.LastFmImageSize = imageSize; } } - private static string GetImageUrl(IHasLastFmImages data) + private static string GetImageUrl(IHasLastFmImages data, out string size) { + size = null; + if (data.image == null) { return null; @@ -61,12 +67,13 @@ namespace MediaBrowser.Providers.Music var img = validImages .FirstOrDefault(i => string.Equals(i.size, "mega", StringComparison.OrdinalIgnoreCase)) ?? data.image.FirstOrDefault(i => string.Equals(i.size, "extralarge", StringComparison.OrdinalIgnoreCase)) ?? - data.image.FirstOrDefault(i => string.Equals(i.size, "large", StringComparison.OrdinalIgnoreCase)) ?? - data.image.FirstOrDefault(i => string.Equals(i.size, "medium", StringComparison.OrdinalIgnoreCase)) ?? + data.image.FirstOrDefault(i => string.Equals(i.size, "large", StringComparison.OrdinalIgnoreCase)) ?? + data.image.FirstOrDefault(i => string.Equals(i.size, "medium", StringComparison.OrdinalIgnoreCase)) ?? data.image.FirstOrDefault(); if (img != null) { + size = img.size; return img.url; } @@ -81,7 +88,7 @@ namespace MediaBrowser.Providers.Music target.Overview = source.Overview; target.ProductionLocations = source.ProductionLocations.ToList(); } - + public static void ProcessAlbumData(BaseItem item, LastfmAlbum data) { if (!string.IsNullOrWhiteSpace(data.mbid)) item.SetProviderId(MetadataProviders.Musicbrainz, data.mbid); @@ -107,7 +114,11 @@ namespace MediaBrowser.Providers.Music } var album = (MusicAlbum)item; - album.LastFmImageUrl = GetImageUrl(data); + + string imageSize; + + album.LastFmImageUrl = GetImageUrl(data, out imageSize); + album.LastFmImageSize = imageSize; } private static void AddTags(BaseItem item, LastfmTags tags) diff --git a/MediaBrowser.Providers/Music/ManualLastFmImageProvider.cs b/MediaBrowser.Providers/Music/ManualLastFmImageProvider.cs index f4f987b45..0b1b95b1e 100644 --- a/MediaBrowser.Providers/Music/ManualLastFmImageProvider.cs +++ b/MediaBrowser.Providers/Music/ManualLastFmImageProvider.cs @@ -3,6 +3,7 @@ using MediaBrowser.Controller.Entities.Audio; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Providers; +using System; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -13,6 +14,11 @@ namespace MediaBrowser.Providers.Music public class ManualLastFmImageProvider : IImageProvider { public string Name + { + get { return ProviderName; } + } + + public static string ProviderName { get { return "last.fm"; } } @@ -29,12 +35,71 @@ namespace MediaBrowser.Providers.Music return images.Where(i => i.Type == imageType); } - public async Task> GetAllImages(BaseItem item, CancellationToken cancellationToken) + public Task> GetAllImages(BaseItem item, CancellationToken cancellationToken) { var list = new List(); + RemoteImageInfo info = null; + + var artist = item as Artist; + + if (artist != null) + { + info = GetInfo(artist.LastFmImageUrl, artist.LastFmImageSize); + } + + var album = item as MusicAlbum; + if (album != null) + { + info = GetInfo(album.LastFmImageUrl, album.LastFmImageSize); + } + + var musicArtist = item as MusicArtist; + if (musicArtist != null) + { + info = GetInfo(musicArtist.LastFmImageUrl, musicArtist.LastFmImageSize); + } + + if (info != null) + { + list.Add(info); + } + // The only info we have is size - return list.OrderByDescending(i => i.Width ?? 0); + return Task.FromResult>(list.OrderByDescending(i => i.Width ?? 0)); + } + + private RemoteImageInfo GetInfo(string url, string size) + { + if (string.IsNullOrEmpty(url)) + { + return null; + } + + var info = new RemoteImageInfo + { + ProviderName = Name, + Url = url + }; + + if (string.Equals(size, "mega", StringComparison.OrdinalIgnoreCase)) + { + + } + else if (string.Equals(size, "extralarge", StringComparison.OrdinalIgnoreCase)) + { + + } + else if (string.Equals(size, "large", StringComparison.OrdinalIgnoreCase)) + { + + } + else if (string.Equals(size, "medium", StringComparison.OrdinalIgnoreCase)) + { + + } + + return info; } public int Priority