2019-01-13 13:03:10 -07:00
|
|
|
using System;
|
2014-02-19 21:53:15 -07:00
|
|
|
using System.Collections.Generic;
|
2016-10-27 14:05:25 -07:00
|
|
|
using System.IO;
|
2014-02-02 09:59:14 -07:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Xml;
|
2019-01-13 12:26:31 -07:00
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
2016-10-27 00:58:33 -07:00
|
|
|
using MediaBrowser.Controller.Extensions;
|
2019-01-13 12:26:31 -07:00
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Providers;
|
2014-02-02 09:59:14 -07:00
|
|
|
|
|
|
|
namespace MediaBrowser.Providers.Music
|
|
|
|
{
|
2014-02-06 20:10:13 -07:00
|
|
|
public class MusicBrainzArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>
|
2014-02-02 09:59:14 -07:00
|
|
|
{
|
2019-02-01 09:43:31 -07:00
|
|
|
public MusicBrainzArtistProvider()
|
2016-10-27 14:05:25 -07:00
|
|
|
{
|
2019-02-01 09:43:31 -07:00
|
|
|
|
2016-10-27 14:05:25 -07:00
|
|
|
}
|
|
|
|
|
2014-02-19 21:53:15 -07:00
|
|
|
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
|
|
|
|
{
|
2014-03-13 20:23:58 -07:00
|
|
|
var musicBrainzId = searchInfo.GetMusicBrainzArtistId();
|
2014-02-02 09:59:14 -07:00
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(musicBrainzId))
|
|
|
|
{
|
2016-06-15 12:52:38 -07:00
|
|
|
var url = string.Format("/ws/2/artist/?query=arid:{0}", musicBrainzId);
|
2014-02-02 09:59:14 -07:00
|
|
|
|
2019-03-13 13:31:21 -07:00
|
|
|
using (var response = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false))
|
2016-10-27 12:03:23 -07:00
|
|
|
{
|
2017-10-20 09:16:56 -07:00
|
|
|
using (var stream = response.Content)
|
|
|
|
{
|
|
|
|
return GetResultsFromResponse(stream);
|
|
|
|
}
|
2016-10-27 12:03:23 -07:00
|
|
|
}
|
2014-02-02 09:59:14 -07:00
|
|
|
}
|
2014-03-13 20:23:58 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// They seem to throw bad request failures on any term with a slash
|
|
|
|
var nameToSearch = searchInfo.Name.Replace('/', ' ');
|
2014-02-02 09:59:14 -07:00
|
|
|
|
2019-01-06 13:50:43 -07:00
|
|
|
var url = string.Format("/ws/2/artist/?query=\"{0}\"&dismax=true", UrlEncode(nameToSearch));
|
2014-03-13 20:23:58 -07:00
|
|
|
|
2019-03-13 13:31:21 -07:00
|
|
|
using (var response = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false))
|
2014-03-13 20:23:58 -07:00
|
|
|
{
|
2017-10-20 09:16:56 -07:00
|
|
|
using (var stream = response.Content)
|
2016-10-27 12:03:23 -07:00
|
|
|
{
|
2019-03-13 14:32:52 -07:00
|
|
|
var results = GetResultsFromResponse(stream).ToList();
|
2017-10-20 09:16:56 -07:00
|
|
|
|
|
|
|
if (results.Count > 0)
|
|
|
|
{
|
|
|
|
return results;
|
|
|
|
}
|
2016-10-27 12:03:23 -07:00
|
|
|
}
|
2014-03-13 20:23:58 -07:00
|
|
|
}
|
2014-02-02 09:59:14 -07:00
|
|
|
|
2014-03-13 20:23:58 -07:00
|
|
|
if (HasDiacritics(searchInfo.Name))
|
|
|
|
{
|
|
|
|
// Try again using the search with accent characters url
|
2019-01-06 13:50:43 -07:00
|
|
|
url = string.Format("/ws/2/artist/?query=artistaccent:\"{0}\"", UrlEncode(nameToSearch));
|
2014-03-13 20:23:58 -07:00
|
|
|
|
2019-03-13 13:31:21 -07:00
|
|
|
using (var response = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false))
|
2016-10-27 12:03:23 -07:00
|
|
|
{
|
2017-10-20 09:16:56 -07:00
|
|
|
using (var stream = response.Content)
|
|
|
|
{
|
|
|
|
return GetResultsFromResponse(stream);
|
|
|
|
}
|
2016-10-27 12:03:23 -07:00
|
|
|
}
|
2014-03-13 20:23:58 -07:00
|
|
|
}
|
|
|
|
}
|
2014-02-02 09:59:14 -07:00
|
|
|
|
2019-03-13 14:32:52 -07:00
|
|
|
return Enumerable.Empty<RemoteSearchResult>();
|
2014-03-13 20:23:58 -07:00
|
|
|
}
|
2014-02-02 09:59:14 -07:00
|
|
|
|
2019-03-13 14:32:52 -07:00
|
|
|
private IEnumerable<RemoteSearchResult> GetResultsFromResponse(Stream stream)
|
2014-03-13 20:23:58 -07:00
|
|
|
{
|
2016-10-27 14:05:25 -07:00
|
|
|
using (var oReader = new StreamReader(stream, Encoding.UTF8))
|
2016-05-08 20:13:38 -07:00
|
|
|
{
|
2019-02-01 09:43:31 -07:00
|
|
|
var settings = new XmlReaderSettings()
|
|
|
|
{
|
|
|
|
ValidationType = ValidationType.None,
|
|
|
|
CheckCharacters = false,
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
IgnoreComments = true
|
|
|
|
};
|
2016-10-27 14:05:25 -07:00
|
|
|
|
|
|
|
using (var reader = XmlReader.Create(oReader, settings))
|
2016-10-27 12:03:23 -07:00
|
|
|
{
|
2016-10-27 14:05:25 -07:00
|
|
|
reader.MoveToContent();
|
2016-11-02 10:08:20 -07:00
|
|
|
reader.Read();
|
2016-10-27 14:05:25 -07:00
|
|
|
|
|
|
|
// Loop through each element
|
2016-12-03 14:46:06 -07:00
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
2016-10-27 14:05:25 -07:00
|
|
|
{
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2016-10-27 12:03:23 -07:00
|
|
|
{
|
2016-10-27 14:05:25 -07:00
|
|
|
switch (reader.Name)
|
2016-10-27 12:03:23 -07:00
|
|
|
{
|
2016-10-27 14:05:25 -07:00
|
|
|
case "artist-list":
|
|
|
|
{
|
2016-12-03 16:57:34 -07:00
|
|
|
if (reader.IsEmptyElement)
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-27 14:05:25 -07:00
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
{
|
2019-04-30 13:18:40 -07:00
|
|
|
return ParseArtistList(subReader).ToList();
|
2016-10-27 14:05:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
2016-10-27 12:03:23 -07:00
|
|
|
}
|
|
|
|
}
|
2016-10-31 11:59:58 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
2016-10-27 14:05:25 -07:00
|
|
|
}
|
|
|
|
|
2019-03-13 14:32:52 -07:00
|
|
|
return Enumerable.Empty<RemoteSearchResult>();
|
2016-10-27 12:03:23 -07:00
|
|
|
}
|
2016-05-08 20:13:38 -07:00
|
|
|
}
|
2016-10-27 12:03:23 -07:00
|
|
|
}
|
|
|
|
|
2019-03-13 14:32:52 -07:00
|
|
|
private IEnumerable<RemoteSearchResult> ParseArtistList(XmlReader reader)
|
2016-10-27 12:03:23 -07:00
|
|
|
{
|
|
|
|
reader.MoveToContent();
|
2016-11-02 10:08:20 -07:00
|
|
|
reader.Read();
|
2014-03-13 20:23:58 -07:00
|
|
|
|
2016-10-27 12:03:23 -07:00
|
|
|
// Loop through each element
|
2016-12-03 14:46:06 -07:00
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
2014-02-02 09:59:14 -07:00
|
|
|
{
|
2016-10-27 14:05:25 -07:00
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2014-03-13 20:23:58 -07:00
|
|
|
{
|
2016-10-27 14:05:25 -07:00
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "artist":
|
2016-10-08 11:51:07 -07:00
|
|
|
{
|
2016-12-03 16:57:34 -07:00
|
|
|
if (reader.IsEmptyElement)
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-27 14:05:25 -07:00
|
|
|
var mbzId = reader.GetAttribute("id");
|
|
|
|
|
|
|
|
using (var subReader = reader.ReadSubtree())
|
|
|
|
{
|
|
|
|
var artist = ParseArtist(subReader, mbzId);
|
|
|
|
if (artist != null)
|
|
|
|
{
|
2019-03-13 14:32:52 -07:00
|
|
|
yield return artist;
|
2016-10-27 14:05:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2016-05-08 20:13:38 -07:00
|
|
|
}
|
2016-10-27 14:05:25 -07:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-27 12:03:23 -07:00
|
|
|
}
|
2016-10-31 11:59:58 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
2016-10-27 12:03:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private RemoteSearchResult ParseArtist(XmlReader reader, string artistId)
|
|
|
|
{
|
|
|
|
var result = new RemoteSearchResult();
|
2014-03-13 20:23:58 -07:00
|
|
|
|
2016-10-27 12:03:23 -07:00
|
|
|
reader.MoveToContent();
|
2016-10-27 14:05:25 -07:00
|
|
|
reader.Read();
|
|
|
|
|
|
|
|
// http://stackoverflow.com/questions/2299632/why-does-xmlreader-skip-every-other-element-if-there-is-no-whitespace-separator
|
2014-03-13 20:23:58 -07:00
|
|
|
|
2016-10-27 12:03:23 -07:00
|
|
|
// Loop through each element
|
2016-12-03 14:46:06 -07:00
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
2016-10-27 12:03:23 -07:00
|
|
|
{
|
2016-10-27 14:05:25 -07:00
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2016-10-27 12:03:23 -07:00
|
|
|
{
|
2016-10-27 14:05:25 -07:00
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "name":
|
|
|
|
{
|
|
|
|
result.Name = reader.ReadElementContentAsString();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "annotation":
|
|
|
|
{
|
|
|
|
result.Overview = reader.ReadElementContentAsString();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
// there is sort-name if ever needed
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
2014-03-13 20:23:58 -07:00
|
|
|
}
|
2014-02-02 09:59:14 -07:00
|
|
|
}
|
|
|
|
|
2016-10-27 12:03:23 -07:00
|
|
|
result.SetProviderId(MetadataProviders.MusicBrainzArtist, artistId);
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(artistId) || string.IsNullOrWhiteSpace(result.Name))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2014-03-13 20:23:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo id, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var result = new MetadataResult<MusicArtist>
|
2014-02-02 09:59:14 -07:00
|
|
|
{
|
2014-03-13 20:23:58 -07:00
|
|
|
Item = new MusicArtist()
|
|
|
|
};
|
2014-02-02 09:59:14 -07:00
|
|
|
|
2014-03-13 20:23:58 -07:00
|
|
|
var musicBrainzId = id.GetMusicBrainzArtistId();
|
2014-02-02 09:59:14 -07:00
|
|
|
|
2014-03-13 20:23:58 -07:00
|
|
|
if (string.IsNullOrWhiteSpace(musicBrainzId))
|
|
|
|
{
|
|
|
|
var searchResults = await GetSearchResults(id, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
var singleResult = searchResults.FirstOrDefault();
|
2014-02-02 09:59:14 -07:00
|
|
|
|
2014-03-13 20:23:58 -07:00
|
|
|
if (singleResult != null)
|
2014-02-02 09:59:14 -07:00
|
|
|
{
|
2014-03-13 20:23:58 -07:00
|
|
|
musicBrainzId = singleResult.GetProviderId(MetadataProviders.MusicBrainzArtist);
|
2015-04-25 20:25:07 -07:00
|
|
|
//result.Item.Name = singleResult.Name;
|
2016-10-08 11:51:07 -07:00
|
|
|
result.Item.Overview = singleResult.Overview;
|
2014-02-02 09:59:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-13 20:23:58 -07:00
|
|
|
if (!string.IsNullOrWhiteSpace(musicBrainzId))
|
|
|
|
{
|
|
|
|
result.HasMetadata = true;
|
|
|
|
result.Item.SetProviderId(MetadataProviders.MusicBrainzArtist, musicBrainzId);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2014-02-02 09:59:14 -07: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)
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
return !string.Equals(text, text.RemoveDiacritics(), StringComparison.Ordinal);
|
2014-02-02 09:59:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Encodes an URL.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
/// <returns>System.String.</returns>
|
2019-03-13 14:32:52 -07:00
|
|
|
private static string UrlEncode(string name)
|
2014-02-02 09:59:14 -07:00
|
|
|
{
|
|
|
|
return WebUtility.UrlEncode(name);
|
|
|
|
}
|
|
|
|
|
2019-01-13 13:31:14 -07:00
|
|
|
public string Name => "MusicBrainz";
|
2014-02-19 21:53:15 -07:00
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
2014-02-02 09:59:14 -07:00
|
|
|
}
|
|
|
|
}
|