< 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
11%
Covered lines: 6
Uncovered lines: 45
Coverable lines: 51
Total lines: 300
Line coverage: 11.7%
Branch coverage
0%
Covered branches: 0
Total branches: 32
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Name()100%210%
get_Order()100%210%
GetSearchResults(...)100%210%
ProcessResult(...)0%812280%
EnsureInfo(...)0%2040%
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;
 2233        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        {
 2241            _config = config;
 2242            _fileSystem = fileSystem;
 2243            _httpClientFactory = httpClientFactory;
 44
 2245            Current = this;
 2246        }
 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        {
 62            var result = new MetadataResult<MusicAlbum>();
 63            var id = info.GetReleaseGroupId();
 64
 65            if (!string.IsNullOrWhiteSpace(id))
 66            {
 67                await EnsureInfo(id, cancellationToken).ConfigureAwait(false);
 68
 69                var path = GetAlbumInfoPath(_config.ApplicationPaths, id);
 70
 71                FileStream jsonStream = AsyncFile.OpenRead(path);
 72                await using (jsonStream.ConfigureAwait(false))
 73                {
 74                    var obj = await JsonSerializer.DeserializeAsync<RootObject>(jsonStream, _jsonOptions, cancellationTo
 75
 76                    if (obj is not null && obj.album is not null && obj.album.Count > 0)
 77                    {
 78                        result.Item = new MusicAlbum();
 79                        result.HasMetadata = true;
 80                        ProcessResult(result.Item, obj.album[0], info.MetadataLanguage);
 81                    }
 82                }
 83            }
 84
 85            return result;
 86        }
 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 = new string[] { 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 = new[] { 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 = result.strDescriptionEN;
 146            }
 147
 0148            item.Overview = (overview ?? string.Empty).StripHtml();
 0149        }
 150
 151        internal Task EnsureInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
 152        {
 0153            var xmlPath = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
 154
 0155            var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
 156
 0157            if (fileInfo.Exists)
 158            {
 0159                if ((DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
 160                {
 0161                    return Task.CompletedTask;
 162                }
 163            }
 164
 0165            return DownloadInfo(musicBrainzReleaseGroupId, cancellationToken);
 166        }
 167
 168        internal async Task DownloadInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
 169        {
 170            cancellationToken.ThrowIfCancellationRequested();
 171
 172            var url = AudioDbArtistProvider.BaseUrl + "/album-mb.php?i=" + musicBrainzReleaseGroupId;
 173
 174            var path = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
 175
 176            Directory.CreateDirectory(Path.GetDirectoryName(path));
 177
 178            using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationTo
 179            var fileStreamOptions = AsyncFile.WriteOptions;
 180            fileStreamOptions.Mode = FileMode.Create;
 181            var fs = new FileStream(path, fileStreamOptions);
 182            await using (fs.ConfigureAwait(false))
 183            {
 184                await response.Content.CopyToAsync(fs, cancellationToken).ConfigureAwait(false);
 185            }
 186        }
 187
 188        private static string GetAlbumDataPath(IApplicationPaths appPaths, string musicBrainzReleaseGroupId)
 189        {
 0190            var dataPath = Path.Combine(GetAlbumDataPath(appPaths), musicBrainzReleaseGroupId);
 191
 0192            return dataPath;
 193        }
 194
 195        private static string GetAlbumDataPath(IApplicationPaths appPaths)
 196        {
 0197            var dataPath = Path.Combine(appPaths.CachePath, "audiodb-album");
 198
 0199            return dataPath;
 200        }
 201
 202        internal static string GetAlbumInfoPath(IApplicationPaths appPaths, string musicBrainzReleaseGroupId)
 203        {
 0204            var dataPath = GetAlbumDataPath(appPaths, musicBrainzReleaseGroupId);
 205
 0206            return Path.Combine(dataPath, "album.json");
 207        }
 208
 209        /// <inheritdoc />
 210        public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
 211        {
 0212            throw new NotImplementedException();
 213        }
 214
 215#pragma warning disable CA1034, CA2227
 216        public class Album
 217        {
 218            public string idAlbum { get; set; }
 219
 220            public string idArtist { get; set; }
 221
 222            public string strAlbum { get; set; }
 223
 224            public string strArtist { get; set; }
 225
 226            public string intYearReleased { get; set; }
 227
 228            public string strGenre { get; set; }
 229
 230            public string strSubGenre { get; set; }
 231
 232            public string strReleaseFormat { get; set; }
 233
 234            public string intSales { get; set; }
 235
 236            public string strAlbumThumb { get; set; }
 237
 238            public string strAlbumCDart { get; set; }
 239
 240            public string strDescriptionEN { get; set; }
 241
 242            public string strDescriptionDE { get; set; }
 243
 244            public string strDescriptionFR { get; set; }
 245
 246            public string strDescriptionCN { get; set; }
 247
 248            public string strDescriptionIT { get; set; }
 249
 250            public string strDescriptionJP { get; set; }
 251
 252            public string strDescriptionRU { get; set; }
 253
 254            public string strDescriptionES { get; set; }
 255
 256            public string strDescriptionPT { get; set; }
 257
 258            public string strDescriptionSE { get; set; }
 259
 260            public string strDescriptionNL { get; set; }
 261
 262            public string strDescriptionHU { get; set; }
 263
 264            public string strDescriptionNO { get; set; }
 265
 266            public string strDescriptionIL { get; set; }
 267
 268            public string strDescriptionPL { get; set; }
 269
 270            public object intLoved { get; set; }
 271
 272            public object intScore { get; set; }
 273
 274            public string strReview { get; set; }
 275
 276            public object strMood { get; set; }
 277
 278            public object strTheme { get; set; }
 279
 280            public object strSpeed { get; set; }
 281
 282            public object strLocation { get; set; }
 283
 284            public string strMusicBrainzID { get; set; }
 285
 286            public string strMusicBrainzArtistID { get; set; }
 287
 288            public object strItunesID { get; set; }
 289
 290            public object strAmazonID { get; set; }
 291
 292            public string strLocked { get; set; }
 293        }
 294
 295        public class RootObject
 296        {
 297            public List<Album> album { get; set; }
 298        }
 299    }
 300}