update music brainz providers

This commit is contained in:
Luke Pulverenti 2016-10-27 17:05:25 -04:00
parent e12f27d8ed
commit 2de485eb9f
2 changed files with 297 additions and 178 deletions

View File

@ -74,18 +74,30 @@ namespace MediaBrowser.Providers.Music
if (!string.IsNullOrWhiteSpace(url)) if (!string.IsNullOrWhiteSpace(url))
{ {
using (var reader = await GetMusicBrainzResponse(url, isNameSearch, cancellationToken).ConfigureAwait(false)) using (var stream = await GetMusicBrainzResponse(url, isNameSearch, cancellationToken).ConfigureAwait(false))
{ {
return GetResultsFromResponse(reader); return GetResultsFromResponse(stream);
} }
} }
return new List<RemoteSearchResult>(); return new List<RemoteSearchResult>();
} }
private IEnumerable<RemoteSearchResult> GetResultsFromResponse(XmlReader reader) private List<RemoteSearchResult> GetResultsFromResponse(Stream stream)
{ {
return ReleaseResult.Parse(reader).Select(i => using (var oReader = new StreamReader(stream, Encoding.UTF8))
{
var settings = _xmlSettings.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
using (var reader = XmlReader.Create(oReader, settings))
{
var results = ReleaseResult.Parse(reader);
return results.Select(i =>
{ {
var result = new RemoteSearchResult var result = new RemoteSearchResult
{ {
@ -103,7 +115,9 @@ namespace MediaBrowser.Providers.Music
} }
return result; return result;
}); }).ToList();
}
}
} }
public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo id, CancellationToken cancellationToken) public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo id, CancellationToken cancellationToken)
@ -195,9 +209,21 @@ namespace MediaBrowser.Providers.Music
WebUtility.UrlEncode(albumName), WebUtility.UrlEncode(albumName),
artistId); artistId);
using (var reader = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false)) using (var stream = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false))
{ {
return ReleaseResult.Parse(reader, 1).FirstOrDefault(); using (var oReader = new StreamReader(stream, Encoding.UTF8))
{
var settings = _xmlSettings.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
using (var reader = XmlReader.Create(oReader, settings))
{
return ReleaseResult.Parse(reader).FirstOrDefault();
}
}
} }
} }
@ -207,9 +233,21 @@ namespace MediaBrowser.Providers.Music
WebUtility.UrlEncode(albumName), WebUtility.UrlEncode(albumName),
WebUtility.UrlEncode(artistName)); WebUtility.UrlEncode(artistName));
using (var reader = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false)) using (var stream = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false))
{ {
return ReleaseResult.Parse(reader, 1).FirstOrDefault(); using (var oReader = new StreamReader(stream, Encoding.UTF8))
{
var settings = _xmlSettings.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
using (var reader = XmlReader.Create(oReader, settings))
{
return ReleaseResult.Parse(reader).FirstOrDefault();
}
}
} }
} }
@ -221,12 +259,16 @@ namespace MediaBrowser.Providers.Music
public string Overview; public string Overview;
public int? Year; public int? Year;
public static List<ReleaseResult> Parse(XmlReader reader, int? limit = null) public static List<ReleaseResult> Parse(XmlReader reader)
{ {
var list = new List<ReleaseResult>();
reader.MoveToContent(); reader.MoveToContent();
// Loop through each element // Loop through each element
while (reader.Read()) while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{ {
switch (reader.Name) switch (reader.Name)
{ {
@ -234,8 +276,9 @@ namespace MediaBrowser.Providers.Music
{ {
using (var subReader = reader.ReadSubtree()) using (var subReader = reader.ReadSubtree())
{ {
return ParseReleaseList(subReader); list.AddRange(ParseReleaseList(subReader));
} }
break;
} }
default: default:
{ {
@ -244,8 +287,9 @@ namespace MediaBrowser.Providers.Music
} }
} }
} }
}
return new List<ReleaseResult>(); return list;
} }
private static List<ReleaseResult> ParseReleaseList(XmlReader reader) private static List<ReleaseResult> ParseReleaseList(XmlReader reader)
@ -256,17 +300,23 @@ namespace MediaBrowser.Providers.Music
// Loop through each element // Loop through each element
while (reader.Read()) while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{ {
switch (reader.Name) switch (reader.Name)
{ {
case "release": case "release":
{ {
var releaseId = reader.GetAttribute("id"); //var releaseId = reader.GetAttribute("id");
string releaseId = null;
using (var subReader = reader.ReadSubtree()) using (var subReader = reader.ReadSubtree())
{ {
var artist = ParseRelease(subReader, releaseId); var release = ParseRelease(subReader, releaseId);
list.Add(artist); if (release != null)
{
list.Add(release);
}
} }
break; break;
} }
@ -277,6 +327,7 @@ namespace MediaBrowser.Providers.Music
} }
} }
} }
}
return list; return list;
} }
@ -290,8 +341,14 @@ namespace MediaBrowser.Providers.Music
reader.MoveToContent(); reader.MoveToContent();
reader.Read();
// http://stackoverflow.com/questions/2299632/why-does-xmlreader-skip-every-other-element-if-there-is-no-whitespace-separator
// Loop through each element // Loop through each element
while (reader.Read()) while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element)
{ {
switch (reader.Name) switch (reader.Name)
{ {
@ -318,8 +375,26 @@ namespace MediaBrowser.Providers.Music
case "release-group": case "release-group":
{ {
result.ReleaseGroupId = reader.GetAttribute("id"); result.ReleaseGroupId = reader.GetAttribute("id");
//explicitly consume these to avoid grabbing data from child nodes
//reader.Skip();
using (var subtree = reader.ReadSubtree())
{
}
break; break;
} }
//explicitly consume these to avoid grabbing data from child nodes
//case "text-representation":
//case "artist-credit":
//case "medium-list":
//case "tag-list":
//case "label-info-list":
//case "release-event-list":
// {
// using (var subtree = reader.ReadSubtree())
// {
// }
// break;
// }
default: default:
{ {
reader.Skip(); reader.Skip();
@ -327,6 +402,11 @@ namespace MediaBrowser.Providers.Music
} }
} }
} }
else
{
reader.Read();
}
}
return result; return result;
} }
@ -342,12 +422,24 @@ namespace MediaBrowser.Providers.Music
{ {
var url = string.Format("/ws/2/release-group/?query=reid:{0}", releaseEntryId); var url = string.Format("/ws/2/release-group/?query=reid:{0}", releaseEntryId);
using (var reader = await GetMusicBrainzResponse(url, false, cancellationToken).ConfigureAwait(false)) using (var stream = await GetMusicBrainzResponse(url, false, cancellationToken).ConfigureAwait(false))
{
using (var oReader = new StreamReader(stream, Encoding.UTF8))
{
var settings = _xmlSettings.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
using (var reader = XmlReader.Create(oReader, settings))
{ {
reader.MoveToContent(); reader.MoveToContent();
// Loop through each element // Loop through each element
while (reader.Read()) while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{ {
switch (reader.Name) switch (reader.Name)
{ {
@ -365,9 +457,12 @@ namespace MediaBrowser.Providers.Music
} }
} }
} }
}
return null; return null;
} }
} }
}
}
private string GetFirstReleaseGroupId(XmlReader reader) private string GetFirstReleaseGroupId(XmlReader reader)
{ {
@ -375,6 +470,8 @@ namespace MediaBrowser.Providers.Music
// Loop through each element // Loop through each element
while (reader.Read()) while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{ {
switch (reader.Name) switch (reader.Name)
{ {
@ -389,6 +486,7 @@ namespace MediaBrowser.Providers.Music
} }
} }
} }
}
return null; return null;
} }
@ -465,7 +563,7 @@ namespace MediaBrowser.Providers.Music
/// <param name="isSearch">if set to <c>true</c> [is search].</param> /// <param name="isSearch">if set to <c>true</c> [is search].</param>
/// <param name="cancellationToken">The cancellation token.</param> /// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{XmlDocument}.</returns> /// <returns>Task{XmlDocument}.</returns>
internal async Task<XmlReader> GetMusicBrainzResponse(string url, bool isSearch, CancellationToken cancellationToken) internal async Task<Stream> GetMusicBrainzResponse(string url, bool isSearch, CancellationToken cancellationToken)
{ {
var urlInfo = await GetMbzUrl().ConfigureAwait(false); var urlInfo = await GetMbzUrl().ConfigureAwait(false);
@ -485,19 +583,7 @@ namespace MediaBrowser.Providers.Music
ResourcePool = _musicBrainzResourcePool ResourcePool = _musicBrainzResourcePool
}; };
using (var xml = await _httpClient.Get(options).ConfigureAwait(false)) return await _httpClient.Get(options).ConfigureAwait(false);
{
using (var oReader = new StreamReader(xml, Encoding.UTF8))
{
var settings = _xmlSettings.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
return XmlReader.Create(oReader, settings);
}
}
} }
public int Order public int Order

