using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Providers.Music
{
///
/// Class LastfmArtistByNameProvider
///
public class LastfmArtistByNameProvider : LastfmArtistProvider
{
///
/// Initializes a new instance of the class.
///
/// The json serializer.
/// The HTTP client.
/// The log manager.
/// The configuration manager.
/// The library manager.
public LastfmArtistByNameProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, ILibraryManager libraryManager)
: base(jsonSerializer, httpClient, logManager, configurationManager, libraryManager)
{
}
///
/// Gets a value indicating whether [save local meta].
///
/// true if [save local meta]; otherwise, false.
protected override bool SaveLocalMeta
{
get
{
return true;
}
}
///
/// Supportses the specified item.
///
/// The item.
/// true if XXXX, false otherwise
public override bool Supports(BaseItem item)
{
return item is Artist;
}
///
/// Gets the provider version.
///
/// The provider version.
protected override string ProviderVersion
{
get
{
return "7";
}
}
///
/// Fetches the lastfm data.
///
/// The item.
/// The music brainz id.
/// The cancellation token.
/// Task.
protected override async Task FetchLastfmData(BaseItem item, string musicBrainzId, bool force, CancellationToken cancellationToken)
{
var artist = (Artist)item;
// See if we can avoid an http request by finding the matching MusicArtist entity
var musicArtist = FindMusicArtist(artist, LibraryManager);
if (musicArtist != null && !force)
{
LastfmHelper.ProcessArtistData(musicArtist, artist);
}
else
{
await base.FetchLastfmData(item, musicBrainzId, force, cancellationToken).ConfigureAwait(false);
}
}
///
/// Finds the music artist.
///
/// The artist.
/// The library manager.
/// MusicArtist.
private static MusicArtist FindMusicArtist(Artist artist, ILibraryManager libraryManager)
{
var musicBrainzId = artist.GetProviderId(MetadataProviders.Musicbrainz);
return libraryManager.RootFolder.RecursiveChildren
.OfType()
.FirstOrDefault(i =>
{
if (!string.IsNullOrWhiteSpace(musicBrainzId) && string.Equals(musicBrainzId, i.GetProviderId(MetadataProviders.Musicbrainz), StringComparison.OrdinalIgnoreCase))
{
return true;
}
return false;
});
}
}
}