< Summary - Jellyfin

Information
Class: MediaBrowser.Providers.Plugins.AudioDb.AudioDbArtistProvider
Assembly: MediaBrowser.Providers
File(s): /srv/git/jellyfin/MediaBrowser.Providers/Plugins/AudioDb/AudioDbArtistProvider.cs
Line coverage
4%
Covered lines: 6
Uncovered lines: 143
Coverable lines: 149
Total lines: 454
Line coverage: 4%
Branch coverage
0%
Covered branches: 0
Total branches: 123
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 5/16/2026 - 12:15:55 AM Line coverage: 8.5% (6/70) Branch coverage: 0% (0/36) Total lines: 2947/29/2026 - 12:15:53 AM Line coverage: 4.3% (6/138) Branch coverage: 0% (0/76) Total lines: 44610/29/2026 - 12:17:11 AM Line coverage: 4% (6/149) Branch coverage: 0% (0/123) Total lines: 454 5/16/2026 - 12:15:55 AM Line coverage: 8.5% (6/70) Branch coverage: 0% (0/36) Total lines: 2947/29/2026 - 12:15:53 AM Line coverage: 4.3% (6/138) Branch coverage: 0% (0/76) Total lines: 44610/29/2026 - 12:17:11 AM Line coverage: 4% (6/149) Branch coverage: 0% (0/123) Total lines: 454

Coverage delta

Coverage delta 5 -5

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Name()100%210%
get_Order()100%210%
GetSearchResults()0%110100%
FetchArtists()0%2040%
ToRemoteSearchResult(...)0%7280%
GetMetadata()0%620%
GetArtist()0%110100%
ProcessResult(...)0%342180%
GetBiography(...)0%3782610%
EnsureArtistInfo()0%2040%
DownloadArtistInfo()100%210%
EnsureArtistInfoByAudioDbId()0%2040%
DownloadArtistInfo()0%620%
GetArtistDataPath(...)100%210%
GetArtistDataPath(...)100%210%
GetArtistInfoPath(...)100%210%
GetImageResponse(...)100%210%

File(s)

/srv/git/jellyfin/MediaBrowser.Providers/Plugins/AudioDb/AudioDbArtistProvider.cs

