< Summary - Jellyfin

Information
Class: MediaBrowser.Providers.Plugins.AudioDb.AudioDbAlbumProvider
Assembly: MediaBrowser.Providers
File(s): /srv/git/jellyfin/MediaBrowser.Providers/Plugins/AudioDb/AudioDbAlbumProvider.cs
Line coverage
7%
Covered lines: 6
Uncovered lines: 76
Coverable lines: 82
Total lines: 307
Line coverage: 7.3%
Branch coverage
0%
Covered branches: 0
Total branches: 48
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 3/6/2026 - 12:14:09 AM Line coverage: 13.3% (6/45) Branch coverage: 0% (0/28) Total lines: 3034/19/2026 - 12:14:27 AM Line coverage: 7.5% (6/80) Branch coverage: 0% (0/46) Total lines: 3036/14/2026 - 12:16:28 AM Line coverage: 7.3% (6/82) Branch coverage: 0% (0/48) Total lines: 307 3/6/2026 - 12:14:09 AM Line coverage: 13.3% (6/45) Branch coverage: 0% (0/28) Total lines: 3034/19/2026 - 12:14:27 AM Line coverage: 7.5% (6/80) Branch coverage: 0% (0/46) Total lines: 3036/14/2026 - 12:16:28 AM Line coverage: 7.3% (6/82) Branch coverage: 0% (0/48) Total lines: 307

Coverage delta

Coverage delta 6 -6

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Name()100%210%
get_Order()100%210%
GetSearchResults(...)100%210%
GetMetadata()0%7280%
ProcessResult(...)0%930300%
EnsureInfo()0%2040%
DownloadInfo()0%4260%
GetAlbumDataPath(...)100%210%
GetAlbumDataPath(...)100%210%
GetAlbumInfoPath(...)100%210%
GetImageResponse(...)100%210%

File(s)

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

