ArtistProvider.cs 9.6 KB

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