#LineLine coverage
 1#nullable disable
 2
 3#pragma warning disable CA1034, CS1591, CA1002, SA1028, SA1300
 4
 5using System;
 6using System.Collections.Generic;
 7using System.Globalization;
 8using System.IO;
 9using System.Linq;
 10using System.Net.Http;
 11using System.Net.Http.Json;
 12using System.Text.Json;
 13using System.Threading;
 14using System.Threading.Tasks;
 15using Jellyfin.Extensions.Json;
 16using MediaBrowser.Common.Configuration;
 17using MediaBrowser.Common.Extensions;
 18using MediaBrowser.Common.Net;
 19using MediaBrowser.Controller.Configuration;
 20using MediaBrowser.Controller.Entities.Audio;
 21using MediaBrowser.Controller.Providers;
 22using MediaBrowser.Model.Entities;
 23using MediaBrowser.Model.IO;
 24using MediaBrowser.Model.Providers;
 25using MediaBrowser.Providers.Manager;
 26using MediaBrowser.Providers.Music;
 27
 28namespace MediaBrowser.Providers.Plugins.AudioDb
 29{
 30    public class AudioDbArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>, IHasOrder
 31    {
 32        private const string ApiKey = "195003";
 33        public const string BaseUrl = "https://www.theaudiodb.com/api/v1/json/" + ApiKey;
 34
 35        private readonly IServerConfigurationManager _config;
 36        private readonly IFileSystem _fileSystem;
 37        private readonly IHttpClientFactory _httpClientFactory;
 2738        private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
 39
 40        public AudioDbArtistProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClientFactory http
 41        {
 2742            _config = config;
 2743            _fileSystem = fileSystem;
 2744            _httpClientFactory = httpClientFactory;
 2745            Current = this;
 2746        }
 47
 48        public static AudioDbArtistProvider Current { get; private set; }
 49
 50        /// <inheritdoc />
 051        public string Name => "TheAudioDB";
 52
 53        /// <inheritdoc />
 54        // After musicbrainz
 055        public int Order => 1;
 56
 57        /// <inheritdoc />
 58        public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken can
 59        {
 60            // Prefer a known TheAudioDB artist id.
 061            var audioDbId = searchInfo.GetProviderId(MetadataProvider.AudioDbArtist);
 062            if (!string.IsNullOrWhiteSpace(audioDbId))
 63            {
 064                var artists = await FetchArtists(BaseUrl + "/artist.php?i=" + audioDbId, cancellationToken).ConfigureAwa
 065                return artists.Select(ToRemoteSearchResult);
 66            }
 67
 68            // Fall back to the MusicBrainz artist id, reusing the on-disk cache also used by GetMetadata.
 069            var musicBrainzId = searchInfo.GetMusicBrainzArtistId();
 070            if (!string.IsNullOrWhiteSpace(musicBrainzId))
 71            {
 072                await EnsureArtistInfo(musicBrainzId, cancellationToken).ConfigureAwait(false);
 73
 074                var path = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId);
 75
 076                FileStream jsonStream = AsyncFile.OpenRead(path);
 077                await using (jsonStream.ConfigureAwait(false))
 78                {
 079                    var obj = await JsonSerializer.DeserializeAsync<RootObject>(jsonStream, _jsonOptions, cancellationTo
 80
 081                    if (obj is not null && obj.artists is not null)
 82                    {
 083                        return obj.artists.Select(ToRemoteSearchResult);
 84                    }
 85                }
 86
 087                return [];
 88            }
 89
 90            // Finally, search by name.
 091            if (!string.IsNullOrWhiteSpace(searchInfo.Name))
 92            {
 093                var artists = await FetchArtists(BaseUrl + "/search.php?s=" + Uri.EscapeDataString(searchInfo.Name), can
 094                return artists.Select(ToRemoteSearchResult);
 95            }
 96
 097            return [];
 098        }
 99
 100        private async Task<List<Artist>> FetchArtists(string url, CancellationToken cancellationToken)
 101        {
 0102            using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationTo
 0103            response.EnsureSuccessStatusCode();
 104
 0105            var obj = await response.Content.ReadFromJsonAsync<RootObject>(_jsonOptions, cancellationToken).ConfigureAwa
 106
 0107            return obj?.artists ?? [];
 0108        }
 109
 110        private RemoteSearchResult ToRemoteSearchResult(Artist artist)
 111        {
 0112            var result = new RemoteSearchResult
 0113            {
 0114                Name = artist.strArtist,
 0115                ImageUrl = artist.strArtistThumb,
 0116                SearchProviderName = Name,
 0117                Overview = (artist.strBiographyEN ?? string.Empty).StripHtml()
 0118            };
 119
 0120            if (!string.IsNullOrEmpty(artist.idArtist))
 121            {
 0122                result.SetProviderId(MetadataProvider.AudioDbArtist, artist.idArtist);
 123            }
 124
 0125            if (!string.IsNullOrEmpty(artist.strMusicBrainzID))
 126            {
 0127                result.SetProviderId(MetadataProvider.MusicBrainzArtist, artist.strMusicBrainzID);
 128            }
 129
 0130            if (int.TryParse(artist.intFormedYear, NumberStyles.Integer, CultureInfo.InvariantCulture, out var formedYea
 131            {
 0132                result.ProductionYear = formedYear;
 133            }
 134
 0135            return result;
 136        }
 137
 138        /// <inheritdoc />
 139        public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken)
 140        {
 0141            var result = new MetadataResult<MusicArtist>();
 142
 0143            var artist = await GetArtist(
 0144                info.GetMusicBrainzArtistId(),
 0145                info.GetProviderId(MetadataProvider.AudioDbArtist),
 0146                cancellationToken).ConfigureAwait(false);
 147
 0148            if (artist is not null)
 149            {
 0150                result.Item = new MusicArtist();
 0151                result.HasMetadata = true;
 0152                ProcessResult(result, artist, info.MetadataLanguage);
 153            }
 154
 0155            return result;
 0156        }
 157
 158        /// <summary>
 159        /// Resolves the cached AudioDB artist, preferring the MusicBrainz id and falling back to the AudioDB id.
 160        /// </summary>
 161        /// <param name="musicBrainzId">The MusicBrainz artist id, if known.</param>
 162        /// <param name="audioDbId">The TheAudioDB artist id, if known.</param>
 163        /// <param name="cancellationToken">The cancellation token.</param>
 164        /// <returns>The matching artist, or <c>null</c> if none could be resolved.</returns>
 165        internal async Task<Artist> GetArtist(string musicBrainzId, string audioDbId, CancellationToken cancellationToke
 166        {
 167            string path;
 0168            if (!string.IsNullOrWhiteSpace(musicBrainzId))
 169            {
 0170                await EnsureArtistInfo(musicBrainzId, cancellationToken).ConfigureAwait(false);
 0171                path = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId);
 172            }
 0173            else if (!string.IsNullOrWhiteSpace(audioDbId))
 174            {
 0175                await EnsureArtistInfoByAudioDbId(audioDbId, cancellationToken).ConfigureAwait(false);
 0176                path = GetArtistInfoPath(_config.ApplicationPaths, audioDbId);
 177            }
 178            else
 179            {
 0180                return null;
 181            }
 182
 0183            FileStream jsonStream = AsyncFile.OpenRead(path);
 0184            await using (jsonStream.ConfigureAwait(false))
 185            {
 0186                var obj = await JsonSerializer.DeserializeAsync<RootObject>(jsonStream, _jsonOptions, cancellationToken)
 187
 0188                if (obj is not null && obj.artists is not null && obj.artists.Count > 0)
 189                {
 0190                    return obj.artists[0];
 191                }
 192            }
 193
 0194            return null;
 0195        }
 196
 197        private void ProcessResult(MetadataResult<MusicArtist> metadataResult, Artist result, string preferredLanguage)
 198        {
 0199            var item = metadataResult.Item;
 200
 0201            if (!string.IsNullOrWhiteSpace(result.strWebsite))
 202            {
 0203                item.HomePageUrl = result.strWebsite;
 204            }
 205
 0206            var genres = new List<string>();
 0207            if (!string.IsNullOrWhiteSpace(result.strGenre))
 208            {
 0209                genres.Add(result.strGenre);
 210            }
 211
 0212            if (!string.IsNullOrWhiteSpace(result.strSubGenre))
 213            {
 0214                genres.Add(result.strSubGenre);
 215            }
 216
 0217            if (genres.Count > 0)
 218            {
 0219                item.Genres = genres.ToArray();
 220            }
 221
 0222            if (int.TryParse(result.intFormedYear, NumberStyles.Integer, CultureInfo.InvariantCulture, out var formedYea
 223            {
 0224                item.ProductionYear = formedYear;
 225            }
 226
 0227            if (!string.IsNullOrWhiteSpace(result.strCountry))
 228            {
 0229                item.ProductionLocations = new[] { result.strCountry };
 230            }
 231
 0232            item.SetProviderId(MetadataProvider.AudioDbArtist, result.idArtist);
 0233            item.SetProviderId(MetadataProvider.MusicBrainzArtist, result.strMusicBrainzID);
 234
 0235            var language = MetadataLanguageUtils.GetLanguageSubtag(preferredLanguage);
 0236            var overview = GetBiography(result, language);
 237
 0238            if (string.IsNullOrWhiteSpace(overview))
 239            {
 0240                overview = string.IsNullOrWhiteSpace(result.strBiographyEN)
 0241                    ? result.strBiography
 0242                    : result.strBiographyEN;
 243
 244                // The biography is not in the requested language, mark it as English so it does not
 245                // block a provider further down the list that can serve the requested language
 0246                metadataResult.ResultLanguage = "en";
 247            }
 248            else
 249            {
 0250                metadataResult.ResultLanguage = language;
 251            }
 252
 0253            item.Overview = (overview ?? string.Empty).StripHtml();
 0254        }
 255
 256        private static string GetBiography(Artist result, string language)
 0257            => language switch
 0258            {
 0259                "de" => result.strBiographyDE,
 0260                "en" => result.strBiographyEN,
 0261                "es" => result.strBiographyES,
 0262                "fr" => result.strBiographyFR,
 0263                "he" => result.strBiographyIL,
 0264                "hu" => result.strBiographyHU,
 0265                "it" => result.strBiographyIT,
 0266                "ja" => result.strBiographyJP,
 0267                "nl" => result.strBiographyNL,
 0268                "no" or "nb" or "nn" => result.strBiographyNO,
 0269                "pl" => result.strBiographyPL,
 0270                "pt" => result.strBiographyPT,
 0271                "ru" => result.strBiographyRU,
 0272                "sv" => result.strBiographySE,
 0273                "zh" => result.strBiographyCN,
 0274                _ => null
 0275            };
 276
 277        internal async Task EnsureArtistInfo(string musicBrainzId, CancellationToken cancellationToken)
 278        {
 0279            var xmlPath = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId);
 280
 0281            var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
 282
 0283            if (fileInfo.Exists
 0284                && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
 285            {
 0286                return;
 287            }
 288
 0289            await DownloadArtistInfo(musicBrainzId, cancellationToken).ConfigureAwait(false);
 0290        }
 291
 292        internal async Task DownloadArtistInfo(string musicBrainzId, CancellationToken cancellationToken)
 293        {
 0294            var url = BaseUrl + "/artist-mb.php?i=" + musicBrainzId;
 0295            await DownloadArtistInfo(url, GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId), cancellationToken)
 0296        }
 297
 298        internal async Task EnsureArtistInfoByAudioDbId(string audioDbId, CancellationToken cancellationToken)
 299        {
 0300            var xmlPath = GetArtistInfoPath(_config.ApplicationPaths, audioDbId);
 301
 0302            var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
 303
 0304            if (fileInfo.Exists
 0305                && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
 306            {
 0307                return;
 308            }
 309
 0310            var url = BaseUrl + "/artist.php?i=" + audioDbId;
 0311            await DownloadArtistInfo(url, xmlPath, cancellationToken).ConfigureAwait(false);
 0312        }
 313
 314        private async Task DownloadArtistInfo(string url, string path, CancellationToken cancellationToken)
 315        {
 0316            cancellationToken.ThrowIfCancellationRequested();
 317
 0318            using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationTo
 0319            response.EnsureSuccessStatusCode();
 0320            Directory.CreateDirectory(Path.GetDirectoryName(path));
 321
 0322            var fileStreamOptions = AsyncFile.WriteOptions;
 0323            fileStreamOptions.Mode = FileMode.Create;
 0324            var xmlFileStream = new FileStream(path, fileStreamOptions);
 0325            await using (xmlFileStream.ConfigureAwait(false))
 326            {
 0327                await response.Content.CopyToAsync(xmlFileStream, cancellationToken).ConfigureAwait(false);
 328            }
 0329        }
 330
 331        /// <summary>
 332        /// Gets the artist data path.
 333        /// </summary>
 334        /// <param name="appPaths">The application paths.</param>
 335        /// <param name="musicBrainzArtistId">The music brainz artist identifier.</param>
 336        /// <returns>System.String.</returns>
 337        private static string GetArtistDataPath(IApplicationPaths appPaths, string musicBrainzArtistId)
 0338            => Path.Combine(GetArtistDataPath(appPaths), musicBrainzArtistId);
 339
 340        /// <summary>
 341        /// Gets the artist data path.
 342        /// </summary>
 343        /// <param name="appPaths">The application paths.</param>
 344        /// <returns>System.String.</returns>
 345        private static string GetArtistDataPath(IApplicationPaths appPaths)
 0346            => Path.Combine(appPaths.CachePath, "audiodb-artist");
 347
 348        internal static string GetArtistInfoPath(IApplicationPaths appPaths, string musicBrainzArtistId)
 349        {
 0350            var dataPath = GetArtistDataPath(appPaths, musicBrainzArtistId);
 351
 0352            return Path.Combine(dataPath, "artist.json");
 353        }
 354
 355        /// <inheritdoc />
 356        public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
 357        {
 0358            throw new NotImplementedException();
 359        }
 360
 361        public class Artist
 362        {
 363            public string idArtist { get; set; }
 364
 365            public string strArtist { get; set; }
 366
 367            public string strArtistAlternate { get; set; }
 368
 369            public object idLabel { get; set; }
 370
 371            public string intFormedYear { get; set; }
 372
 373            public string intBornYear { get; set; }
 374
 375            public object intDiedYear { get; set; }
 376
 377            public object strDisbanded { get; set; }
 378
 379            public string strGenre { get; set; }
 380
 381            public string strSubGenre { get; set; }
 382
 383            public string strWebsite { get; set; }
 384
 385            public string strFacebook { get; set; }
 386
 387            public string strTwitter { get; set; }
 388
 389            public string strBiography { get; set; }
 390
 391            public string strBiographyEN { get; set; }
 392
 393            public string strBiographyDE { get; set; }
 394
 395            public string strBiographyFR { get; set; }
 396
 397            public string strBiographyCN { get; set; }
 398
 399            public string strBiographyIT { get; set; }
 400
 401            public string strBiographyJP { get; set; }
 402
 403            public string strBiographyRU { get; set; }
 404
 405            public string strBiographyES { get; set; }
 406
 407            public string strBiographyPT { get; set; }
 408
 409            public string strBiographySE { get; set; }
 410
 411            public string strBiographyNL { get; set; }
 412
 413            public string strBiographyHU { get; set; }
 414
 415            public string strBiographyNO { get; set; }
 416
 417            public string strBiographyIL { get; set; }
 418
 419            public string strBiographyPL { get; set; }
 420
 421            public string strGender { get; set; }
 422
 423            public string intMembers { get; set; }
 424
 425            public string strCountry { get; set; }
 426
 427            public string strCountryCode { get; set; }
 428
 429            public string strArtistThumb { get; set; }
 430
 431            public string strArtistLogo { get; set; }
 432
 433            public string strArtistFanart { get; set; }
 434
 435            public string strArtistFanart2 { get; set; }
 436
 437            public string strArtistFanart3 { get; set; }
 438
 439            public string strArtistBanner { get; set; }
 440
 441            public string strMusicBrainzID { get; set; }
 442
 443            public object strLastFMChart { get; set; }
 444
 445            public string strLocked { get; set; }
 446        }
 447
 448#pragma warning disable CA2227
 449        public class RootObject
 450        {
 451            public List<Artist> artists { get; set; }
 452        }
 453    }
 454}