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