AudioDbArtistProvider.cs 8.5 KB

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