ArtistProvider.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. #pragma warning disable CS1591
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Http;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Xml;
  13. using MediaBrowser.Controller.Entities.Audio;
  14. using MediaBrowser.Controller.Extensions;
  15. using MediaBrowser.Controller.Providers;
  16. using MediaBrowser.Model.Entities;
  17. using MediaBrowser.Model.Providers;
  18. using MediaBrowser.Providers.Plugins.MusicBrainz;
  19. namespace MediaBrowser.Providers.Music
  20. {
  21. public class MusicBrainzArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>
  22. {
  23. /// <inheritdoc />
  24. public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
  25. {
  26. var musicBrainzId = searchInfo.GetMusicBrainzArtistId();
  27. if (!string.IsNullOrWhiteSpace(musicBrainzId))
  28. {
  29. var url = "/ws/2/artist/?query=arid:{0}" + musicBrainzId.ToString(CultureInfo.InvariantCulture);
  30. using var response = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
  31. await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
  32. return GetResultsFromResponse(stream);
  33. }
  34. else
  35. {
  36. // They seem to throw bad request failures on any term with a slash
  37. var nameToSearch = searchInfo.Name.Replace('/', ' ');
  38. var url = string.Format(CultureInfo.InvariantCulture, "/ws/2/artist/?query=\"{0}\"&dismax=true", UrlEncode(nameToSearch));
  39. using (var response = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false))
  40. await using (var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false))
  41. {
  42. var results = GetResultsFromResponse(stream).ToList();
  43. if (results.Count > 0)
  44. {
  45. return results;
  46. }
  47. }
  48. if (HasDiacritics(searchInfo.Name))
  49. {
  50. // Try again using the search with accent characters url
  51. url = string.Format(CultureInfo.InvariantCulture, "/ws/2/artist/?query=artistaccent:\"{0}\"", UrlEncode(nameToSearch));
  52. using var response = await MusicBrainzAlbumProvider.Current.GetMusicBrainzResponse(url, cancellationToken).ConfigureAwait(false);
  53. await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false);
  54. return GetResultsFromResponse(stream);
  55. }
  56. }
  57. return Enumerable.Empty<RemoteSearchResult>();
  58. }
  59. private IEnumerable<RemoteSearchResult> GetResultsFromResponse(Stream stream)
  60. {
  61. using (var oReader = new StreamReader(stream, Encoding.UTF8))
  62. {
  63. var settings = new XmlReaderSettings()
  64. {
  65. ValidationType = ValidationType.None,
  66. CheckCharacters = false,
  67. IgnoreProcessingInstructions = true,
  68. IgnoreComments = true
  69. };
  70. using (var reader = XmlReader.Create(oReader, settings))
  71. {
  72. reader.MoveToContent();
  73. reader.Read();
  74. // Loop through each element
  75. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  76. {
  77. if (reader.NodeType == XmlNodeType.Element)
  78. {
  79. switch (reader.Name)
  80. {
  81. case "artist-list":
  82. {
  83. if (reader.IsEmptyElement)
  84. {
  85. reader.Read();
  86. continue;
  87. }
  88. using (var subReader = reader.ReadSubtree())
  89. {
  90. return ParseArtistList(subReader).ToList();
  91. }
  92. }
  93. default:
  94. {
  95. reader.Skip();
  96. break;
  97. }
  98. }
  99. }
  100. else
  101. {
  102. reader.Read();
  103. }
  104. }
  105. return Enumerable.Empty<RemoteSearchResult>();
  106. }
  107. }
  108. }
  109. private IEnumerable<RemoteSearchResult> ParseArtistList(XmlReader reader)
  110. {
  111. reader.MoveToContent();
  112. reader.Read();
  113. // Loop through each element
  114. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  115. {
  116. if (reader.NodeType == XmlNodeType.Element)
  117. {
  118. switch (reader.Name)
  119. {
  120. case "artist":
  121. {
  122. if (reader.IsEmptyElement)
  123. {
  124. reader.Read();
  125. continue;
  126. }
  127. var mbzId = reader.GetAttribute("id");
  128. using (var subReader = reader.ReadSubtree())
  129. {
  130. var artist = ParseArtist(subReader, mbzId);
  131. if (artist != null)
  132. {
  133. yield return artist;
  134. }
  135. }
  136. break;
  137. }
  138. default:
  139. {
  140. reader.Skip();
  141. break;
  142. }
  143. }
  144. }
  145. else
  146. {
  147. reader.Read();
  148. }
  149. }
  150. }
  151. private RemoteSearchResult ParseArtist(XmlReader reader, string artistId)
  152. {
  153. var result = new RemoteSearchResult();
  154. reader.MoveToContent();
  155. reader.Read();
  156. // http://stackoverflow.com/questions/2299632/why-does-xmlreader-skip-every-other-element-if-there-is-no-whitespace-separator
  157. // Loop through each element
  158. while (!reader.EOF && reader.ReadState == ReadState.Interactive)
  159. {
  160. if (reader.NodeType == XmlNodeType.Element)
  161. {
  162. switch (reader.Name)
  163. {
  164. case "name":
  165. {
  166. result.Name = reader.ReadElementContentAsString();
  167. break;
  168. }
  169. case "annotation":
  170. {
  171. result.Overview = reader.ReadElementContentAsString();
  172. break;
  173. }
  174. default:
  175. {
  176. // there is sort-name if ever needed
  177. reader.Skip();
  178. break;
  179. }
  180. }
  181. }
  182. else
  183. {
  184. reader.Read();
  185. }
  186. }
  187. result.SetProviderId(MetadataProvider.MusicBrainzArtist, artistId);
  188. if (string.IsNullOrWhiteSpace(artistId) || string.IsNullOrWhiteSpace(result.Name))
  189. {
  190. return null;
  191. }
  192. return result;
  193. }
  194. public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo id, CancellationToken cancellationToken)
  195. {
  196. var result = new MetadataResult<MusicArtist>
  197. {
  198. Item = new MusicArtist()
  199. };
  200. var musicBrainzId = id.GetMusicBrainzArtistId();
  201. if (string.IsNullOrWhiteSpace(musicBrainzId))
  202. {
  203. var searchResults = await GetSearchResults(id, cancellationToken).ConfigureAwait(false);
  204. var singleResult = searchResults.FirstOrDefault();
  205. if (singleResult != null)
  206. {
  207. musicBrainzId = singleResult.GetProviderId(MetadataProvider.MusicBrainzArtist);
  208. result.Item.Overview = singleResult.Overview;
  209. if (Plugin.Instance.Configuration.ReplaceArtistName)
  210. {
  211. result.Item.Name = singleResult.Name;
  212. }
  213. }
  214. }
  215. if (!string.IsNullOrWhiteSpace(musicBrainzId))
  216. {
  217. result.HasMetadata = true;
  218. result.Item.SetProviderId(MetadataProvider.MusicBrainzArtist, musicBrainzId);
  219. }
  220. return result;
  221. }
  222. /// <summary>
  223. /// Determines whether the specified text has diacritics.
  224. /// </summary>
  225. /// <param name="text">The text.</param>
  226. /// <returns><c>true</c> if the specified text has diacritics; otherwise, <c>false</c>.</returns>
  227. private bool HasDiacritics(string text)
  228. {
  229. return !string.Equals(text, text.RemoveDiacritics(), StringComparison.Ordinal);
  230. }
  231. /// <summary>
  232. /// Encodes an URL.
  233. /// </summary>
  234. /// <param name="name">The name.</param>
  235. /// <returns>System.String.</returns>
  236. private static string UrlEncode(string name)
  237. {
  238. return WebUtility.UrlEncode(name);
  239. }
  240. public string Name => "MusicBrainz";
  241. public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
  242. {
  243. throw new NotImplementedException();
  244. }
  245. }
  246. }