From d433ea5bf926d54f8697b6f83decf5dae92d8ec2 Mon Sep 17 00:00:00 2001 From: Eric Reed Date: Tue, 5 Mar 2013 10:45:59 -0500 Subject: [PATCH] Add FanArtAlbumProvider --- .../MediaBrowser.Controller.csproj | 1 + .../Providers/FanartBaseProvider.cs | 8 ++ .../Providers/Music/FanArtAlbumProvider.cs | 74 +++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 MediaBrowser.Controller/Providers/Music/FanArtAlbumProvider.cs diff --git a/MediaBrowser.Controller/MediaBrowser.Controller.csproj b/MediaBrowser.Controller/MediaBrowser.Controller.csproj index bdf464bbf..c7a26b77e 100644 --- a/MediaBrowser.Controller/MediaBrowser.Controller.csproj +++ b/MediaBrowser.Controller/MediaBrowser.Controller.csproj @@ -108,6 +108,7 @@ + diff --git a/MediaBrowser.Controller/Providers/FanartBaseProvider.cs b/MediaBrowser.Controller/Providers/FanartBaseProvider.cs index 8ea7532f3..fdac03c9a 100644 --- a/MediaBrowser.Controller/Providers/FanartBaseProvider.cs +++ b/MediaBrowser.Controller/Providers/FanartBaseProvider.cs @@ -7,6 +7,14 @@ using MediaBrowser.Model.Logging; namespace MediaBrowser.Controller.Providers { + class FanArtProviderException : ApplicationException + { + public FanArtProviderException(string msg) + : base(msg) + { + } + + } /// /// Class FanartBaseProvider /// diff --git a/MediaBrowser.Controller/Providers/Music/FanArtAlbumProvider.cs b/MediaBrowser.Controller/Providers/Music/FanArtAlbumProvider.cs new file mode 100644 index 000000000..2d7284c80 --- /dev/null +++ b/MediaBrowser.Controller/Providers/Music/FanArtAlbumProvider.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Entities.Audio; +using MediaBrowser.Model.Entities; +using MediaBrowser.Model.Logging; +using MediaBrowser.Common.Extensions; + +namespace MediaBrowser.Controller.Providers.Music +{ + public class FanArtAlbumProvider : FanartBaseProvider + { + public FanArtAlbumProvider(ILogManager logManager, IServerConfigurationManager configurationManager) : base(logManager, configurationManager) + { + } + + public override bool Supports(BaseItem item) + { + return item is MusicAlbum && item.Parent is MusicArtist; + } + + /// + /// Needses the refresh internal. + /// + /// The item. + /// The provider info. + /// true if we need refreshing, false otherwise + protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo) + { + //we fetch if image needed and haven't already tried recently + return string.IsNullOrEmpty(item.PrimaryImagePath) && + DateTime.Today.Subtract(providerInfo.LastRefreshed).TotalDays > ConfigurationManager.Configuration.MetadataRefreshDays; + } + + protected override async Task FetchAsyncInternal(BaseItem item, bool force, CancellationToken cancellationToken) + { + var mbid = item.GetProviderId(MetadataProviders.Musicbrainz); + if (mbid == null) + { + Logger.Warn("No Musicbrainz id associated with album {0}", item.Name); + SetLastRefreshed(item, DateTime.UtcNow, ProviderRefreshStatus.CompletedWithErrors); + return false; + } + + cancellationToken.ThrowIfCancellationRequested(); + + //Look at our parent for our album cover + var artist = (MusicArtist)item.Parent; + var cover = artist.AlbumCovers != null ? artist.AlbumCovers.GetValueOrDefault(mbid, null) : null; + if (cover == null) + { + // Not there - maybe it is new since artist last refreshed so refresh it and try again + await artist.RefreshMetadata(cancellationToken).ConfigureAwait(false); + cancellationToken.ThrowIfCancellationRequested(); + + cover = artist.AlbumCovers != null ? artist.AlbumCovers.GetValueOrDefault(mbid, null) : null; + } + if (cover == null) + { + Logger.Warn("Unable to find cover art for {0}", item.Name); + SetLastRefreshed(item, DateTime.UtcNow, ProviderRefreshStatus.CompletedWithErrors); + return false; + } + + item.SetImage(ImageType.Primary, await Kernel.Instance.ProviderManager.DownloadAndSaveImage(item, cover, "folder.jpg", FanArtResourcePool, cancellationToken).ConfigureAwait(false)); + return true; + } + } +}