AudioDbArtistProvider.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.IO;
  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.Serialization;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. namespace MediaBrowser.Providers.Music
  15. {
  16. public class AudioDbArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>, IHasOrder
  17. {
  18. private readonly IServerConfigurationManager _config;
  19. private readonly IFileSystem _fileSystem;
  20. private readonly IHttpClient _httpClient;
  21. private readonly IJsonSerializer _json;
  22. public static AudioDbArtistProvider Current;
  23. public SemaphoreSlim AudioDbResourcePool = new SemaphoreSlim(2, 2);
  24. private const string ApiKey = "49jhsf8248yfahka89724011";
  25. public const string BaseUrl = "http://www.theaudiodb.com/api/v1/json/" + ApiKey;
  26. public AudioDbArtistProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClient httpClient, IJsonSerializer json)
  27. {
  28. _config = config;
  29. _fileSystem = fileSystem;
  30. _httpClient = httpClient;
  31. _json = json;
  32. Current = this;
  33. }
  34. public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken)
  35. {
  36. var result = new MetadataResult<MusicArtist>();
  37. var id = info.GetMusicBrainzArtistId();
  38. if (!string.IsNullOrWhiteSpace(id))
  39. {
  40. await EnsureArtistInfo(id, cancellationToken).ConfigureAwait(false);
  41. var path = GetArtistInfoPath(_config.ApplicationPaths, id);
  42. var obj = _json.DeserializeFromFile<RootObject>(path);
  43. if (obj != null && obj.artists != null && obj.artists.Count > 0)
  44. {
  45. result.Item = new MusicArtist();
  46. result.HasMetadata = true;
  47. ProcessResult(result.Item, obj.artists[0]);
  48. }
  49. }
  50. return result;
  51. }
  52. private void ProcessResult(MusicArtist item, Artist result)
  53. {
  54. item.HomePageUrl = result.strWebsite;
  55. item.Overview = result.strBiographyEN;
  56. if (!string.IsNullOrEmpty(result.strGenre))
  57. {
  58. item.Genres = new List<string> { result.strGenre };
  59. }
  60. item.SetProviderId(MetadataProviders.AudioDbArtist, result.idArtist);
  61. item.SetProviderId(MetadataProviders.MusicBrainzArtist, result.strMusicBrainzID);
  62. }
  63. public string Name
  64. {
  65. get { return "TheAudioDB"; }
  66. }
  67. private readonly Task _cachedTask = Task.FromResult(true);
  68. internal Task EnsureArtistInfo(string musicBrainzId, CancellationToken cancellationToken)
  69. {
  70. var xmlPath = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId);
  71. var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
  72. if (fileInfo.Exists)
  73. {
  74. if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 7)
  75. {
  76. return _cachedTask;
  77. }
  78. }
  79. return DownloadArtistInfo(musicBrainzId, cancellationToken);
  80. }
  81. internal async Task DownloadArtistInfo(string musicBrainzId, CancellationToken cancellationToken)
  82. {
  83. cancellationToken.ThrowIfCancellationRequested();
  84. var url = BaseUrl + "/artist-mb.php?i=" + musicBrainzId;
  85. var path = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId);
  86. Directory.CreateDirectory(Path.GetDirectoryName(path));
  87. using (var response = await _httpClient.Get(new HttpRequestOptions
  88. {
  89. Url = url,
  90. ResourcePool = AudioDbResourcePool,
  91. CancellationToken = cancellationToken
  92. }).ConfigureAwait(false))
  93. {
  94. using (var xmlFileStream = _fileSystem.GetFileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, true))
  95. {
  96. await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
  97. }
  98. }
  99. }
  100. /// <summary>
  101. /// Gets the artist data path.
  102. /// </summary>
  103. /// <param name="appPaths">The application paths.</param>
  104. /// <param name="musicBrainzArtistId">The music brainz artist identifier.</param>
  105. /// <returns>System.String.</returns>
  106. private static string GetArtistDataPath(IApplicationPaths appPaths, string musicBrainzArtistId)
  107. {
  108. var dataPath = Path.Combine(GetArtistDataPath(appPaths), musicBrainzArtistId);
  109. return dataPath;
  110. }
  111. /// <summary>
  112. /// Gets the artist data path.
  113. /// </summary>
  114. /// <param name="appPaths">The application paths.</param>
  115. /// <returns>System.String.</returns>
  116. private static string GetArtistDataPath(IApplicationPaths appPaths)
  117. {
  118. var dataPath = Path.Combine(appPaths.CachePath, "audiodb-artist");
  119. return dataPath;
  120. }
  121. internal static string GetArtistInfoPath(IApplicationPaths appPaths, string musicBrainzArtistId)
  122. {
  123. var dataPath = GetArtistDataPath(appPaths, musicBrainzArtistId);
  124. return Path.Combine(dataPath, "artist.json");
  125. }
  126. public class Artist
  127. {
  128. public string idArtist { get; set; }
  129. public string strArtist { get; set; }
  130. public string strArtistAlternate { get; set; }
  131. public object idLabel { get; set; }
  132. public string intFormedYear { get; set; }
  133. public string intBornYear { get; set; }
  134. public object intDiedYear { get; set; }
  135. public object strDisbanded { get; set; }
  136. public string strGenre { get; set; }
  137. public string strSubGenre { get; set; }
  138. public string strWebsite { get; set; }
  139. public string strFacebook { get; set; }
  140. public string strTwitter { get; set; }
  141. public string strBiographyEN { get; set; }
  142. public string strBiographyDE { get; set; }
  143. public string strBiographyFR { get; set; }
  144. public object strBiographyCN { get; set; }
  145. public string strBiographyIT { get; set; }
  146. public object strBiographyJP { get; set; }
  147. public object strBiographyRU { get; set; }
  148. public object strBiographyES { get; set; }
  149. public object strBiographyPT { get; set; }
  150. public object strBiographySE { get; set; }
  151. public object strBiographyNL { get; set; }
  152. public object strBiographyHU { get; set; }
  153. public object strBiographyNO { get; set; }
  154. public object strBiographyIL { get; set; }
  155. public object strBiographyPL { get; set; }
  156. public string strGender { get; set; }
  157. public string intMembers { get; set; }
  158. public string strCountry { get; set; }
  159. public string strCountryCode { get; set; }
  160. public string strArtistThumb { get; set; }
  161. public string strArtistLogo { get; set; }
  162. public string strArtistFanart { get; set; }
  163. public string strArtistFanart2 { get; set; }
  164. public string strArtistFanart3 { get; set; }
  165. public string strArtistBanner { get; set; }
  166. public string strMusicBrainzID { get; set; }
  167. public object strLastFMChart { get; set; }
  168. public string strLocked { get; set; }
  169. }
  170. public class RootObject
  171. {
  172. public List<Artist> artists { get; set; }
  173. }
  174. public int Order
  175. {
  176. get
  177. {
  178. // After musicbrainz
  179. return 1;
  180. }
  181. }
  182. }
  183. }