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;
|
2016-10-27 07:58:33 +00:00
|
|
|
|
using MediaBrowser.Controller.Extensions;
|
2014-02-02 16:59:14 +00:00
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
{
|
2016-06-15 19:52:38 +00:00
|
|
|
|
var url = string.Format("/ws/2/artist/?query=arid:{0}", musicBrainzId);
|
2014-02-02 16:59:14 +00:00
|
|
|
|
|
2016-10-27 19:03:23 +00:00
|
|
|
|
using (var reader = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, false, cancellationToken)
|
|
|
|
|
.ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
return GetResultsFromResponse(reader);
|
|
|
|
|
}
|
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
|
|
|
|
|
2016-06-15 19:52:38 +00:00
|
|
|
|
var url = String.Format("/ws/2/artist/?query=artist:\"{0}\"", UrlEncode(nameToSearch));
|
2014-03-14 03:23:58 +00:00
|
|
|
|
|
2016-10-27 19:03:23 +00:00
|
|
|
|
using (var doc = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false))
|
2014-03-14 03:23:58 +00:00
|
|
|
|
{
|
2016-10-27 19:03:23 +00:00
|
|
|
|
var results = GetResultsFromResponse(doc).ToList();
|
|
|
|
|
|
|
|
|
|
if (results.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
return results;
|
|
|
|
|
}
|
2014-03-14 03:23:58 +00:00
|
|
|
|
}
|
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
|
2016-06-15 19:52:38 +00:00
|
|
|
|
url = String.Format("/ws/2/artist/?query=artistaccent:\"{0}\"", UrlEncode(nameToSearch));
|
2014-03-14 03:23:58 +00:00
|
|
|
|
|
2016-10-27 19:03:23 +00:00
|
|
|
|
using (var doc = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
return GetResultsFromResponse(doc);
|
|
|
|
|
}
|
2014-03-14 03:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
|
2016-10-27 19:03:23 +00:00
|
|
|
|
private IEnumerable<RemoteSearchResult> GetResultsFromResponse(XmlReader reader)
|
2014-03-14 03:23:58 +00:00
|
|
|
|
{
|
2016-10-27 19:03:23 +00:00
|
|
|
|
reader.MoveToContent();
|
2016-05-09 03:13:38 +00:00
|
|
|
|
|
2016-10-27 19:03:23 +00:00
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
2016-05-09 03:13:38 +00:00
|
|
|
|
{
|
2016-10-27 19:03:23 +00:00
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "artist-list":
|
|
|
|
|
{
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
|
{
|
|
|
|
|
return ParseArtistList(subReader);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-09 03:13:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 19:03:23 +00:00
|
|
|
|
return new List<RemoteSearchResult>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<RemoteSearchResult> ParseArtistList(XmlReader reader)
|
|
|
|
|
{
|
|
|
|
|
var list = new List<RemoteSearchResult>();
|
2016-05-09 03:13:38 +00:00
|
|
|
|
|
2016-10-27 19:03:23 +00:00
|
|
|
|
reader.MoveToContent();
|
2014-03-14 03:23:58 +00:00
|
|
|
|
|
2016-10-27 19:03:23 +00:00
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
2014-02-02 16:59:14 +00:00
|
|
|
|
{
|
2016-10-27 19:03:23 +00:00
|
|
|
|
switch (reader.Name)
|
2014-03-14 03:23:58 +00:00
|
|
|
|
{
|
2016-10-27 19:03:23 +00:00
|
|
|
|
case "artist":
|
2014-03-14 03:23:58 +00:00
|
|
|
|
{
|
2016-10-27 19:03:23 +00:00
|
|
|
|
var mbzId = reader.GetAttribute("id");
|
|
|
|
|
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
2016-10-08 18:51:07 +00:00
|
|
|
|
{
|
2016-10-27 19:03:23 +00:00
|
|
|
|
var artist = ParseArtist(subReader, mbzId);
|
|
|
|
|
list.Add(artist);
|
2016-05-09 03:13:38 +00:00
|
|
|
|
}
|
2016-10-27 19:03:23 +00:00
|
|
|
|
break;
|
2014-03-14 03:23:58 +00:00
|
|
|
|
}
|
2016-10-27 19:03:23 +00:00
|
|
|
|
default:
|
2014-03-14 03:23:58 +00:00
|
|
|
|
{
|
2016-10-27 19:03:23 +00:00
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RemoteSearchResult ParseArtist(XmlReader reader, string artistId)
|
|
|
|
|
{
|
|
|
|
|
var result = new RemoteSearchResult();
|
2014-03-14 03:23:58 +00:00
|
|
|
|
|
2016-10-27 19:03:23 +00:00
|
|
|
|
reader.MoveToContent();
|
2014-03-14 03:23:58 +00:00
|
|
|
|
|
2016-10-27 19:03:23 +00:00
|
|
|
|
// Loop through each element
|
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
switch (reader.Name)
|
|
|
|
|
{
|
|
|
|
|
case "name":
|
|
|
|
|
{
|
|
|
|
|
result.Name = reader.ReadElementContentAsString();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
//case "sort-name":
|
|
|
|
|
// {
|
|
|
|
|
// break;
|
|
|
|
|
// }
|
|
|
|
|
//case "tag-list":
|
|
|
|
|
// {
|
|
|
|
|
// break;
|
|
|
|
|
// }
|
|
|
|
|
case "annotation":
|
|
|
|
|
{
|
|
|
|
|
result.Overview = reader.ReadElementContentAsString();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
reader.Skip();
|
|
|
|
|
break;
|
2014-03-14 03:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-02 16:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-27 19:03:23 +00:00
|
|
|
|
result.SetProviderId(MetadataProviders.MusicBrainzArtist, artistId);
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(artistId) || string.IsNullOrWhiteSpace(result.Name))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2014-03-14 03:23:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2015-04-26 03:25:07 +00:00
|
|
|
|
//result.Item.Name = singleResult.Name;
|
2016-10-08 18:51:07 +00:00
|
|
|
|
result.Item.Overview = singleResult.Overview;
|
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)
|
|
|
|
|
{
|
2016-10-27 07:58:33 +00:00
|
|
|
|
return !String.Equals(text, text.RemoveDiacritics(), StringComparison.Ordinal);
|
2014-02-02 16:59:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
}
|
|
|
|
|
}
|