AudioDbArtistProvider.cs 8.5 KB

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