AudioDbArtistImageProvider.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #nullable disable
  2. #pragma warning disable CS1591
  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 Jellyfin.Extensions.Json;
  11. using MediaBrowser.Common.Net;
  12. using MediaBrowser.Controller.Configuration;
  13. using MediaBrowser.Controller.Entities;
  14. using MediaBrowser.Controller.Entities.Audio;
  15. using MediaBrowser.Controller.Providers;
  16. using MediaBrowser.Model.Entities;
  17. using MediaBrowser.Model.IO;
  18. using MediaBrowser.Model.Providers;
  19. namespace MediaBrowser.Providers.Plugins.AudioDb
  20. {
  21. public class AudioDbArtistImageProvider : IRemoteImageProvider, IHasOrder
  22. {
  23. private readonly IServerConfigurationManager _config;
  24. private readonly IHttpClientFactory _httpClientFactory;
  25. private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
  26. public AudioDbArtistImageProvider(IServerConfigurationManager config, IHttpClientFactory httpClientFactory)
  27. {
  28. _config = config;
  29. _httpClientFactory = httpClientFactory;
  30. }
  31. /// <inheritdoc />
  32. public string Name => "TheAudioDB";
  33. /// <inheritdoc />
  34. // After fanart
  35. public int Order => 1;
  36. /// <inheritdoc />
  37. public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
  38. {
  39. return new ImageType[]
  40. {
  41. ImageType.Primary,
  42. ImageType.Logo,
  43. ImageType.Banner,
  44. ImageType.Backdrop
  45. };
  46. }
  47. /// <inheritdoc />
  48. public async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
  49. {
  50. var id = item.GetProviderId(MetadataProvider.MusicBrainzArtist);
  51. if (!string.IsNullOrWhiteSpace(id))
  52. {
  53. await AudioDbArtistProvider.Current.EnsureArtistInfo(id, cancellationToken).ConfigureAwait(false);
  54. var path = AudioDbArtistProvider.GetArtistInfoPath(_config.ApplicationPaths, id);
  55. FileStream jsonStream = AsyncFile.OpenRead(path);
  56. await using (jsonStream.ConfigureAwait(false))
  57. {
  58. var obj = await JsonSerializer.DeserializeAsync<AudioDbArtistProvider.RootObject>(jsonStream, _jsonOptions, cancellationToken).ConfigureAwait(false);
  59. if (obj is not null && obj.artists is not null && obj.artists.Count > 0)
  60. {
  61. return GetImages(obj.artists[0]);
  62. }
  63. }
  64. }
  65. return Enumerable.Empty<RemoteImageInfo>();
  66. }
  67. private IEnumerable<RemoteImageInfo> GetImages(AudioDbArtistProvider.Artist item)
  68. {
  69. var list = new List<RemoteImageInfo>();
  70. if (!string.IsNullOrWhiteSpace(item.strArtistThumb))
  71. {
  72. list.Add(new RemoteImageInfo
  73. {
  74. ProviderName = Name,
  75. Url = item.strArtistThumb,
  76. Type = ImageType.Primary
  77. });
  78. }
  79. if (!string.IsNullOrWhiteSpace(item.strArtistLogo))
  80. {
  81. list.Add(new RemoteImageInfo
  82. {
  83. ProviderName = Name,
  84. Url = item.strArtistLogo,
  85. Type = ImageType.Logo
  86. });
  87. }
  88. if (!string.IsNullOrWhiteSpace(item.strArtistBanner))
  89. {
  90. list.Add(new RemoteImageInfo
  91. {
  92. ProviderName = Name,
  93. Url = item.strArtistBanner,
  94. Type = ImageType.Banner
  95. });
  96. }
  97. if (!string.IsNullOrWhiteSpace(item.strArtistFanart))
  98. {
  99. list.Add(new RemoteImageInfo
  100. {
  101. ProviderName = Name,
  102. Url = item.strArtistFanart,
  103. Type = ImageType.Backdrop
  104. });
  105. }
  106. if (!string.IsNullOrWhiteSpace(item.strArtistFanart2))
  107. {
  108. list.Add(new RemoteImageInfo
  109. {
  110. ProviderName = Name,
  111. Url = item.strArtistFanart2,
  112. Type = ImageType.Backdrop
  113. });
  114. }
  115. if (!string.IsNullOrWhiteSpace(item.strArtistFanart3))
  116. {
  117. list.Add(new RemoteImageInfo
  118. {
  119. ProviderName = Name,
  120. Url = item.strArtistFanart3,
  121. Type = ImageType.Backdrop
  122. });
  123. }
  124. return list;
  125. }
  126. public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
  127. {
  128. var httpClient = _httpClientFactory.CreateClient(NamedClient.Default);
  129. return httpClient.GetAsync(url, cancellationToken);
  130. }
  131. /// <inheritdoc />
  132. public bool Supports(BaseItem item)
  133. => item is MusicArtist;
  134. }
  135. }