using MediaBrowser.Common.IO; using MediaBrowser.Common.Net; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities.Audio; 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.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; using MediaBrowser.Model.Net; using System.Net; namespace MediaBrowser.Providers.Music { /// /// Class FanArtAlbumProvider /// public class FanArtAlbumProvider : BaseMetadataProvider { /// /// The _provider manager /// private readonly IProviderManager _providerManager; /// /// Gets the HTTP client. /// /// The HTTP client. protected IHttpClient HttpClient { get; private set; } private readonly IFileSystem _fileSystem; /// /// Initializes a new instance of the class. /// /// The HTTP client. /// The log manager. /// The configuration manager. /// The provider manager. public FanArtAlbumProvider(IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager, IFileSystem fileSystem) : base(logManager, configurationManager) { _providerManager = providerManager; _fileSystem = fileSystem; HttpClient = httpClient; } /// /// Gets the priority. /// /// The priority. public override MetadataProviderPriority Priority { get { return MetadataProviderPriority.Fifth; } } /// /// Supportses the specified item. /// /// The item. /// true if XXXX, false otherwise public override bool Supports(BaseItem item) { return item is MusicAlbum; } public override ItemUpdateType ItemUpdateType { get { return ItemUpdateType.ImageUpdate; } } /// /// Gets a value indicating whether [refresh on version change]. /// /// true if [refresh on version change]; otherwise, false. protected override bool RefreshOnVersionChange { get { return true; } } /// /// Gets the provider version. /// /// The provider version. protected override string ProviderVersion { get { return "18"; } } /// /// Needses the refresh internal. /// /// The item. /// The provider info. /// true if XXXX, false otherwise protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo) { if (!ConfigurationManager.Configuration.DownloadMusicAlbumImages.Disc && !ConfigurationManager.Configuration.DownloadMusicAlbumImages.Primary) { return false; } if (item.HasImage(ImageType.Primary) && item.HasImage(ImageType.Disc)) { return false; } return base.NeedsRefreshInternal(item, providerInfo); } protected override DateTime CompareDate(BaseItem item) { var artistMusicBrainzId = item.Parent.GetProviderId(MetadataProviders.Musicbrainz); if (!string.IsNullOrEmpty(artistMusicBrainzId)) { var artistXmlPath = FanartArtistProvider.GetArtistXmlPath(ConfigurationManager.CommonApplicationPaths, artistMusicBrainzId); var file = new FileInfo(artistXmlPath); if (file.Exists) { return _fileSystem.GetLastWriteTimeUtc(file); } } return base.CompareDate(item); } /// /// 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.LockedFields.Contains(MetadataFields.Images)) { var images = await _providerManager.GetAvailableRemoteImages(item, cancellationToken, ManualFanartAlbumProvider.ProviderName).ConfigureAwait(false); await FetchFromXml(item, images.ToList(), cancellationToken).ConfigureAwait(false); } SetLastRefreshed(item, DateTime.UtcNow, providerInfo); return true; } /// /// Fetches from XML. /// /// The item. /// The images. /// The cancellation token. /// Task. private async Task FetchFromXml(BaseItem item, List images, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (ConfigurationManager.Configuration.DownloadMusicAlbumImages.Primary && !item.HasImage(ImageType.Primary)) { await SaveImage(item, images, ImageType.Primary, cancellationToken).ConfigureAwait(false); } cancellationToken.ThrowIfCancellationRequested(); if (ConfigurationManager.Configuration.DownloadMusicAlbumImages.Disc && !item.HasImage(ImageType.Disc)) { await SaveImage(item, images, ImageType.Disc, cancellationToken).ConfigureAwait(false); } } private async Task SaveImage(BaseItem item, List images, ImageType type, CancellationToken cancellationToken) { foreach (var image in images.Where(i => i.Type == type)) { try { await _providerManager.SaveImage(item, image.Url, FanartArtistProvider.FanArtResourcePool, 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; } } } } }