using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; 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; namespace MediaBrowser.Providers.Music { /// /// Class LastFmArtistImageProvider /// public class LastFmImageProvider : BaseMetadataProvider { /// /// The _provider manager /// private readonly IProviderManager _providerManager; /// /// Initializes a new instance of the class. /// /// The log manager. /// The configuration manager. /// The provider manager. public LastFmImageProvider(ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager) : base(logManager, configurationManager) { _providerManager = providerManager; } /// /// Supportses the specified item. /// /// The item. /// true if XXXX, false otherwise public override bool Supports(BaseItem item) { return item is MusicArtist || item is MusicAlbum; } /// /// Needses the refresh internal. /// /// The item. /// The provider info. /// true if XXXX, false otherwise protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo) { if (item.HasImage(ImageType.Primary)) { return false; } return base.NeedsRefreshInternal(item, providerInfo); } /// /// Fetches metadata and returns true or false indicating if any work that requires persistence was done /// /// The item. /// if set to true [force]. /// The cancellation token. /// Task{System.Boolean}. public override async Task FetchAsync(BaseItem item, bool force, BaseProviderInfo providerInfo, CancellationToken cancellationToken) { if (!item.HasImage(ImageType.Primary)) { var images = await _providerManager.GetAvailableRemoteImages(item, cancellationToken, ManualLastFmImageProvider.ProviderName).ConfigureAwait(false); await DownloadImages(item, images.ToList(), cancellationToken).ConfigureAwait(false); } SetLastRefreshed(item, DateTime.UtcNow, providerInfo); 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. /// /// The priority. public override MetadataProviderPriority Priority { get { return MetadataProviderPriority.Fifth; } } } }