2020-06-19 18:24:13 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 20:03:10 +00:00
|
|
|
using System;
|
2014-02-20 04:53:15 +00:00
|
|
|
using System.Collections.Generic;
|
2019-09-10 20:37:53 +00:00
|
|
|
using System.Globalization;
|
2016-10-27 21:05:25 +00:00
|
|
|
using System.IO;
|
2014-02-02 16:59:14 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
2020-08-17 19:10:02 +00:00
|
|
|
using System.Net.Http;
|
2014-02-02 16:59:14 +00:00
|
|
|
using System.Text;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Xml;
|
2021-06-12 09:22:26 +00:00
|
|
|
using Diacritics.Extensions;
|
2019-01-13 19:26:31 +00:00
|
|
|
using MediaBrowser.Controller.Entities.Audio;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.Providers;
|
2020-02-22 06:04:52 +00:00
|
|
|
using MediaBrowser.Providers.Plugins.MusicBrainz;
|
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
|
|
|
{
|
2019-09-10 20:37:53 +00:00
|
|
|
/// <inheritdoc />
|
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))
|
|
|
|
{
|
2019-09-10 20:37:53 +00:00
|
|
|
var url = "/ws/2/artist/?query=arid:{0}" + musicBrainzId.ToString(CultureInfo.InvariantCulture);
|
2014-02-02 16:59:14 +00:00
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
using var response = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
|
2020-11-17 18:43:00 +00:00
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2020-08-17 19:10:02 +00:00
|
|
|
return GetResultsFromResponse(stream);
|
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
|
|
|
|
2020-08-07 17:26:28 +00:00
|
|
|
var url = string.Format(CultureInfo.InvariantCulture, "/ws/2/artist/?query=\"{0}\"&dismax=true", UrlEncode(nameToSearch));
|
2014-03-14 03:23:58 +00:00
|
|
|
|
2019-03-13 20:31:21 +00:00
|
|
|
using (var response = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false))
|
2020-11-17 18:43:00 +00:00
|
|
|
await using (var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false))
|
2014-03-14 03:23:58 +00:00
|
|
|
{
|
2019-09-10 20:37:53 +00:00
|
|
|
var results = GetResultsFromResponse(stream).ToList();
|
2017-10-20 16:16:56 +00:00
|
|
|
|
2019-09-10 20:37:53 +00:00
|
|
|
if (results.Count > 0)
|
|
|
|
{
|
|
|
|
return results;
|
2016-10-27 19:03:23 +00:00
|
|
|
}
|
2014-03-14 03:23:58 +00:00
|
|
|
}
|
2014-02-02 16:59:14 +00:00
|
|
|
|
2021-06-12 09:22:26 +00:00
|
|
|
if (searchInfo.Name.HasDiacritics())
|
2014-03-14 03:23:58 +00:00
|
|
|
{
|
|
|
|
// Try again using the search with accent characters url
|
2020-08-07 17:26:28 +00:00
|
|
|
url = string.Format(CultureInfo.InvariantCulture, "/ws/2/artist/?query=artistaccent:\"{0}\"", UrlEncode(nameToSearch));
|
2014-03-14 03:23:58 +00:00
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
using var response = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
|
2020-11-17 18:43:00 +00:00
|
|
|
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
|
2020-08-17 19:10:02 +00:00
|
|
|
return GetResultsFromResponse(stream);
|
2014-03-14 03:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-02 16:59:14 +00:00
|
|
|
|
2019-03-13 21:32:52 +00:00
|
|
|
return Enumerable.Empty<RemoteSearchResult>();
|
2014-03-14 03:23:58 +00:00
|
|
|
}
|
2014-02-02 16:59:14 +00:00
|
|
|
|
2019-03-13 21:32:52 +00:00
|
|
|
private IEnumerable<RemoteSearchResult> GetResultsFromResponse(Stream stream)
|
2014-03-14 03:23:58 +00:00
|
|
|
{
|
2021-02-26 01:38:18 +00:00
|
|
|
using var oReader = new StreamReader(stream, Encoding.UTF8);
|
|
|
|
var settings = new XmlReaderSettings()
|
2016-05-09 03:13:38 +00:00
|
|
|
{
|
2021-02-26 01:38:18 +00:00
|
|
|
ValidationType = ValidationType.None,
|
|
|
|
CheckCharacters = false,
|
|
|
|
IgnoreProcessingInstructions = true,
|
|
|
|
IgnoreComments = true
|
|
|
|
};
|
2016-10-27 21:05:25 +00:00
|
|
|
|
2021-02-26 01:38:18 +00:00
|
|
|
using var reader = XmlReader.Create(oReader, settings);
|
|
|
|
reader.MoveToContent();
|
|
|
|
reader.Read();
|
2016-10-27 21:05:25 +00:00
|
|
|
|
2021-02-26 01:38:18 +00:00
|
|
|
// Loop through each element
|
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
|
|
|
{
|
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
|
|
|
{
|
|
|
|
switch (reader.Name)
|
2016-10-27 21:05:25 +00:00
|
|
|
{
|
2021-02-26 01:38:18 +00:00
|
|
|
case "artist-list":
|
2016-10-27 19:03:23 +00:00
|
|
|
{
|
2021-02-26 01:38:18 +00:00
|
|
|
if (reader.IsEmptyElement)
|
2016-10-27 19:03:23 +00:00
|
|
|
{
|
2021-02-26 01:38:18 +00:00
|
|
|
reader.Read();
|
|
|
|
continue;
|
2016-10-27 19:03:23 +00:00
|
|
|
}
|
2021-02-26 01:38:18 +00:00
|
|
|
|
|
|
|
using var subReader = reader.ReadSubtree();
|
|
|
|
return ParseArtistList(subReader).ToList();
|
2016-10-27 19:03:23 +00:00
|
|
|
}
|
2021-02-26 01:38:18 +00:00
|
|
|
|
|
|
|
default:
|
2016-10-31 18:59:58 +00:00
|
|
|
{
|
2021-02-26 01:38:18 +00:00
|
|
|
reader.Skip();
|
|
|
|
break;
|
2016-10-31 18:59:58 +00:00
|
|
|
}
|
2016-10-27 21:05:25 +00:00
|
|
|
}
|
2021-02-26 01:38:18 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
2016-10-27 19:03:23 +00:00
|
|
|
}
|
2016-05-09 03:13:38 +00:00
|
|
|
}
|
2021-02-26 01:38:18 +00:00
|
|
|
|
|
|
|
return Enumerable.Empty<RemoteSearchResult>();
|
2016-10-27 19:03:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-13 21:32:52 +00:00
|
|
|
private IEnumerable<RemoteSearchResult> ParseArtistList(XmlReader reader)
|
2016-10-27 19:03:23 +00:00
|
|
|
{
|
|
|
|
reader.MoveToContent();
|
2016-11-02 17:08:20 +00:00
|
|
|
reader.Read();
|
2014-03-14 03:23:58 +00:00
|
|
|
|
2016-10-27 19:03:23 +00:00
|
|
|
// Loop through each element
|
2016-12-03 21:46:06 +00:00
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
2014-02-02 16:59:14 +00:00
|
|
|
{
|
2016-10-27 21:05:25 +00:00
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2014-03-14 03:23:58 +00:00
|
|
|
{
|
2016-10-27 21:05:25 +00:00
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "artist":
|
2016-10-08 18:51:07 +00:00
|
|
|
{
|
2016-12-03 23:57:34 +00:00
|
|
|
if (reader.IsEmptyElement)
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
continue;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2016-10-27 21:05:25 +00:00
|
|
|
var mbzId = reader.GetAttribute("id");
|
|
|
|
|
2021-02-26 01:38:18 +00:00
|
|
|
using var subReader = reader.ReadSubtree();
|
|
|
|
var artist = ParseArtist(subReader, mbzId);
|
|
|
|
if (artist != null)
|
2016-10-27 21:05:25 +00:00
|
|
|
{
|
2021-02-26 01:38:18 +00:00
|
|
|
yield return artist;
|
2016-10-27 21:05:25 +00:00
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2016-10-27 21:05:25 +00:00
|
|
|
break;
|
2016-05-09 03:13:38 +00:00
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2016-10-27 21:05:25 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-27 19:03:23 +00:00
|
|
|
}
|
2016-10-31 18:59:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
|
|
|
}
|
2016-10-27 19:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2016-10-27 21:05:25 +00:00
|
|
|
reader.Read();
|
|
|
|
|
|
|
|
// http://stackoverflow.com/questions/2299632/why-does-xmlreader-skip-every-other-element-if-there-is-no-whitespace-separator
|
2014-03-14 03:23:58 +00:00
|
|
|
|
2016-10-27 19:03:23 +00:00
|
|
|
// Loop through each element
|
2016-12-03 21:46:06 +00:00
|
|
|
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
|
2016-10-27 19:03:23 +00:00
|
|
|
{
|
2016-10-27 21:05:25 +00:00
|
|
|
if (reader.NodeType == XmlNodeType.Element)
|
2016-10-27 19:03:23 +00:00
|
|
|
{
|
2016-10-27 21:05:25 +00:00
|
|
|
switch (reader.Name)
|
|
|
|
{
|
|
|
|
case "name":
|
|
|
|
{
|
|
|
|
result.Name = reader.ReadElementContentAsString();
|
|
|
|
break;
|
|
|
|
}
|
2020-09-28 20:04:31 +00:00
|
|
|
|
2016-10-27 21:05:25 +00:00
|
|
|
case "annotation":
|
|
|
|
{
|
|
|
|
result.Overview = reader.ReadElementContentAsString();
|
|
|
|
break;
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2016-10-27 21:05:25 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
// there is sort-name if ever needed
|
|
|
|
reader.Skip();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
reader.Read();
|
2014-03-14 03:23:58 +00:00
|
|
|
}
|
2014-02-02 16:59:14 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 19:17:49 +00:00
|
|
|
result.SetProviderId(MetadataProvider.MusicBrainzArtist, artistId);
|
2016-10-27 19:03:23 +00:00
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(artistId) || string.IsNullOrWhiteSpace(result.Name))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2014-03-14 03:23:58 +00:00
|
|
|
}
|
|
|
|
|
2021-06-27 00:00:17 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken)
|
2014-03-14 03:23:58 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2021-06-27 00:00:17 +00:00
|
|
|
var musicBrainzId = info.GetMusicBrainzArtistId();
|
2014-02-02 16:59:14 +00:00
|
|
|
|
2014-03-14 03:23:58 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(musicBrainzId))
|
|
|
|
{
|
2021-06-27 00:00:17 +00:00
|
|
|
var searchResults = await GetSearchResults(info, cancellationToken).ConfigureAwait(false);
|
2014-03-14 03:23:58 +00:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2020-06-06 19:17:49 +00:00
|
|
|
musicBrainzId = singleResult.GetProviderId(MetadataProvider.MusicBrainzArtist);
|
2016-10-08 18:51:07 +00:00
|
|
|
result.Item.Overview = singleResult.Overview;
|
2020-02-22 06:04:52 +00:00
|
|
|
|
|
|
|
if (Plugin.Instance.Configuration.ReplaceArtistName)
|
|
|
|
{
|
|
|
|
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;
|
2020-06-06 19:17:49 +00:00
|
|
|
result.Item.SetProviderId(MetadataProvider.MusicBrainzArtist, musicBrainzId);
|
2014-03-14 03:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2014-02-02 16:59:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Encodes an URL.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="name">The name.</param>
|
|
|
|
/// <returns>System.String.</returns>
|
2019-03-13 21:32:52 +00:00
|
|
|
private static string UrlEncode(string name)
|
2014-02-02 16:59:14 +00:00
|
|
|
{
|
|
|
|
return WebUtility.UrlEncode(name);
|
|
|
|
}
|
|
|
|
|
2019-01-13 20:31:14 +00:00
|
|
|
public string Name => "MusicBrainz";
|
2014-02-20 04:53:15 +00:00
|
|
|
|
2020-08-17 19:10:02 +00:00
|
|
|
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
|
2014-02-20 04:53:15 +00:00
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
2014-02-02 16:59:14 +00:00
|
|
|
}
|
|
|
|
}
|