ArtistProvider.cs 11 KB

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