ArtistProvider.cs 9.8 KB

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