< 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
13%
Covered lines: 6
Uncovered lines: 39
Coverable lines: 45
Total lines: 303
Line coverage: 13.3%
Branch coverage
0%
Covered branches: 0
Total branches: 28
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%
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        {
 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 = [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 = result.strDescriptionEN;
 146            }
 147
 0148            item.Overview = (overview ?? string.Empty).StripHtml();
 0149        }
 150
 151        internal async Task EnsureInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
 152        {
 153            var xmlPath = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
 154
 155            var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath);
 156
 157            if (fileInfo.Exists
 158                && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
 159            {
 160                return;
 161            }
 162
 163            await DownloadInfo(musicBrainzReleaseGroupId, cancellationToken).ConfigureAwait(false);
 164        }
 165
 166        internal async Task DownloadInfo(string musicBrainzReleaseGroupId, CancellationToken cancellationToken)
 167        {
 168            cancellationToken.ThrowIfCancellationRequested();
 169
 170            var url = AudioDbArtistProvider.BaseUrl + "/album-mb.php?i=" + musicBrainzReleaseGroupId;
 171
 172            var path = GetAlbumInfoPath(_config.ApplicationPaths, musicBrainzReleaseGroupId);
 173            var fileInfo = _fileSystem.GetFileSystemInfo(path);
 174            if (fileInfo.Exists && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2)
 175            {
 176                return;
 177            }
 178
 179            Directory.CreateDirectory(Path.GetDirectoryName(path));
 180
 181            using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationTo
 182            var fileStreamOptions = AsyncFile.WriteOptions;
 183            fileStreamOptions.Mode = FileMode.Create;
 184            var fs = new FileStream(path, fileStreamOptions);
 185            await using (fs.ConfigureAwait(false))
 186            {
 187                await response.Content.CopyToAsync(fs, cancellationToken).ConfigureAwait(false);
 188            }
 189        }
 190
 191        private static string GetAlbumDataPath(IApplicationPaths appPaths, string musicBrainzReleaseGroupId)
 192        {
 0193            var dataPath = Path.Combine(GetAlbumDataPath(appPaths), musicBrainzReleaseGroupId);
 194
 0195            return dataPath;
 196        }
 197
 198        private static string GetAlbumDataPath(IApplicationPaths appPaths)
 199        {
 0200            var dataPath = Path.Combine(appPaths.CachePath, "audiodb-album");
 201
 0202            return dataPath;
 203        }
 204
 205        internal static string GetAlbumInfoPath(IApplicationPaths appPaths, string musicBrainzReleaseGroupId)
 206        {
 0207            var dataPath = GetAlbumDataPath(appPaths, musicBrainzReleaseGroupId);
 208
 0209            return Path.Combine(dataPath, "album.json");
 210        }
 211
 212        /// <inheritdoc />
 213        public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
 214        {
 0215            throw new NotImplementedException();
 216        }
 217
 218#pragma warning disable CA1034, CA2227
 219        public class Album
 220        {
 221            public string idAlbum { get; set; }
 222
 223            public string idArtist { get; set; }
 224
 225            public string strAlbum { get; set; }
 226
 227            public string strArtist { get; set; }
 228
 229            public string intYearReleased { get; set; }
 230
 231            public string strGenre { get; set; }
 232
 233            public string strSubGenre { get; set; }
 234
 235            public string strReleaseFormat { get; set; }
 236
 237            public string intSales { get; set; }
 238
 239            public string strAlbumThumb { get; set; }
 240
 241            public string strAlbumCDart { get; set; }
 242
 243            public string strDescriptionEN { get; set; }
 244
 245            public string strDescriptionDE { get; set; }
 246
 247            public string strDescriptionFR { get; set; }
 248
 249            public string strDescriptionCN { get; set; }
 250
 251            public string strDescriptionIT { get; set; }
 252
 253            public string strDescriptionJP { get; set; }
 254
 255            public string strDescriptionRU { get; set; }
 256
 257            public string strDescriptionES { get; set; }
 258
 259            public string strDescriptionPT { get; set; }
 260
 261            public string strDescriptionSE { get; set; }
 262
 263            public string strDescriptionNL { get; set; }
 264
 265            public string strDescriptionHU { get; set; }
 266
 267            public string strDescriptionNO { get; set; }
 268
 269            public string strDescriptionIL { get; set; }
 270
 271            public string strDescriptionPL { get; set; }
 272
 273            public object intLoved { get; set; }
 274
 275            public object intScore { get; set; }
 276
 277            public string strReview { get; set; }
 278
 279            public object strMood { get; set; }
 280
 281            public object strTheme { get; set; }
 282
 283            public object strSpeed { get; set; }
 284
 285            public object strLocation { get; set; }
 286
 287            public string strMusicBrainzID { get; set; }
 288
 289            public string strMusicBrainzArtistID { get; set; }
 290
 291            public object strItunesID { get; set; }
 292
 293            public object strAmazonID { get; set; }
 294
 295            public string strLocked { get; set; }
 296        }
 297
 298        public class RootObject
 299        {
 300            public List<Album> album { get; set; }
 301        }
 302    }
 303}