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