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.Net;
using MediaBrowser.Model.Serialization;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
namespace MediaBrowser.Providers.Music
{
///
/// Class LastfmArtistProvider
///
public class LastfmArtistProvider : LastfmBaseProvider
{
///
/// The _library manager
///
protected readonly ILibraryManager LibraryManager;
///
/// Initializes a new instance of the class.
///
/// The json serializer.
/// The HTTP client.
/// The log manager.
/// The configuration manager.
/// The library manager.
public LastfmArtistProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, ILibraryManager libraryManager)
: base(jsonSerializer, httpClient, logManager, configurationManager)
{
LibraryManager = libraryManager;
}
protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo)
{
if (HasAltMeta(item))
{
return false;
}
return base.NeedsRefreshInternal(item, providerInfo);
}
protected override string ProviderVersion
{
get
{
return "6";
}
}
///
/// Gets the priority.
///
/// The priority.
public override MetadataProviderPriority Priority
{
get { return MetadataProviderPriority.Third; }
}
private bool HasAltMeta(BaseItem item)
{
return item.LocationType == LocationType.FileSystem && item.ResolveArgs.ContainsMetaFileByName("artist.xml");
}
///
/// Finds the id.
///
/// The item.
/// The cancellation token.
/// Task{System.String}.
protected override async Task FindId(BaseItem item, CancellationToken cancellationToken)
{
if (item is Artist)
{
// Since MusicArtists are refreshed first, try to find it from one of them
var id = FindIdFromMusicArtistEntity(item);
if (!string.IsNullOrEmpty(id))
{
return id;
}
}
try
{
// If we don't get anything, go directly to music brainz
return await FindIdFromMusicBrainz(item, cancellationToken).ConfigureAwait(false);
}
catch (HttpException e)
{
if (e.StatusCode.HasValue && e.StatusCode.Value == HttpStatusCode.BadRequest)
{
// They didn't like a character in the name. Handle the exception so that the provider doesn't keep retrying over and over
return null;
}
throw;
}
}
///
/// Finds the id from music artist entity.
///
/// The item.
/// System.String.
private string FindIdFromMusicArtistEntity(BaseItem item)
{
var artist = LibraryManager.RootFolder.RecursiveChildren.OfType()
.FirstOrDefault(i => string.Compare(i.Name, item.Name, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase | CompareOptions.IgnoreSymbols) == 0);
return artist != null ? artist.GetProviderId(MetadataProviders.Musicbrainz) : null;
}
///
/// Finds the id from music brainz.
///
/// The item.
/// The cancellation token.
/// Task{System.String}.
private async Task FindIdFromMusicBrainz(BaseItem item, CancellationToken cancellationToken)
{
// They seem to throw bad request failures on any term with a slash
var nameToSearch = item.Name.Replace('/', ' ');
var url = string.Format("http://www.musicbrainz.org/ws/2/artist/?query=artist:{0}", UrlEncode(nameToSearch));
var doc = await FanArtAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
var ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-2.0#");
var node = doc.SelectSingleNode("//mb:artist-list/mb:artist/@id", ns);
if (node != null && node.Value != null)
{
return node.Value;
}
if (HasDiacritics(item.Name))
{
// Try again using the search with accent characters url
url = string.Format("http://www.musicbrainz.org/ws/2/artist/?query=artistaccent:{0}", UrlEncode(nameToSearch));
doc = await FanArtAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-2.0#");
node = doc.SelectSingleNode("//mb:artist-list/mb:artist/@id", ns);
if (node != null && node.Value != null)
{
return node.Value;
}
}
return null;
}
///
/// Determines whether the specified text has diacritics.
///
/// The text.
/// true if the specified text has diacritics; otherwise, false.
private bool HasDiacritics(string text)
{
return !string.Equals(text, RemoveDiacritics(text), StringComparison.Ordinal);
}
///
/// Removes the diacritics.
///
/// The text.
/// System.String.
private string RemoveDiacritics(string text)
{
return string.Concat(
text.Normalize(NormalizationForm.FormD)
.Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) !=
UnicodeCategory.NonSpacingMark)
).Normalize(NormalizationForm.FormC);
}
///
/// Fetches the lastfm data.
///
/// The item.
/// The music brainz id.
/// The cancellation token.
/// Task.
protected override async Task FetchLastfmData(BaseItem item, string musicBrainzId, CancellationToken cancellationToken)
{
// Get artist info with provided id
var url = RootUrl + string.Format("method=artist.getInfo&mbid={0}&api_key={1}&format=json", UrlEncode(musicBrainzId), ApiKey);
LastfmGetArtistResult result;
using (var json = await HttpClient.Get(new HttpRequestOptions
{
Url = url,
ResourcePool = LastfmResourcePool,
CancellationToken = cancellationToken,
EnableHttpCompression = false
}).ConfigureAwait(false))
{
result = JsonSerializer.DeserializeFromStream(json);
}
if (result != null && result.artist != null)
{
LastfmHelper.ProcessArtistData(item, result.artist);
}
}
///
/// Supportses the specified item.
///
/// The item.
/// true if XXXX, false otherwise
public override bool Supports(BaseItem item)
{
return item is MusicArtist;
}
}
}