2014-02-20 04:53:15 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2014-02-02 16:59:14 +00:00
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-02-20 04:53:15 +00:00
|
|
|
|
using MediaBrowser.Model.Providers;
|
2014-02-02 16:59:14 +00:00
|
|
|
|
using System;
|
2014-02-20 04:53:15 +00:00
|
|
|
|
using System.Collections.Generic;
|
2014-02-02 16:59:14 +00:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Music
|
|
|
|
|
{
|
2014-02-07 03:10:13 +00:00
|
|
|
|
public class MusicBrainzArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>
|
2014-02-02 16:59:14 +00:00
|
|
|
|
{
|
2014-02-20 04:53:15 +00:00
|
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2014-03-14 03:23:58 +00:00
|
|
|
|
var musicBrainzId = searchInfo.GetMusicBrainzArtistId();
|
2014-02-02 16:59:14 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(musicBrainzId))
|
|
|
|
|
{
|
2014-03-14 03:23:58 +00:00
|
|
|
|
var url = string.Format("http://www.musicbrainz.org/ws/2/artist/?query=arid:{0}", musicBrainzId);
|
2014-02-02 16:59:14 +00:00
|
|
|
|
|
2014-09-28 16:50:33 +00:00
|
|
|
|
var doc = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, false, cancellationToken)
|
2014-03-14 03:23:58 +00:00
|
|
|
|
.ConfigureAwait(false);
|
2014-02-02 16:59:14 +00:00
|
|
|
|
|
2014-03-14 03:23:58 +00:00
|
|
|
|
return GetResultsFromResponse(doc);
|
2014-02-02 16:59:14 +00:00
|
|
|
|
}
|
2014-03-14 03:23:58 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// They seem to throw bad request failures on any term with a slash
|
|
|
|
|
var nameToSearch = searchInfo.Name.Replace('/', ' ');
|
2014-02-02 16:59:14 +00:00
|
|
|
|
|
2014-03-14 03:23:58 +00:00
|
|
|
|
var url = String.Format("http://www.musicbrainz.org/ws/2/artist/?query=artist:\"{0}\"", UrlEncode(nameToSearch));
|
|
|
|
|
|
2014-09-28 16:50:33 +00:00
|
|
|
|
var doc = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false);
|
2014-03-14 03:23:58 +00:00
|
|
|
|
|
|
|
|
|
var results = GetResultsFromResponse(doc).ToList();
|
|
|
|
|
|
|
|
|
|
if (results.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
return results;
|
|
|
|
|
}
|
2014-02-02 16:59:14 +00:00
|
|
|
|
|
2014-03-14 03:23:58 +00:00
|
|
|
|
if (HasDiacritics(searchInfo.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));
|
|
|
|
|
|
2014-09-28 16:50:33 +00:00
|
|
|
|
doc = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false);
|
2014-03-14 03:23:58 +00:00
|
|
|
|
|
|
|
|
|
return GetResultsFromResponse(doc);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-02 16:59:14 +00:00
|
|
|
|
|
2014-03-14 03:23:58 +00:00
|
|
|
|
return new List<RemoteSearchResult>();
|
|
|
|
|
}
|
2014-02-02 16:59:14 +00:00
|
|
|
|
|
2014-03-14 03:23:58 +00:00
|
|
|
|
private IEnumerable<RemoteSearchResult> GetResultsFromResponse(XmlDocument doc)
|
|
|
|
|
{
|
2014-02-02 16:59:14 +00:00
|
|
|
|
var ns = new XmlNamespaceManager(doc.NameTable);
|
|
|
|
|
ns.AddNamespace("mb", "http://musicbrainz.org/ns/mmd-2.0#");
|
|
|
|
|
|
2014-03-14 03:23:58 +00:00
|
|
|
|
var list = new List<RemoteSearchResult>();
|
|
|
|
|
|
|
|
|
|
var nodes = doc.SelectNodes("//mb:artist-list/mb:artist", ns);
|
|
|
|
|
|
|
|
|
|
if (nodes != null)
|
2014-02-02 16:59:14 +00:00
|
|
|
|
{
|
2014-03-14 03:23:58 +00:00
|
|
|
|
foreach (var node in nodes.Cast<XmlNode>())
|
|
|
|
|
{
|
|
|
|
|
if (node.Attributes != null)
|
|
|
|
|
{
|
|
|
|
|
string name = null;
|
|
|
|
|
|
|
|
|
|
string mbzId = node.Attributes["id"].Value;
|
|
|
|
|
|
|
|
|
|
var nameNode = node.SelectSingleNode("//mb:name", ns);
|
|
|
|
|
|
|
|
|
|
if (nameNode != null)
|
|
|
|
|
{
|
|
|
|
|
name = nameNode.InnerText;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(mbzId) && !string.IsNullOrWhiteSpace(name))
|
|
|
|
|
{
|
|
|
|
|
var result = new RemoteSearchResult
|
|
|
|
|
{
|
|
|
|
|
Name = name
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
result.SetProviderId(MetadataProviders.MusicBrainzArtist, mbzId);
|
|
|
|
|
|
|
|
|
|
list.Add(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-02 16:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-14 03:23:58 +00:00
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
var result = new MetadataResult<MusicArtist>
|
2014-02-02 16:59:14 +00:00
|
|
|
|
{
|
2014-03-14 03:23:58 +00:00
|
|
|
|
Item = new MusicArtist()
|
|
|
|
|
};
|
2014-02-02 16:59:14 +00:00
|
|
|
|
|
2014-03-14 03:23:58 +00:00
|
|
|
|
var musicBrainzId = id.GetMusicBrainzArtistId();
|
2014-02-02 16:59:14 +00:00
|
|
|
|
|
2014-03-14 03:23:58 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(musicBrainzId))
|
|
|
|
|
{
|
|
|
|
|
var searchResults = await GetSearchResults(id, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var singleResult = searchResults.FirstOrDefault();
|
2014-02-02 16:59:14 +00:00
|
|
|
|
|
2014-03-14 03:23:58 +00:00
|
|
|
|
if (singleResult != null)
|
2014-02-02 16:59:14 +00:00
|
|
|
|
{
|
2014-03-14 03:23:58 +00:00
|
|
|
|
musicBrainzId = singleResult.GetProviderId(MetadataProviders.MusicBrainzArtist);
|
|
|
|
|
result.Item.Name = singleResult.Name;
|
2014-02-02 16:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-14 03:23:58 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(musicBrainzId))
|
|
|
|
|
{
|
|
|
|
|
result.HasMetadata = true;
|
|
|
|
|
result.Item.SetProviderId(MetadataProviders.MusicBrainzArtist, musicBrainzId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2014-02-02 16:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Determines whether the specified text has diacritics.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The text.</param>
|
|
|
|
|
/// <returns><c>true</c> if the specified text has diacritics; otherwise, <c>false</c>.</returns>
|
|
|
|
|
private bool HasDiacritics(string text)
|
|
|
|
|
{
|
|
|
|
|
return !String.Equals(text, RemoveDiacritics(text), StringComparison.Ordinal);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes the diacritics.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">The text.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
private string RemoveDiacritics(string text)
|
|
|
|
|
{
|
|
|
|
|
return String.Concat(
|
|
|
|
|
text.Normalize(NormalizationForm.FormD)
|
|
|
|
|
.Where(ch => CharUnicodeInfo.GetUnicodeCategory(ch) !=
|
|
|
|
|
UnicodeCategory.NonSpacingMark)
|
|
|
|
|
).Normalize(NormalizationForm.FormC);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Encodes an URL.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
private string UrlEncode(string name)
|
|
|
|
|
{
|
|
|
|
|
return WebUtility.UrlEncode(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name
|
|
|
|
|
{
|
|
|
|
|
get { return "MusicBrainz"; }
|
|
|
|
|
}
|
2014-02-20 04:53:15 +00:00
|
|
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2014-02-02 16:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
}
|