ArtistProvider.cs 11 KB

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