fix music brainz album search results

This commit is contained in:
Luke Pulverenti 2016-08-16 14:45:57 -04:00
parent 8a7b24000d
commit 1d37f9ad0e

View File

@ -81,46 +81,24 @@ namespace MediaBrowser.Providers.Music
private IEnumerable<RemoteSearchResult> GetResultsFromResponse(XmlDocument doc) private IEnumerable<RemoteSearchResult> GetResultsFromResponse(XmlDocument doc)
{ {
var ns = new XmlNamespaceManager(doc.NameTable); return ReleaseResult.Parse(doc).Select(i =>
ns.AddNamespace("mb", MusicBrainzBaseUrl + "/ns/mmd-2.0#");
var list = new List<RemoteSearchResult>();
var nodes = doc.SelectNodes("//mb:release-list/mb:release", ns);
if (nodes != null)
{ {
foreach (var node in nodes.Cast<XmlNode>()) var result = new RemoteSearchResult
{ {
if (node.Attributes != null) Name = i.Title
{ };
string name = null;
string mbzId = node.Attributes["id"].Value; if (!string.IsNullOrWhiteSpace(i.ReleaseId))
{
var nameNode = node.SelectSingleNode("//mb:title", ns); result.SetProviderId(MetadataProviders.MusicBrainzAlbum, i.ReleaseId);
}
if (nameNode != null) if (!string.IsNullOrWhiteSpace(i.ReleaseGroupId))
{ {
name = nameNode.InnerText; result.SetProviderId(MetadataProviders.MusicBrainzAlbum, i.ReleaseGroupId);
}
if (!string.IsNullOrWhiteSpace(mbzId) && !string.IsNullOrWhiteSpace(name))
{
var result = new RemoteSearchResult
{
Name = name
};
result.SetProviderId(MetadataProviders.MusicBrainzAlbum, mbzId);
list.Add(result);
}
}
} }
}
return list; return result;
});
} }
public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo id, CancellationToken cancellationToken) public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo id, CancellationToken cancellationToken)
@ -208,7 +186,7 @@ namespace MediaBrowser.Providers.Music
var doc = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false); var doc = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false);
return ReleaseResult.Parse(doc); return ReleaseResult.Parse(doc, 1).FirstOrDefault();
} }
private async Task<ReleaseResult> GetReleaseResultByArtistName(string albumName, string artistName, CancellationToken cancellationToken) private async Task<ReleaseResult> GetReleaseResultByArtistName(string albumName, string artistName, CancellationToken cancellationToken)
@ -219,32 +197,32 @@ namespace MediaBrowser.Providers.Music
var doc = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false); var doc = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false);
return ReleaseResult.Parse(doc); return ReleaseResult.Parse(doc, 1).FirstOrDefault();
} }
private class ReleaseResult private class ReleaseResult
{ {
public string ReleaseId; public string ReleaseId;
public string ReleaseGroupId; public string ReleaseGroupId;
public string Title;
public static ReleaseResult Parse(XmlDocument doc) public static List<ReleaseResult> Parse(XmlDocument doc, int? limit = null)
{ {
var docElem = doc.DocumentElement; var docElem = doc.DocumentElement;
var list = new List<ReleaseResult>();
if (docElem == null) if (docElem == null)
{ {
return new ReleaseResult(); return list;
} }
var releaseList = docElem.FirstChild; var releaseList = docElem.FirstChild;
if (releaseList == null) if (releaseList == null)
{ {
return new ReleaseResult(); return list;
} }
var nodes = releaseList.ChildNodes; var nodes = releaseList.ChildNodes;
string releaseId = null;
string releaseGroupId = null;
if (nodes != null) if (nodes != null)
{ {
@ -252,18 +230,42 @@ namespace MediaBrowser.Providers.Music
{ {
if (string.Equals(node.Name, "release", StringComparison.OrdinalIgnoreCase)) if (string.Equals(node.Name, "release", StringComparison.OrdinalIgnoreCase))
{ {
releaseId = node.Attributes["id"].Value; var releaseId = node.Attributes["id"].Value;
releaseGroupId = GetReleaseGroupIdFromReleaseNode(node); var releaseGroupId = GetReleaseGroupIdFromReleaseNode(node);
break;
list.Add(new ReleaseResult
{
ReleaseId = releaseId,
ReleaseGroupId = releaseGroupId,
Title = GetTitleFromReleaseNode(node)
});
if (limit.HasValue && list.Count >= limit.Value)
{
break;
}
} }
} }
} }
return new ReleaseResult return list;
}
private static string GetTitleFromReleaseNode(XmlNode node)
{
var subNodes = node.ChildNodes;
if (subNodes != null)
{ {
ReleaseId = releaseId, foreach (var subNode in subNodes.Cast<XmlNode>())
ReleaseGroupId = releaseGroupId {
}; if (string.Equals(subNode.Name, "title", StringComparison.OrdinalIgnoreCase))
{
return subNode.InnerText;
}
}
}
return null;
} }
private static string GetReleaseGroupIdFromReleaseNode(XmlNode node) private static string GetReleaseGroupIdFromReleaseNode(XmlNode node)