#LineLine coverage
 1#nullable disable
 2
 3#pragma warning disable CA1002, CS1591, SA1300
 4
 5using System;
 6using System.Collections.Generic;
 7using System.Globalization;
 8using System.IO;
 9using System.Linq;
 10using System.Net.Http;
 11using System.Text.Json;
 12using System.Threading;
 13using System.Threading.Tasks;
 14using Jellyfin.Extensions.Json;
 15using MediaBrowser.Common.Configuration;
 16using MediaBrowser.Common.Extensions;
 17using MediaBrowser.Common.Net;
 18using MediaBrowser.Controller.Configuration;
 19using MediaBrowser.Controller.Entities.Audio;
 20using MediaBrowser.Controller.Providers;
 21using MediaBrowser.Model.Entities;
 22using MediaBrowser.Model.IO;
 23using MediaBrowser.Model.Providers;
 24using MediaBrowser.Providers.Music;
 25
 26namespace MediaBrowser.Providers.Plugins.AudioDb
 27{
 28    public class AudioDbAlbumProvider : IRemoteMetadataProvider<MusicAlbum, AlbumInfo>, IHasOrder
 29    {
 30        private readonly IServerConfigurationManager _config;
 31        private readonly IFileSystem _fileSystem;
 32        private readonly IHttpClientFactory _httpClientFactory;
 2133        private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options;
 34
 35#pragma warning disable SA1401, CA2211
 36        public static AudioDbAlbumProvider Current;
 37#pragma warning restore SA1401, CA2211
 38
 39        public AudioDbAlbumProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClientFactory httpC
 40        {
 2141            _config = config;
 2142            _fileSystem = fileSystem;
 2143            _httpClientFactory = httpClientFactory;
 44
 2145            Current = this;
 2146        }
 47
 48        /// <inheritdoc />
 049        public string Name => "TheAudioDB";
 50
 51        /// <inheritdoc />
 52        // After music brainz
 053        public int Order => 1;
 54
 55        /// <inheritdoc />
 56        public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(AlbumInfo searchInfo, CancellationToken cancellati
 057            => Task.FromResult(Enumerable.Empty<RemoteSearchResult>());
 58
 59        /// <inheritdoc />
 60        public async Task<MetadataResult<MusicAlbum>> GetMetadata(AlbumInfo info, CancellationToken cancellationToken)
 61        {
 062            var result = new MetadataResult<MusicAlbum>();
 063            var id = info.GetReleaseGroupId();
 64
 065            if (!string.IsNullOrWhiteSpace(id))
 66            {
 067                await EnsureInfo(id, cancellationToken).ConfigureAwait(false);
 68
 069                var path = GetAlbumInfoPath(_config.ApplicationPaths, id);
 70
 071                FileStream jsonStream = AsyncFile.OpenRead(path);
 072                await using (jsonStream.ConfigureAwait(false))
 73                {
 074                    var obj = await JsonSerializer.DeserializeAsync<RootObject>(jsonStream, _jsonOptions, cancellationTo
 75
 076                    if (obj is not null && obj.album is not null && obj.album.Count > 0)
 77                    {
 078                        result.Item = new MusicAlbum();
 079                        result.HasMetadata = true;
 080                        ProcessResult(result.Item, obj.album[0], info.MetadataLanguage);
 81                    }
 82                }
 83            }
 84
 085            return result;
 086        }
 87
 88        private void ProcessResult(MusicAlbum item, Album result, string preferredLanguage)
 89        {
 090            if (Plugin.Instance.Configuration.ReplaceAlbumName && !string.IsNullOrWhiteSpace(result.strAlbum))
 91            {
 092                item.Album = result.strAlbum;
 93            }
 94
 095            if (!string.IsNullOrWhiteSpace(result.strArtist))
 96            {
 097                item.AlbumArtists = [result.strArtist];
 98            }
 99
 0100            if (!string.IsNullOrEmpty(result.intYearReleased))
 101            {
 0102                item.ProductionYear = int.Parse(result.intYearReleased, CultureInfo.InvariantCulture);
 103            }
 104
 0105            if (!string.IsNullOrEmpty(result.strGenre))
 106            {
 0107                item.Genres = [result.strGenre];
 108            }
 109
 0110            item.SetProviderId(MetadataProvider.AudioDbArtist, result.idArtist);
 0111            item.SetProviderId(MetadataProvider.AudioDbAlbum, result.idAlbum);
 112
 0113            item.SetProviderId(MetadataProvider.MusicBrainzAlbumArtist, result.strMusicBrainzArtistID);
 0114            item.SetProviderId(MetadataProvider.MusicBrainzReleaseGroup, result.strMusicBrainzID);
 115
 0116            string overview = null;
 117
 0118            if (string.Equals(preferredLanguage, "de", StringComparison.OrdinalIgnoreCase))
 119            {
 0120                overview = result.strDescriptionDE;
 121            }
 0122            else if (string.Equals(preferredLanguage, "fr", StringComparison.OrdinalIgnoreCase))
 123            {
 0124                overview = result.strDescriptionFR;
 125            }
 0126            else if (string.Equals(preferredLanguage, "nl", StringComparison.OrdinalIgnoreCase))
 127            {
 0128                overview = result.strDescriptionNL;
 129            }
 0130            else if (string.Equals(preferredLanguage, "ru", StringComparison.OrdinalIgnoreCase))
 131            {
 0132                overview = result.strDescriptionRU;
 133            }
 0134            else if (string.Equals(preferredLanguage, "it", StringComparison.OrdinalIgnoreCase))
 135            {
 0136                overview = result.strDescriptionIT;
 137            }
 0138            else if ((preferredLanguage ?? string.Empty).StartsWith("pt", StringComparison.OrdinalIgnoreCase))
 139            {
 0140                overview = result.strDescriptionPT;
 141            }
 142
 0143            if (string.IsNullOrWhiteSpace(overview))
 144            {
 0145                overview = string.IsNullOrWhiteSpace(result.strDescriptionEN)
 0146                    ? result.strDescription
 0147                    : result.strDescriptionEN;
 148            }
 149
 0150            item.Overview = (overview ?? string.Empty).StripHtml();
 0151        }
 152
 153        internal async Task EnsureInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
 154        {
 0155            var xmlPath = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
 156
 0157            var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
 158
 0159            if (fileInfo.Exists
 0160                && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
 161            {
 0162                return;
 163            }
 164
 0165            await DownloadInfo(musicBrainzReleaseGroupId, cancellationToken).ConfigureAwait(false);
 0166        }
 167
 168        internal async Task DownloadInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
 169        {
 0170            cancellationToken.ThrowIfCancellationRequested();
 171
 0172            var url = AudioDbArtistProvider.BaseUrl + "/album-mb.php?i=" + musicBrainzReleaseGroupId;
 173
 0174            var path = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
 0175            var fileInfo = _fileSystem.GetFileSystemInfo(path);
 0176            if (fileInfo.Exists && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
 177            {
 0178                return;
 179            }
 180
 0181            Directory.CreateDirectory(Path.GetDirectoryName(path));
 182
 0183            using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationTo
 0184            var fileStreamOptions = AsyncFile.WriteOptions;
 0185            fileStreamOptions.Mode = FileMode.Create;
 0186            var fs = new FileStream(path, fileStreamOptions);
 0187            await using (fs.ConfigureAwait(false))
 188            {
 0189                await response.Content.CopyToAsync(fs, cancellationToken).ConfigureAwait(false);
 190            }
 0191        }
 192
 193        private static string GetAlbumDataPath(IApplicationPaths appPaths, string musicBrainzReleaseGroupId)
 194        {
 0195            var dataPath = Path.Combine(GetAlbumDataPath(appPaths), musicBrainzReleaseGroupId);
 196
 0197            return dataPath;
 198        }
 199
 200        private static string GetAlbumDataPath(IApplicationPaths appPaths)
 201        {
 0202            var dataPath = Path.Combine(appPaths.CachePath, "audiodb-album");
 203
 0204            return dataPath;
 205        }
 206
 207        internal static string GetAlbumInfoPath(IApplicationPaths appPaths, string musicBrainzReleaseGroupId)
 208        {
 0209            var dataPath = GetAlbumDataPath(appPaths, musicBrainzReleaseGroupId);
 210
 0211            return Path.Combine(dataPath, "album.json");
 212        }
 213
 214        /// <inheritdoc />
 215        public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
 216        {
 0217            throw new NotImplementedException();
 218        }
 219
 220#pragma warning disable CA1034, CA2227
 221        public class Album
 222        {
 223            public string idAlbum { get; set; }
 224
 225            public string idArtist { get; set; }
 226
 227            public string strAlbum { get; set; }
 228
 229            public string strArtist { get; set; }
 230
 231            public string intYearReleased { get; set; }
 232
 233            public string strGenre { get; set; }
 234
 235            public string strSubGenre { get; set; }
 236
 237            public string strReleaseFormat { get; set; }
 238
 239            public string intSales { get; set; }
 240
 241            public string strAlbumThumb { get; set; }
 242
 243            public string strAlbumCDart { get; set; }
 244
 245            public string strDescription { get; set; }
 246
 247            public string strDescriptionEN { get; set; }
 248
 249            public string strDescriptionDE { get; set; }
 250
 251            public string strDescriptionFR { get; set; }
 252
 253            public string strDescriptionCN { get; set; }
 254
 255            public string strDescriptionIT { get; set; }
 256
 257            public string strDescriptionJP { get; set; }
 258
 259            public string strDescriptionRU { get; set; }
 260
 261            public string strDescriptionES { get; set; }
 262
 263            public string strDescriptionPT { get; set; }
 264
 265            public string strDescriptionSE { get; set; }
 266
 267            public string strDescriptionNL { get; set; }
 268
 269            public string strDescriptionHU { get; set; }
 270
 271            public string strDescriptionNO { get; set; }
 272
 273            public string strDescriptionIL { get; set; }
 274
 275            public string strDescriptionPL { get; set; }
 276
 277            public object intLoved { get; set; }
 278
 279            public object intScore { get; set; }
 280
 281            public string strReview { get; set; }
 282
 283            public object strMood { get; set; }
 284
 285            public object strTheme { get; set; }
 286
 287            public object strSpeed { get; set; }
 288
 289            public object strLocation { get; set; }
 290
 291            public string strMusicBrainzID { get; set; }
 292
 293            public string strMusicBrainzArtistID { get; set; }
 294
 295            public object strItunesID { get; set; }
 296
 297            public object strAmazonID { get; set; }
 298
 299            public string strLocked { get; set; }
 300        }
 301
 302        public class RootObject
 303        {
 304            public List<Album> album { get; set; }
 305        }
 306    }
 307}