From 1d37f9ad0e8281a612580c438f0130c5796ff8c4 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 16 Aug 2016 14:45:57 -0400 Subject: [PATCH] fix music brainz album search results --- .../Music/MusicBrainzAlbumProvider.cs | 100 +++++++++--------- 1 file changed, 51 insertions(+), 49 deletions(-) diff --git a/MediaBrowser.Providers/Music/MusicBrainzAlbumProvider.cs b/MediaBrowser.Providers/Music/MusicBrainzAlbumProvider.cs index 1710ec2b0f..a0ce806105 100644 --- a/MediaBrowser.Providers/Music/MusicBrainzAlbumProvider.cs +++ b/MediaBrowser.Providers/Music/MusicBrainzAlbumProvider.cs @@ -81,46 +81,24 @@ namespace MediaBrowser.Providers.Music private IEnumerable GetResultsFromResponse(XmlDocument doc) { - var ns = new XmlNamespaceManager(doc.NameTable); - ns.AddNamespace("mb", MusicBrainzBaseUrl + "/ns/mmd-2.0#"); - - var list = new List(); - - var nodes = doc.SelectNodes("//mb:release-list/mb:release", ns); - - if (nodes != null) + return ReleaseResult.Parse(doc).Select(i => { - foreach (var node in nodes.Cast()) + var result = new RemoteSearchResult { - if (node.Attributes != null) - { - string name = null; - - string mbzId = node.Attributes["id"].Value; - - var nameNode = node.SelectSingleNode("//mb:title", ns); - - if (nameNode != null) - { - name = nameNode.InnerText; - } - - if (!string.IsNullOrWhiteSpace(mbzId) && !string.IsNullOrWhiteSpace(name)) - { - var result = new RemoteSearchResult - { - Name = name - }; - - result.SetProviderId(MetadataProviders.MusicBrainzAlbum, mbzId); + Name = i.Title + }; - list.Add(result); - } - } + if (!string.IsNullOrWhiteSpace(i.ReleaseId)) + { + result.SetProviderId(MetadataProviders.MusicBrainzAlbum, i.ReleaseId); + } + if (!string.IsNullOrWhiteSpace(i.ReleaseGroupId)) + { + result.SetProviderId(MetadataProviders.MusicBrainzAlbum, i.ReleaseGroupId); } - } - return list; + return result; + }); } public async Task> GetMetadata(AlbumInfo id, CancellationToken cancellationToken) @@ -208,7 +186,7 @@ namespace MediaBrowser.Providers.Music var doc = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false); - return ReleaseResult.Parse(doc); + return ReleaseResult.Parse(doc, 1).FirstOrDefault(); } private async Task GetReleaseResultByArtistName(string albumName, string artistName, CancellationToken cancellationToken) @@ -219,32 +197,32 @@ namespace MediaBrowser.Providers.Music var doc = await GetMusicBrainzResponse(url, true, cancellationToken).ConfigureAwait(false); - return ReleaseResult.Parse(doc); + return ReleaseResult.Parse(doc, 1).FirstOrDefault(); } private class ReleaseResult { public string ReleaseId; public string ReleaseGroupId; + public string Title; - public static ReleaseResult Parse(XmlDocument doc) + public static List Parse(XmlDocument doc, int? limit = null) { var docElem = doc.DocumentElement; + var list = new List(); if (docElem == null) { - return new ReleaseResult(); + return list; } var releaseList = docElem.FirstChild; if (releaseList == null) { - return new ReleaseResult(); + return list; } var nodes = releaseList.ChildNodes; - string releaseId = null; - string releaseGroupId = null; if (nodes != null) { @@ -252,18 +230,42 @@ namespace MediaBrowser.Providers.Music { if (string.Equals(node.Name, "release", StringComparison.OrdinalIgnoreCase)) { - releaseId = node.Attributes["id"].Value; - releaseGroupId = GetReleaseGroupIdFromReleaseNode(node); - break; + var releaseId = node.Attributes["id"].Value; + var releaseGroupId = GetReleaseGroupIdFromReleaseNode(node); + + 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, - ReleaseGroupId = releaseGroupId - }; + foreach (var subNode in subNodes.Cast()) + { + if (string.Equals(subNode.Name, "title", StringComparison.OrdinalIgnoreCase)) + { + return subNode.InnerText; + } + } + } + + return null; } private static string GetReleaseGroupIdFromReleaseNode(XmlNode node)