2013-05-06 17:28:29 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-03-04 06:06:38 +00:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2013-03-03 20:55:30 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-03-04 01:46:06 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2013-05-06 18:34:42 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-03-03 20:55:30 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-05-06 01:21:07 +00:00
|
|
|
|
using MediaBrowser.Model.Net;
|
2013-03-03 20:55:30 +00:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2013-04-10 15:56:36 +00:00
|
|
|
|
using System.IO;
|
2013-05-06 01:21:07 +00:00
|
|
|
|
using System.Linq;
|
2013-04-10 15:56:36 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2013-05-05 05:46:17 +00:00
|
|
|
|
using System.Xml;
|
2013-05-06 18:34:42 +00:00
|
|
|
|
using Mediabrowser.Model.Entities;
|
2013-03-03 20:55:30 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Providers.Music
|
|
|
|
|
{
|
2013-03-04 20:03:36 +00:00
|
|
|
|
public class LastfmArtistProvider : LastfmBaseProvider
|
2013-03-03 20:55:30 +00:00
|
|
|
|
{
|
2013-03-08 05:08:27 +00:00
|
|
|
|
private readonly IProviderManager _providerManager;
|
2013-05-06 18:34:42 +00:00
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
|
2013-05-06 18:35:24 +00:00
|
|
|
|
public LastfmArtistProvider(IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager, ILibraryManager libraryManager)
|
2013-03-04 06:06:38 +00:00
|
|
|
|
: base(jsonSerializer, httpClient, logManager, configurationManager)
|
2013-03-03 20:55:30 +00:00
|
|
|
|
{
|
2013-03-08 05:08:27 +00:00
|
|
|
|
_providerManager = providerManager;
|
2013-05-06 18:35:24 +00:00
|
|
|
|
_libraryManager = libraryManager;
|
2013-03-04 20:03:36 +00:00
|
|
|
|
LocalMetaFileName = LastfmHelper.LocalArtistMetaFileName;
|
2013-03-03 20:55:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-06 18:34:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Finds the id.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task{System.String}.</returns>
|
2013-05-05 05:46:17 +00:00
|
|
|
|
protected override async Task<string> FindId(BaseItem item, CancellationToken cancellationToken)
|
2013-05-06 01:21:07 +00:00
|
|
|
|
{
|
2013-05-06 18:34:42 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-06 01:21:07 +00:00
|
|
|
|
// Try to find the id using last fm
|
2013-05-06 17:28:29 +00:00
|
|
|
|
var result = await FindIdFromLastFm(item, cancellationToken).ConfigureAwait(false);
|
2013-05-06 01:21:07 +00:00
|
|
|
|
|
2013-05-07 13:06:01 +00:00
|
|
|
|
if (result != null)
|
2013-05-06 01:21:07 +00:00
|
|
|
|
{
|
2013-05-08 20:58:52 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(result))
|
2013-05-07 13:06:01 +00:00
|
|
|
|
{
|
2013-05-08 20:58:52 +00:00
|
|
|
|
return result;
|
2013-05-07 13:06:01 +00:00
|
|
|
|
}
|
2013-05-06 17:28:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2013-05-06 01:21:07 +00:00
|
|
|
|
|
2013-05-06 17:28:29 +00:00
|
|
|
|
throw;
|
|
|
|
|
}
|
2013-05-06 01:21:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-06 18:34:42 +00:00
|
|
|
|
private string FindIdFromMusicArtistEntity(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
var artist = _libraryManager.RootFolder.RecursiveChildren.OfType<MusicArtist>()
|
|
|
|
|
.FirstOrDefault(i => string.Equals(i.Name, item.Name, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
return artist != null ? artist.GetProviderId(MetadataProviders.Musicbrainz) : null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-08 20:58:52 +00:00
|
|
|
|
private async Task<string> FindIdFromLastFm(BaseItem item, CancellationToken cancellationToken)
|
2013-05-06 01:21:07 +00:00
|
|
|
|
{
|
|
|
|
|
//Execute the Artist search against our name and assume first one is the one we want
|
|
|
|
|
var url = RootUrl + string.Format("method=artist.search&artist={0}&api_key={1}&format=json", UrlEncode(item.Name), ApiKey);
|
|
|
|
|
|
|
|
|
|
LastfmArtistSearchResults searchResult;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-05-06 19:31:57 +00:00
|
|
|
|
using (var json = await HttpClient.Get(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = LastfmResourcePool,
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
EnableResponseCache = true
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
2013-05-06 01:21:07 +00:00
|
|
|
|
{
|
|
|
|
|
searchResult = JsonSerializer.DeserializeFromStream<LastfmArtistSearchResults>(json);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (HttpException e)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-06 17:28:29 +00:00
|
|
|
|
if (searchResult != null && searchResult.results != null && searchResult.results.artistmatches != null && searchResult.results.artistmatches.artist.Count > 0)
|
2013-05-06 01:21:07 +00:00
|
|
|
|
{
|
2013-05-06 17:28:29 +00:00
|
|
|
|
var artist = searchResult.results.artistmatches.artist.FirstOrDefault(i => i.name != null && string.Compare(i.name, item.Name, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace) == 0) ??
|
|
|
|
|
searchResult.results.artistmatches.artist.First();
|
|
|
|
|
|
2013-05-08 20:58:52 +00:00
|
|
|
|
return artist.mbid;
|
2013-05-06 01:21:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-08 20:58:52 +00:00
|
|
|
|
return null;
|
2013-05-06 01:21:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<string> FindIdFromMusicBrainz(BaseItem item, CancellationToken cancellationToken)
|
2013-05-05 05:46:17 +00:00
|
|
|
|
{
|
2013-05-06 17:28:29 +00:00
|
|
|
|
// 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));
|
2013-03-04 01:46:06 +00:00
|
|
|
|
|
2013-05-05 05:46:17 +00:00
|
|
|
|
var doc = await FanArtAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
|
2013-05-02 15:18:28 +00:00
|
|
|
|
|
2013-05-05 05:46:17 +00:00
|
|
|
|
var ns = new XmlNamespaceManager(doc.NameTable);
|
|
|
|
|
ns.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-2.0#");
|
2013-05-05 13:27:20 +00:00
|
|
|
|
var node = doc.SelectSingleNode("//mb:artist-list/mb:artist[@type='Group']/@id", ns);
|
|
|
|
|
|
|
|
|
|
if (node != null && node.Value != null)
|
|
|
|
|
{
|
|
|
|
|
return node.Value;
|
2013-05-06 01:21:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-05-06 17:28:29 +00:00
|
|
|
|
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));
|
2013-05-05 13:27:20 +00:00
|
|
|
|
|
2013-05-06 17:28:29 +00:00
|
|
|
|
doc = await FanArtAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
|
2013-05-05 13:27:20 +00:00
|
|
|
|
|
2013-05-06 17:28:29 +00:00
|
|
|
|
ns = new XmlNamespaceManager(doc.NameTable);
|
|
|
|
|
ns.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-2.0#");
|
|
|
|
|
node = doc.SelectSingleNode("//mb:artist-list/mb:artist[@type='Group']/@id", ns);
|
2013-05-05 13:27:20 +00:00
|
|
|
|
|
2013-05-06 17:28:29 +00:00
|
|
|
|
if (node != null && node.Value != null)
|
|
|
|
|
{
|
|
|
|
|
return node.Value;
|
|
|
|
|
}
|
2013-05-05 13:27:20 +00:00
|
|
|
|
}
|
2013-03-04 01:46:06 +00:00
|
|
|
|
|
2013-05-05 13:27:20 +00:00
|
|
|
|
return null;
|
2013-03-04 01:46:06 +00:00
|
|
|
|
}
|
2013-05-06 17:28:29 +00:00
|
|
|
|
|
|
|
|
|
private bool HasDiacritics(string text)
|
|
|
|
|
{
|
|
|
|
|
return !string.Equals(text, RemoveDiacritics(text), StringComparison.Ordinal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string RemoveDiacritics(string text)
|
|
|
|
|
{
|
|
|
|
|
return string.Concat(
|
|
|
|
|
text.Normalize(NormalizationForm.FormD)
|
|
|
|
|
.Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) !=
|
|
|
|
|
UnicodeCategory.NonSpacingMark)
|
|
|
|
|
).Normalize(NormalizationForm.FormC);
|
|
|
|
|
}
|
2013-05-06 01:21:07 +00:00
|
|
|
|
|
2013-03-04 01:46:06 +00:00
|
|
|
|
protected override async Task FetchLastfmData(BaseItem item, string id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
// Get artist info with provided id
|
|
|
|
|
var url = RootUrl + string.Format("method=artist.getInfo&mbid={0}&api_key={1}&format=json", UrlEncode(id), ApiKey);
|
|
|
|
|
|
2013-04-22 04:38:03 +00:00
|
|
|
|
LastfmGetArtistResult result;
|
2013-03-04 01:46:06 +00:00
|
|
|
|
|
2013-05-06 19:31:57 +00:00
|
|
|
|
using (var json = await HttpClient.Get(new HttpRequestOptions
|
|
|
|
|
{
|
|
|
|
|
Url = url,
|
|
|
|
|
ResourcePool = LastfmResourcePool,
|
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
|
EnableResponseCache = true
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false))
|
2013-03-04 01:46:06 +00:00
|
|
|
|
{
|
2013-04-22 04:38:03 +00:00
|
|
|
|
result = JsonSerializer.DeserializeFromStream<LastfmGetArtistResult>(json);
|
2013-03-04 01:46:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result != null && result.artist != null)
|
|
|
|
|
{
|
2013-03-04 20:03:36 +00:00
|
|
|
|
LastfmHelper.ProcessArtistData(item, result.artist);
|
|
|
|
|
//And save locally if indicated
|
2013-04-22 04:38:03 +00:00
|
|
|
|
if (SaveLocalMeta)
|
2013-03-04 20:03:36 +00:00
|
|
|
|
{
|
|
|
|
|
var ms = new MemoryStream();
|
|
|
|
|
JsonSerializer.SerializeToStream(result.artist, ms);
|
|
|
|
|
|
2013-04-25 17:27:36 +00:00
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
ms.Dispose();
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
}
|
2013-03-04 20:03:36 +00:00
|
|
|
|
|
2013-03-08 05:08:27 +00:00
|
|
|
|
await _providerManager.SaveToLibraryFilesystem(item, Path.Combine(item.MetaLocation, LocalMetaFileName), ms, cancellationToken).ConfigureAwait(false);
|
2013-03-04 20:03:36 +00:00
|
|
|
|
|
|
|
|
|
}
|
2013-03-04 01:46:06 +00:00
|
|
|
|
}
|
2013-03-03 20:55:30 +00:00
|
|
|
|
}
|
2013-03-04 20:03:36 +00:00
|
|
|
|
|
2013-04-10 15:56:36 +00:00
|
|
|
|
public override bool Supports(BaseItem item)
|
2013-03-04 20:03:36 +00:00
|
|
|
|
{
|
2013-05-02 14:30:38 +00:00
|
|
|
|
return item is MusicArtist;
|
2013-03-04 20:03:36 +00:00
|
|
|
|
}
|
2013-03-03 20:55:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|