jellyfin/MediaBrowser.Providers/Music/MusicBrainzArtistProvider.cs

290 lines
10 KiB
C#
Raw Normal View History

using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
using System;
using System.Collections.Generic;
using System.Globalization;
2016-10-27 21:05:25 +00:00
using System.IO;
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;
2016-10-27 21:05:25 +00:00
using MediaBrowser.Model.Xml;
namespace MediaBrowser.Providers.Music
{
2014-02-07 03:10:13 +00:00
public class MusicBrainzArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>
{
2016-10-27 21:05:25 +00:00
private readonly IXmlReaderSettingsFactory _xmlSettings;
public MusicBrainzArtistProvider(IXmlReaderSettingsFactory xmlSettings)
{
_xmlSettings = xmlSettings;
}
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
{
2014-03-14 03:23:58 +00:00
var musicBrainzId = searchInfo.GetMusicBrainzArtistId();
if (!string.IsNullOrWhiteSpace(musicBrainzId))
{
2016-06-15 19:52:38 +00:00
var url = string.Format("/ws/2/artist/?query=arid:{0}", musicBrainzId);
2016-10-27 21:05:25 +00:00
using (var stream = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, false, cancellationToken)
2016-10-27 19:03:23 +00:00
.ConfigureAwait(false))
{
2016-10-27 21:05:25 +00:00
return GetResultsFromResponse(stream);
2016-10-27 19:03:23 +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('/', ' ');
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 21:05:25 +00:00
using (var stream = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false))
2014-03-14 03:23:58 +00:00
{
var results = GetResultsFromResponse(stream);
2016-10-27 19:03:23 +00:00
if (results.Count > 0)
{
return results;
}
2014-03-14 03:23:58 +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 21:05:25 +00:00
using (var stream = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false))
2016-10-27 19:03:23 +00:00
{
2016-10-27 21:05:25 +00:00
return GetResultsFromResponse(stream);
2016-10-27 19:03:23 +00:00
}
2014-03-14 03:23:58 +00:00
}
}
2014-03-14 03:23:58 +00:00
return new List<RemoteSearchResult>();
}
private List<RemoteSearchResult> GetResultsFromResponse(Stream stream)
2014-03-14 03:23:58 +00:00
{
2016-10-27 21:05:25 +00:00
using (var oReader = new StreamReader(stream, Encoding.UTF8))
2016-05-09 03:13:38 +00:00
{
2016-10-27 21:05:25 +00:00
var settings = _xmlSettings.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
using (var reader = XmlReader.Create(oReader, settings))
2016-10-27 19:03:23 +00:00
{
2016-10-27 21:05:25 +00:00
reader.MoveToContent();
2016-11-02 17:08:20 +00:00
reader.Read();
2016-10-27 21:05:25 +00:00
// Loop through each element
2016-12-03 21:46:06 +00:00
while (!reader.EOF && reader.ReadState == ReadState.Interactive)
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)
2016-10-27 19:03:23 +00:00
{
2016-10-27 21:05:25 +00:00
case "artist-list":
{
2016-12-03 23:57:34 +00:00
if (reader.IsEmptyElement)
{
reader.Read();
continue;
}
2016-10-27 21:05:25 +00:00
using (var subReader = reader.ReadSubtree())
{
return ParseArtistList(subReader);
}
}
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 21:05:25 +00:00
}
return new List<RemoteSearchResult>();
2016-10-27 19:03:23 +00:00
}
2016-05-09 03:13:38 +00:00
}
2016-10-27 19:03:23 +00:00
}
private List<RemoteSearchResult> ParseArtistList(XmlReader reader)
2016-10-27 19:03:23 +00:00
{
var list = new List<RemoteSearchResult>();
2016-05-09 03:13:38 +00:00
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)
{
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;
}
2016-10-27 21:05:25 +00:00
var mbzId = reader.GetAttribute("id");
using (var subReader = reader.ReadSubtree())
{
var artist = ParseArtist(subReader, mbzId);
if (artist != null)
{
list.Add(artist);
}
}
break;
2016-05-09 03:13:38 +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
}
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();
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;
}
case "annotation":
{
result.Overview = reader.ReadElementContentAsString();
break;
}
default:
{
// there is sort-name if ever needed
reader.Skip();
break;
}
}
}
else
{
reader.Read();
2014-03-14 03:23:58 +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-03-14 03:23:58 +00:00
Item = new MusicArtist()
};
2014-03-14 03:23:58 +00:00
var musicBrainzId = id.GetMusicBrainzArtistId();
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-03-14 03:23:58 +00:00
if (singleResult != null)
{
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-03-14 03:23:58 +00:00
if (!string.IsNullOrWhiteSpace(musicBrainzId))
{
result.HasMetadata = true;
result.Item.SetProviderId(MetadataProviders.MusicBrainzArtist, musicBrainzId);
}
return result;
}
/// <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);
}
/// <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"; }
}
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
}