AudioDbArtistProvider.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Controller.Configuration;
  5. using MediaBrowser.Controller.Entities.Audio;
  6. using MediaBrowser.Controller.Providers;
  7. using MediaBrowser.Model.Entities;
  8. using MediaBrowser.Model.Providers;
  9. using MediaBrowser.Model.Serialization;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using MediaBrowser.Controller.IO;
  16. using MediaBrowser.Model.IO;
  17. namespace MediaBrowser.Providers.Music
  18. {
  19. public class AudioDbArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>, IHasOrder
  20. {
  21. private readonly IServerConfigurationManager _config;
  22. private readonly IFileSystem _fileSystem;
  23. private readonly IHttpClient _httpClient;
  24. private readonly IJsonSerializer _json;
  25. public static AudioDbArtistProvider Current;
  26. private const string ApiKey = "195003";
  27. public const string BaseUrl = "https://www.theaudiodb.com/api/v1/json/" + ApiKey;
  28. public AudioDbArtistProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClient httpClient, IJsonSerializer json)
  29. {
  30. _config = config;
  31. _fileSystem = fileSystem;
  32. _httpClient = httpClient;
  33. _json = json;
  34. Current = this;
  35. }
  36. public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
  37. {
  38. return Task.FromResult((IEnumerable<RemoteSearchResult>)new List<RemoteSearchResult>());
  39. }
  40. public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken)
  41. {
  42. var result = new MetadataResult<MusicArtist>();
  43. var id = info.GetMusicBrainzArtistId();
  44. if (!string.IsNullOrWhiteSpace(id))
  45. {
  46. await EnsureArtistInfo(id, cancellationToken).ConfigureAwait(false);
  47. var path = GetArtistInfoPath(_config.ApplicationPaths, id);
  48. var obj = _json.DeserializeFromFile<RootObject>(path);
  49. if (obj != null && obj.artists != null && obj.artists.Count > 0)
  50. {
  51. result.Item = new MusicArtist();
  52. result.HasMetadata = true;
  53. ProcessResult(result.Item, obj.artists[0], info.MetadataLanguage);
  54. }
  55. }
  56. return result;
  57. }
  58. private void ProcessResult(MusicArtist item, Artist result, string preferredLanguage)
  59. {
  60. //item.HomePageUrl = result.strWebsite;
  61. if (!string.IsNullOrEmpty(result.strGenre))
  62. {
  63. item.Genres = new[] { result.strGenre };
  64. }
  65. item.SetProviderId(MetadataProviders.AudioDbArtist, result.idArtist);
  66. item.SetProviderId(MetadataProviders.MusicBrainzArtist, result.strMusicBrainzID);
  67. string overview = null;
  68. if (string.Equals(preferredLanguage, "de", StringComparison.OrdinalIgnoreCase))
  69. {
  70. overview = result.strBiographyDE;
  71. }
  72. else if (string.Equals(preferredLanguage, "fr", StringComparison.OrdinalIgnoreCase))
  73. {
  74. overview = result.strBiographyFR;
  75. }
  76. else if (string.Equals(preferredLanguage, "nl", StringComparison.OrdinalIgnoreCase))
  77. {
  78. overview = result.strBiographyNL;
  79. }
  80. else if (string.Equals(preferredLanguage, "ru", StringComparison.OrdinalIgnoreCase))
  81. {
  82. overview = result.strBiographyRU;
  83. }
  84. else if (string.Equals(preferredLanguage, "it", StringComparison.OrdinalIgnoreCase))
  85. {
  86. overview = result.strBiographyIT;
  87. }
  88. else if ((preferredLanguage ?? string.Empty).StartsWith("pt", StringComparison.OrdinalIgnoreCase))
  89. {
  90. overview = result.strBiographyPT;
  91. }
  92. if (string.IsNullOrWhiteSpace(overview))
  93. {
  94. overview = result.strBiographyEN;
  95. }
  96. item.Overview = (overview ?? string.Empty).StripHtml();
  97. }
  98. public string Name
  99. {
  100. get { return "TheAudioDB"; }
  101. }
  102. internal Task EnsureArtistInfo(string musicBrainzId, CancellationToken cancellationToken)
  103. {
  104. var xmlPath = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId);
  105. var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
  106. if (fileInfo.Exists)
  107. {
  108. if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
  109. {
  110. return Task.CompletedTask;
  111. }
  112. }
  113. return DownloadArtistInfo(musicBrainzId, cancellationToken);
  114. }
  115. internal async Task DownloadArtistInfo(string musicBrainzId, CancellationToken cancellationToken)
  116. {
  117. cancellationToken.ThrowIfCancellationRequested();
  118. var url = BaseUrl + "/artist-mb.php?i=" + musicBrainzId;
  119. var path = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId);
  120. using (var httpResponse = await _httpClient.SendAsync(new HttpRequestOptions
  121. {
  122. Url = url,
  123. CancellationToken = cancellationToken,
  124. BufferContent = true
  125. }, "GET").ConfigureAwait(false))
  126. {
  127. using (var response = httpResponse.Content)
  128. {
  129. _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path));
  130. using (var xmlFileStream = _fileSystem.GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true))
  131. {
  132. await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
  133. }
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// Gets the artist data path.
  139. /// </summary>
  140. /// <param name="appPaths">The application paths.</param>
  141. /// <param name="musicBrainzArtistId">The music brainz artist identifier.</param>
  142. /// <returns>System.String.</returns>
  143. private static string GetArtistDataPath(IApplicationPaths appPaths, string musicBrainzArtistId)
  144. {
  145. var dataPath = Path.Combine(GetArtistDataPath(appPaths), musicBrainzArtistId);
  146. return dataPath;
  147. }
  148. /// <summary>
  149. /// Gets the artist data path.
  150. /// </summary>
  151. /// <param name="appPaths">The application paths.</param>
  152. /// <returns>System.String.</returns>
  153. private static string GetArtistDataPath(IApplicationPaths appPaths)
  154. {
  155. var dataPath = Path.Combine(appPaths.CachePath, "audiodb-artist");
  156. return dataPath;
  157. }
  158. internal static string GetArtistInfoPath(IApplicationPaths appPaths, string musicBrainzArtistId)
  159. {
  160. var dataPath = GetArtistDataPath(appPaths, musicBrainzArtistId);
  161. return Path.Combine(dataPath, "artist.json");
  162. }
  163. public class Artist
  164. {
  165. public string idArtist { get; set; }
  166. public string strArtist { get; set; }
  167. public string strArtistAlternate { get; set; }
  168. public object idLabel { get; set; }
  169. public string intFormedYear { get; set; }
  170. public string intBornYear { get; set; }
  171. public object intDiedYear { get; set; }
  172. public object strDisbanded { get; set; }
  173. public string strGenre { get; set; }
  174. public string strSubGenre { get; set; }
  175. public string strWebsite { get; set; }
  176. public string strFacebook { get; set; }
  177. public string strTwitter { get; set; }
  178. public string strBiographyEN { get; set; }
  179. public string strBiographyDE { get; set; }
  180. public string strBiographyFR { get; set; }
  181. public string strBiographyCN { get; set; }
  182. public string strBiographyIT { get; set; }
  183. public string strBiographyJP { get; set; }
  184. public string strBiographyRU { get; set; }
  185. public string strBiographyES { get; set; }
  186. public string strBiographyPT { get; set; }
  187. public string strBiographySE { get; set; }
  188. public string strBiographyNL { get; set; }
  189. public string strBiographyHU { get; set; }
  190. public string strBiographyNO { get; set; }
  191. public string strBiographyIL { get; set; }
  192. public string strBiographyPL { get; set; }
  193. public string strGender { get; set; }
  194. public string intMembers { get; set; }
  195. public string strCountry { get; set; }
  196. public string strCountryCode { get; set; }
  197. public string strArtistThumb { get; set; }
  198. public string strArtistLogo { get; set; }
  199. public string strArtistFanart { get; set; }
  200. public string strArtistFanart2 { get; set; }
  201. public string strArtistFanart3 { get; set; }
  202. public string strArtistBanner { get; set; }
  203. public string strMusicBrainzID { get; set; }
  204. public object strLastFMChart { get; set; }
  205. public string strLocked { get; set; }
  206. }
  207. public class RootObject
  208. {
  209. public List<Artist> artists { get; set; }
  210. }
  211. public int Order
  212. {
  213. get
  214. {
  215. // After musicbrainz
  216. return 1;
  217. }
  218. }
  219. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  220. {
  221. throw new NotImplementedException();
  222. }
  223. }
  224. }