View File

@ -6,6 +6,7 @@ using MediaBrowser.Model.Providers;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Text; using System.Text;
@ -13,11 +14,19 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Xml; using System.Xml;
using MediaBrowser.Controller.Extensions; using MediaBrowser.Controller.Extensions;
using MediaBrowser.Model.Xml;
namespace MediaBrowser.Providers.Music namespace MediaBrowser.Providers.Music
{ {
public class MusicBrainzArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo> public class MusicBrainzArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>
{ {
private readonly IXmlReaderSettingsFactory _xmlSettings;
public MusicBrainzArtistProvider(IXmlReaderSettingsFactory xmlSettings)
{
_xmlSettings = xmlSettings;
}
public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken) public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
{ {
var musicBrainzId = searchInfo.GetMusicBrainzArtistId(); var musicBrainzId = searchInfo.GetMusicBrainzArtistId();
@ -26,10 +35,10 @@ namespace MediaBrowser.Providers.Music
{ {
var url = string.Format("/ws/2/artist/?query=arid:{0}", musicBrainzId); var url = string.Format("/ws/2/artist/?query=arid:{0}", musicBrainzId);
using (var reader = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, false, cancellationToken) using (var stream = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, false, cancellationToken)
.ConfigureAwait(false)) .ConfigureAwait(false))
{ {
return GetResultsFromResponse(reader); return GetResultsFromResponse(stream);
} }
} }
else else
@ -39,9 +48,9 @@ namespace MediaBrowser.Providers.Music
var url = String.Format("/ws/2/artist/?query=artist:\"{0}\"", UrlEncode(nameToSearch)); var url = String.Format("/ws/2/artist/?query=artist:\"{0}\"", UrlEncode(nameToSearch));
using (var doc = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false)) using (var stream = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false))
{ {
var results = GetResultsFromResponse(doc).ToList(); var results = GetResultsFromResponse(stream).ToList();
if (results.Count > 0) if (results.Count > 0)
{ {
@ -54,9 +63,9 @@ namespace MediaBrowser.Providers.Music
// Try again using the search with accent characters url // Try again using the search with accent characters url
url = String.Format("/ws/2/artist/?query=artistaccent:\"{0}\"", UrlEncode(nameToSearch)); url = String.Format("/ws/2/artist/?query=artistaccent:\"{0}\"", UrlEncode(nameToSearch));
using (var doc = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false)) using (var stream = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false))
{ {
return GetResultsFromResponse(doc); return GetResultsFromResponse(stream);
} }
} }
} }
@ -64,12 +73,24 @@ namespace MediaBrowser.Providers.Music
return new List<RemoteSearchResult>(); return new List<RemoteSearchResult>();
} }
private IEnumerable<RemoteSearchResult> GetResultsFromResponse(XmlReader reader) private IEnumerable<RemoteSearchResult> GetResultsFromResponse(Stream stream)
{
using (var oReader = new StreamReader(stream, Encoding.UTF8))
{
var settings = _xmlSettings.Create(false);
settings.CheckCharacters = false;
settings.IgnoreProcessingInstructions = true;
settings.IgnoreComments = true;
using (var reader = XmlReader.Create(oReader, settings))
{ {
reader.MoveToContent(); reader.MoveToContent();
// Loop through each element // Loop through each element
while (reader.Read()) while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{ {
switch (reader.Name) switch (reader.Name)
{ {
@ -87,9 +108,12 @@ namespace MediaBrowser.Providers.Music
} }
} }
} }
}
return new List<RemoteSearchResult>(); return new List<RemoteSearchResult>();
} }
}
}
private IEnumerable<RemoteSearchResult> ParseArtistList(XmlReader reader) private IEnumerable<RemoteSearchResult> ParseArtistList(XmlReader reader)
{ {
@ -99,6 +123,8 @@ namespace MediaBrowser.Providers.Music
// Loop through each element // Loop through each element
while (reader.Read()) while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{ {
switch (reader.Name) switch (reader.Name)
{ {
@ -109,8 +135,11 @@ namespace MediaBrowser.Providers.Music
using (var subReader = reader.ReadSubtree()) using (var subReader = reader.ReadSubtree())
{ {
var artist = ParseArtist(subReader, mbzId); var artist = ParseArtist(subReader, mbzId);
if (artist != null)
{
list.Add(artist); list.Add(artist);
} }
}
break; break;
} }
default: default:
@ -120,6 +149,7 @@ namespace MediaBrowser.Providers.Music
} }
} }
} }
}
return list; return list;
} }
@ -129,9 +159,14 @@ namespace MediaBrowser.Providers.Music
var result = new RemoteSearchResult(); var result = new RemoteSearchResult();
reader.MoveToContent(); reader.MoveToContent();
reader.Read();
// http://stackoverflow.com/questions/2299632/why-does-xmlreader-skip-every-other-element-if-there-is-no-whitespace-separator
// Loop through each element // Loop through each element
while (reader.Read()) while (!reader.EOF)
{
if (reader.NodeType == XmlNodeType.Element)
{ {
switch (reader.Name) switch (reader.Name)
{ {
@ -140,14 +175,6 @@ namespace MediaBrowser.Providers.Music
result.Name = reader.ReadElementContentAsString(); result.Name = reader.ReadElementContentAsString();
break; break;
} }
//case "sort-name":
// {
// break;
// }
//case "tag-list":
// {
// break;
// }
case "annotation": case "annotation":
{ {
result.Overview = reader.ReadElementContentAsString(); result.Overview = reader.ReadElementContentAsString();
@ -155,11 +182,17 @@ namespace MediaBrowser.Providers.Music
} }
default: default:
{ {
// there is sort-name if ever needed
reader.Skip(); reader.Skip();
break; break;
} }
} }
} }
else
{
reader.Read();
}
}
result.SetProviderId(MetadataProviders.MusicBrainzArtist, artistId); result.SetProviderId(MetadataProviders.MusicBrainzArtist, artistId);