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 System;
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 Artist || 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;
}
if (string.IsNullOrWhiteSpace(GetImageUrl(item)))
{
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, CancellationToken cancellationToken)
{
var url = GetImageUrl(item);
if (!string.IsNullOrWhiteSpace(url))
{
await _providerManager.SaveImage(item, url, LastfmBaseProvider.LastfmResourcePool, ImageType.Primary, null, cancellationToken)
.ConfigureAwait(false);
}
SetLastRefreshed(item, DateTime.UtcNow);
return true;
}
///
/// Gets the priority.
///
/// The priority.
public override MetadataProviderPriority Priority
{
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;
}
}
}