AudioDbArtistProvider.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #nullable disable
  2. #pragma warning disable CA1034, CS1591, CA1002, SA1028, SA1300
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net.Http;
  8. using System.Text.Json;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using Jellyfin.Extensions.Json;
  12. using MediaBrowser.Common.Configuration;
  13. using MediaBrowser.Common.Extensions;
  14. using MediaBrowser.Common.Net;
  15. using MediaBrowser.Controller.Configuration;
  16. using MediaBrowser.Controller.Entities.Audio;
  17. using MediaBrowser.Controller.Providers;
  18. using MediaBrowser.Model.Entities;
  19. using MediaBrowser.Model.IO;
  20. using MediaBrowser.Model.Providers;
  21. using MediaBrowser.Providers.Music;
  22. namespace MediaBrowser.Providers.Plugins.AudioDb
  23. {
  24. public class AudioDbArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>, IHasOrder
  25. {
  26. private const string ApiKey = "195003";
  27. public const string BaseUrl = "https://www.theaudiodb.com/api/v1/json/" + ApiKey;
  28. private readonly IServerConfigurationManager _config;
  29. private readonly IFileSystem _fileSystem;
  30. private readonly IHttpClientFactory _httpClientFactory;
  31. private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
  32. public AudioDbArtistProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClientFactory httpClientFactory)
  33. {
  34. _config = config;
  35. _fileSystem = fileSystem;
  36. _httpClientFactory = httpClientFactory;
  37. Current = this;
  38. }
  39. public static AudioDbArtistProvider Current { get; private set; }
  40. /// <inheritdoc />
  41. public string Name => "TheAudioDB";
  42. /// <inheritdoc />
  43. // After musicbrainz
  44. public int Order => 1;
  45. /// <inheritdoc />
  46. public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken cancellationToken)
  47. => Task.FromResult(Enumerable.Empty<RemoteSearchResult>());
  48. /// <inheritdoc />
  49. public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken)
  50. {
  51. var result = new MetadataResult<MusicArtist>();
  52. var id = info.GetMusicBrainzArtistId();
  53. if (!string.IsNullOrWhiteSpace(id))
  54. {
  55. await EnsureArtistInfo(id, cancellationToken).ConfigureAwait(false);
  56. var path = GetArtistInfoPath(_config.ApplicationPaths, id);
  57. FileStream jsonStream = AsyncFile.OpenRead(path);
  58. await using (jsonStream.ConfigureAwait(false))
  59. {
  60. var obj = await JsonSerializer.DeserializeAsync<RootObject>(jsonStream, _jsonOptions, cancellationToken).ConfigureAwait(false);
  61. if (obj is not null && obj.artists is not null && obj.artists.Count > 0)
  62. {
  63. result.Item = new MusicArtist();
  64. result.HasMetadata = true;
  65. ProcessResult(result.Item, obj.artists[0], info.MetadataLanguage);
  66. }
  67. }
  68. }
  69. return result;
  70. }
  71. private void ProcessResult(MusicArtist item, Artist result, string preferredLanguage)
  72. {
  73. // item.HomePageUrl = result.strWebsite;
  74. if (!string.IsNullOrEmpty(result.strGenre))
  75. {
  76. item.Genres = new[] { result.strGenre };
  77. }
  78. item.SetProviderId(MetadataProvider.AudioDbArtist, result.idArtist);
  79. item.SetProviderId(MetadataProvider.MusicBrainzArtist, result.strMusicBrainzID);
  80. string overview = null;
  81. if (string.Equals(preferredLanguage, "de", StringComparison.OrdinalIgnoreCase))
  82. {
  83. overview = result.strBiographyDE;
  84. }
  85. else if (string.Equals(preferredLanguage, "fr", StringComparison.OrdinalIgnoreCase))
  86. {
  87. overview = result.strBiographyFR;
  88. }
  89. else if (string.Equals(preferredLanguage, "nl", StringComparison.OrdinalIgnoreCase))
  90. {
  91. overview = result.strBiographyNL;
  92. }
  93. else if (string.Equals(preferredLanguage, "ru", StringComparison.OrdinalIgnoreCase))
  94. {
  95. overview = result.strBiographyRU;
  96. }
  97. else if (string.Equals(preferredLanguage, "it", StringComparison.OrdinalIgnoreCase))
  98. {
  99. overview = result.strBiographyIT;
  100. }
  101. else if ((preferredLanguage ?? string.Empty).StartsWith("pt", StringComparison.OrdinalIgnoreCase))
  102. {
  103. overview = result.strBiographyPT;
  104. }
  105. if (string.IsNullOrWhiteSpace(overview))
  106. {
  107. overview = result.strBiographyEN;
  108. }
  109. item.Overview = (overview ?? string.Empty).StripHtml();
  110. }
  111. internal async Task EnsureArtistInfo(string musicBrainzId, CancellationToken cancellationToken)
  112. {
  113. var xmlPath = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId);
  114. var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
  115. if (fileInfo.Exists
  116. && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
  117. {
  118. return;
  119. }
  120. await DownloadArtistInfo(musicBrainzId, cancellationToken).ConfigureAwait(false);
  121. }
  122. internal async Task DownloadArtistInfo(string musicBrainzId, CancellationToken cancellationToken)
  123. {
  124. cancellationToken.ThrowIfCancellationRequested();
  125. var url = BaseUrl + "/artist-mb.php?i=" + musicBrainzId;
  126. using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationToken).ConfigureAwait(false);
  127. response.EnsureSuccessStatusCode();
  128. var path = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId);
  129. Directory.CreateDirectory(Path.GetDirectoryName(path));
  130. var fileStreamOptions = AsyncFile.WriteOptions;
  131. fileStreamOptions.Mode = FileMode.Create;
  132. var xmlFileStream = new FileStream(path, fileStreamOptions);
  133. await using (xmlFileStream.ConfigureAwait(false))
  134. {
  135. await response.Content.CopyToAsync(xmlFileStream, cancellationToken).ConfigureAwait(false);
  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. /// <inheritdoc />
  159. public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
  160. {
  161. throw new NotImplementedException();
  162. }
  163. public class Artist
  164. {
  165. public string idArtist { get; set; }
  166. public string strArtist { get; set; }
  167. public string strArtistAlternate { get; set; }
  168. public object idLabel { get; set; }
  169. public string intFormedYear { get; set; }
  170. public string intBornYear { get; set; }
  171. public object intDiedYear { get; set; }
  172. public object strDisbanded { get; set; }
  173. public string strGenre { get; set; }
  174. public string strSubGenre { get; set; }
  175. public string strWebsite { get; set; }
  176. public string strFacebook { get; set; }
  177. public string strTwitter { get; set; }
  178. public string strBiographyEN { get; set; }
  179. public string strBiographyDE { get; set; }
  180. public string strBiographyFR { get; set; }
  181. public string strBiographyCN { get; set; }
  182. public string strBiographyIT { get; set; }
  183. public string strBiographyJP { get; set; }
  184. public string strBiographyRU { get; set; }
  185. public string strBiographyES { get; set; }
  186. public string strBiographyPT { get; set; }
  187. public string strBiographySE { get; set; }
  188. public string strBiographyNL { get; set; }
  189. public string strBiographyHU { get; set; }
  190. public string strBiographyNO { get; set; }
  191. public string strBiographyIL { get; set; }
  192. public string strBiographyPL { get; set; }
  193. public string strGender { get; set; }
  194. public string intMembers { get; set; }
  195. public string strCountry { get; set; }
  196. public string strCountryCode { get; set; }
  197. public string strArtistThumb { get; set; }
  198. public string strArtistLogo { get; set; }
  199. public string strArtistFanart { get; set; }
  200. public string strArtistFanart2 { get; set; }
  201. public string strArtistFanart3 { get; set; }
  202. public string strArtistBanner { get; set; }
  203. public string strMusicBrainzID { get; set; }
  204. public object strLastFMChart { get; set; }
  205. public string strLocked { get; set; }
  206. }
  207. #pragma warning disable CA2227
  208. public class RootObject
  209. {
  210. public List<Artist> artists { get; set; }
  211. }
  212. }
  213. }