AudioDbArtistProvider.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net.Http;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Common.Configuration;
  9. using MediaBrowser.Common.Extensions;
  10. using MediaBrowser.Common.Net;
  11. using MediaBrowser.Controller.Configuration;
  12. using MediaBrowser.Controller.Entities.Audio;
  13. using MediaBrowser.Controller.Providers;
  14. using MediaBrowser.Model.Entities;
  15. using MediaBrowser.Model.IO;
  16. using MediaBrowser.Model.Providers;
  17. using MediaBrowser.Model.Serialization;
  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. private const string ApiKey = "195003";
  28. public const string BaseUrl = "https://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. /// <inheritdoc />
  38. public string Name => "TheAudioDB";
  39. /// <inheritdoc />
  40. // After musicbrainz
  41. public int Order => 1;
  42. /// <inheritdoc />
  43. public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
  44. => Task.FromResult(Enumerable.Empty<RemoteSearchResult>());
  45. /// <inheritdoc />
  46. public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken)
  47. {
  48. var result = new MetadataResult<MusicArtist>();
  49. var id = info.GetMusicBrainzArtistId();
  50. if (!string.IsNullOrWhiteSpace(id))
  51. {
  52. await EnsureArtistInfo(id, cancellationToken).ConfigureAwait(false);
  53. var path = GetArtistInfoPath(_config.ApplicationPaths, id);
  54. var obj = _json.DeserializeFromFile<RootObject>(path);
  55. if (obj != null && obj.artists != null && obj.artists.Count > 0)
  56. {
  57. result.Item = new MusicArtist();
  58. result.HasMetadata = true;
  59. ProcessResult(result.Item, obj.artists[0], info.MetadataLanguage);
  60. }
  61. }
  62. return result;
  63. }
  64. private void ProcessResult(MusicArtist item, Artist result, string preferredLanguage)
  65. {
  66. //item.HomePageUrl = result.strWebsite;
  67. if (!string.IsNullOrEmpty(result.strGenre))
  68. {
  69. item.Genres = new[] { result.strGenre };
  70. }
  71. item.SetProviderId(MetadataProviders.AudioDbArtist, result.idArtist);
  72. item.SetProviderId(MetadataProviders.MusicBrainzArtist, result.strMusicBrainzID);
  73. string overview = null;
  74. if (string.Equals(preferredLanguage, "de", StringComparison.OrdinalIgnoreCase))
  75. {
  76. overview = result.strBiographyDE;
  77. }
  78. else if (string.Equals(preferredLanguage, "fr", StringComparison.OrdinalIgnoreCase))
  79. {
  80. overview = result.strBiographyFR;
  81. }
  82. else if (string.Equals(preferredLanguage, "nl", StringComparison.OrdinalIgnoreCase))
  83. {
  84. overview = result.strBiographyNL;
  85. }
  86. else if (string.Equals(preferredLanguage, "ru", StringComparison.OrdinalIgnoreCase))
  87. {
  88. overview = result.strBiographyRU;
  89. }
  90. else if (string.Equals(preferredLanguage, "it", StringComparison.OrdinalIgnoreCase))
  91. {
  92. overview = result.strBiographyIT;
  93. }
  94. else if ((preferredLanguage ?? string.Empty).StartsWith("pt", StringComparison.OrdinalIgnoreCase))
  95. {
  96. overview = result.strBiographyPT;
  97. }
  98. if (string.IsNullOrWhiteSpace(overview))
  99. {
  100. overview = result.strBiographyEN;
  101. }
  102. item.Overview = (overview ?? string.Empty).StripHtml();
  103. }
  104. internal Task EnsureArtistInfo(string musicBrainzId, CancellationToken cancellationToken)
  105. {
  106. var xmlPath = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId);
  107. var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
  108. if (fileInfo.Exists
  109. && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
  110. {
  111. return Task.CompletedTask;
  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(
  121. new HttpRequestOptions
  122. {
  123. Url = url,
  124. CancellationToken = cancellationToken,
  125. BufferContent = true
  126. },
  127. HttpMethod.Get).ConfigureAwait(false))
  128. using (var response = httpResponse.Content)
  129. {
  130. Directory.CreateDirectory(Path.GetDirectoryName(path));
  131. using (var xmlFileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, IODefaults.FileStreamBufferSize, true))
  132. {
  133. await response.CopyToAsync(xmlFileStream).ConfigureAwait(false);
  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. => Path.Combine(GetArtistDataPath(appPaths), musicBrainzArtistId);
  145. /// <summary>
  146. /// Gets the artist data path.
  147. /// </summary>
  148. /// <param name="appPaths">The application paths.</param>
  149. /// <returns>System.String.</returns>
  150. private static string GetArtistDataPath(IApplicationPaths appPaths)
  151. => Path.Combine(appPaths.CachePath, "audiodb-artist");
  152. internal static string GetArtistInfoPath(IApplicationPaths appPaths, string musicBrainzArtistId)
  153. {
  154. var dataPath = GetArtistDataPath(appPaths, musicBrainzArtistId);
  155. return Path.Combine(dataPath, "artist.json");
  156. }
  157. public class Artist
  158. {
  159. public string idArtist { get; set; }
  160. public string strArtist { get; set; }
  161. public string strArtistAlternate { get; set; }
  162. public object idLabel { get; set; }
  163. public string intFormedYear { get; set; }
  164. public string intBornYear { get; set; }
  165. public object intDiedYear { get; set; }
  166. public object strDisbanded { get; set; }
  167. public string strGenre { get; set; }
  168. public string strSubGenre { get; set; }
  169. public string strWebsite { get; set; }
  170. public string strFacebook { get; set; }
  171. public string strTwitter { get; set; }
  172. public string strBiographyEN { get; set; }
  173. public string strBiographyDE { get; set; }
  174. public string strBiographyFR { get; set; }
  175. public string strBiographyCN { get; set; }
  176. public string strBiographyIT { get; set; }
  177. public string strBiographyJP { get; set; }
  178. public string strBiographyRU { get; set; }
  179. public string strBiographyES { get; set; }
  180. public string strBiographyPT { get; set; }
  181. public string strBiographySE { get; set; }
  182. public string strBiographyNL { get; set; }
  183. public string strBiographyHU { get; set; }
  184. public string strBiographyNO { get; set; }
  185. public string strBiographyIL { get; set; }
  186. public string strBiographyPL { get; set; }
  187. public string strGender { get; set; }
  188. public string intMembers { get; set; }
  189. public string strCountry { get; set; }
  190. public string strCountryCode { get; set; }
  191. public string strArtistThumb { get; set; }
  192. public string strArtistLogo { get; set; }
  193. public string strArtistFanart { get; set; }
  194. public string strArtistFanart2 { get; set; }
  195. public string strArtistFanart3 { get; set; }
  196. public string strArtistBanner { get; set; }
  197. public string strMusicBrainzID { get; set; }
  198. public object strLastFMChart { get; set; }
  199. public string strLocked { get; set; }
  200. }
  201. public class RootObject
  202. {
  203. public List<Artist> artists { get; set; }
  204. }
  205. /// <inheritdoc />
  206. public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
  207. {
  208. throw new NotImplementedException();
  209. }
  210. }
  211. }