| | | 1 | | #nullable disable |
| | | 2 | | |
| | | 3 | | #pragma warning disable CA1034, CS1591, CA1002, SA1028, SA1300 |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System.Net.Http; |
| | | 11 | | using System.Net.Http.Json; |
| | | 12 | | using System.Text.Json; |
| | | 13 | | using System.Threading; |
| | | 14 | | using System.Threading.Tasks; |
| | | 15 | | using Jellyfin.Extensions.Json; |
| | | 16 | | using MediaBrowser.Common.Configuration; |
| | | 17 | | using MediaBrowser.Common.Extensions; |
| | | 18 | | using MediaBrowser.Common.Net; |
| | | 19 | | using MediaBrowser.Controller.Configuration; |
| | | 20 | | using MediaBrowser.Controller.Entities.Audio; |
| | | 21 | | using MediaBrowser.Controller.Providers; |
| | | 22 | | using MediaBrowser.Model.Entities; |
| | | 23 | | using MediaBrowser.Model.IO; |
| | | 24 | | using MediaBrowser.Model.Providers; |
| | | 25 | | using MediaBrowser.Providers.Music; |
| | | 26 | | |
| | | 27 | | namespace MediaBrowser.Providers.Plugins.AudioDb |
| | | 28 | | { |
| | | 29 | | public class AudioDbArtistProvider : IRemoteMetadataProvider<MusicArtist, ArtistInfo>, IHasOrder |
| | | 30 | | { |
| | | 31 | | private const string ApiKey = "195003"; |
| | | 32 | | public const string BaseUrl = "https://www.theaudiodb.com/api/v1/json/" + ApiKey; |
| | | 33 | | |
| | | 34 | | private readonly IServerConfigurationManager _config; |
| | | 35 | | private readonly IFileSystem _fileSystem; |
| | | 36 | | private readonly IHttpClientFactory _httpClientFactory; |
| | 22 | 37 | | private readonly JsonSerializerOptions _jsonOptions = JsonDefaults.Options; |
| | | 38 | | |
| | | 39 | | public AudioDbArtistProvider(IServerConfigurationManager config, IFileSystem fileSystem, IHttpClientFactory http |
| | | 40 | | { |
| | 22 | 41 | | _config = config; |
| | 22 | 42 | | _fileSystem = fileSystem; |
| | 22 | 43 | | _httpClientFactory = httpClientFactory; |
| | 22 | 44 | | Current = this; |
| | 22 | 45 | | } |
| | | 46 | | |
| | | 47 | | public static AudioDbArtistProvider Current { get; private set; } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | 0 | 50 | | public string Name => "TheAudioDB"; |
| | | 51 | | |
| | | 52 | | /// <inheritdoc /> |
| | | 53 | | // After musicbrainz |
| | 0 | 54 | | public int Order => 1; |
| | | 55 | | |
| | | 56 | | /// <inheritdoc /> |
| | | 57 | | public async Task<IEnumerable<RemoteSearchResult>> GetSearchResults(ArtistInfo searchInfo, CancellationToken can |
| | | 58 | | { |
| | | 59 | | // Prefer a known TheAudioDB artist id. |
| | 0 | 60 | | var audioDbId = searchInfo.GetProviderId(MetadataProvider.AudioDbArtist); |
| | 0 | 61 | | if (!string.IsNullOrWhiteSpace(audioDbId)) |
| | | 62 | | { |
| | 0 | 63 | | var artists = await FetchArtists(BaseUrl + "/artist.php?i=" + audioDbId, cancellationToken).ConfigureAwa |
| | 0 | 64 | | return artists.Select(ToRemoteSearchResult); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | // Fall back to the MusicBrainz artist id, reusing the on-disk cache also used by GetMetadata. |
| | 0 | 68 | | var musicBrainzId = searchInfo.GetMusicBrainzArtistId(); |
| | 0 | 69 | | if (!string.IsNullOrWhiteSpace(musicBrainzId)) |
| | | 70 | | { |
| | 0 | 71 | | await EnsureArtistInfo(musicBrainzId, cancellationToken).ConfigureAwait(false); |
| | | 72 | | |
| | 0 | 73 | | var path = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId); |
| | | 74 | | |
| | 0 | 75 | | FileStream jsonStream = AsyncFile.OpenRead(path); |
| | 0 | 76 | | await using (jsonStream.ConfigureAwait(false)) |
| | | 77 | | { |
| | 0 | 78 | | var obj = await JsonSerializer.DeserializeAsync<RootObject>(jsonStream, _jsonOptions, cancellationTo |
| | | 79 | | |
| | 0 | 80 | | if (obj is not null && obj.artists is not null) |
| | | 81 | | { |
| | 0 | 82 | | return obj.artists.Select(ToRemoteSearchResult); |
| | | 83 | | } |
| | | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | return []; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | // Finally, search by name. |
| | 0 | 90 | | if (!string.IsNullOrWhiteSpace(searchInfo.Name)) |
| | | 91 | | { |
| | 0 | 92 | | var artists = await FetchArtists(BaseUrl + "/search.php?s=" + Uri.EscapeDataString(searchInfo.Name), can |
| | 0 | 93 | | return artists.Select(ToRemoteSearchResult); |
| | | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | return []; |
| | 0 | 97 | | } |
| | | 98 | | |
| | | 99 | | private async Task<List<Artist>> FetchArtists(string url, CancellationToken cancellationToken) |
| | | 100 | | { |
| | 0 | 101 | | using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationTo |
| | 0 | 102 | | response.EnsureSuccessStatusCode(); |
| | | 103 | | |
| | 0 | 104 | | var obj = await response.Content.ReadFromJsonAsync<RootObject>(_jsonOptions, cancellationToken).ConfigureAwa |
| | | 105 | | |
| | 0 | 106 | | return obj?.artists ?? []; |
| | 0 | 107 | | } |
| | | 108 | | |
| | | 109 | | private RemoteSearchResult ToRemoteSearchResult(Artist artist) |
| | | 110 | | { |
| | 0 | 111 | | var result = new RemoteSearchResult |
| | 0 | 112 | | { |
| | 0 | 113 | | Name = artist.strArtist, |
| | 0 | 114 | | ImageUrl = artist.strArtistThumb, |
| | 0 | 115 | | SearchProviderName = Name, |
| | 0 | 116 | | Overview = (artist.strBiographyEN ?? string.Empty).StripHtml() |
| | 0 | 117 | | }; |
| | | 118 | | |
| | 0 | 119 | | if (!string.IsNullOrEmpty(artist.idArtist)) |
| | | 120 | | { |
| | 0 | 121 | | result.SetProviderId(MetadataProvider.AudioDbArtist, artist.idArtist); |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | if (!string.IsNullOrEmpty(artist.strMusicBrainzID)) |
| | | 125 | | { |
| | 0 | 126 | | result.SetProviderId(MetadataProvider.MusicBrainzArtist, artist.strMusicBrainzID); |
| | | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | if (int.TryParse(artist.intFormedYear, NumberStyles.Integer, CultureInfo.InvariantCulture, out var formedYea |
| | | 130 | | { |
| | 0 | 131 | | result.ProductionYear = formedYear; |
| | | 132 | | } |
| | | 133 | | |
| | 0 | 134 | | return result; |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <inheritdoc /> |
| | | 138 | | public async Task<MetadataResult<MusicArtist>> GetMetadata(ArtistInfo info, CancellationToken cancellationToken) |
| | | 139 | | { |
| | 0 | 140 | | var result = new MetadataResult<MusicArtist>(); |
| | | 141 | | |
| | 0 | 142 | | var artist = await GetArtist( |
| | 0 | 143 | | info.GetMusicBrainzArtistId(), |
| | 0 | 144 | | info.GetProviderId(MetadataProvider.AudioDbArtist), |
| | 0 | 145 | | cancellationToken).ConfigureAwait(false); |
| | | 146 | | |
| | 0 | 147 | | if (artist is not null) |
| | | 148 | | { |
| | 0 | 149 | | result.Item = new MusicArtist(); |
| | 0 | 150 | | result.HasMetadata = true; |
| | 0 | 151 | | ProcessResult(result.Item, artist, info.MetadataLanguage); |
| | | 152 | | } |
| | | 153 | | |
| | 0 | 154 | | return result; |
| | 0 | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Resolves the cached AudioDB artist, preferring the MusicBrainz id and falling back to the AudioDB id. |
| | | 159 | | /// </summary> |
| | | 160 | | /// <param name="musicBrainzId">The MusicBrainz artist id, if known.</param> |
| | | 161 | | /// <param name="audioDbId">The TheAudioDB artist id, if known.</param> |
| | | 162 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 163 | | /// <returns>The matching artist, or <c>null</c> if none could be resolved.</returns> |
| | | 164 | | internal async Task<Artist> GetArtist(string musicBrainzId, string audioDbId, CancellationToken cancellationToke |
| | | 165 | | { |
| | | 166 | | string path; |
| | 0 | 167 | | if (!string.IsNullOrWhiteSpace(musicBrainzId)) |
| | | 168 | | { |
| | 0 | 169 | | await EnsureArtistInfo(musicBrainzId, cancellationToken).ConfigureAwait(false); |
| | 0 | 170 | | path = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId); |
| | | 171 | | } |
| | 0 | 172 | | else if (!string.IsNullOrWhiteSpace(audioDbId)) |
| | | 173 | | { |
| | 0 | 174 | | await EnsureArtistInfoByAudioDbId(audioDbId, cancellationToken).ConfigureAwait(false); |
| | 0 | 175 | | path = GetArtistInfoPath(_config.ApplicationPaths, audioDbId); |
| | | 176 | | } |
| | | 177 | | else |
| | | 178 | | { |
| | 0 | 179 | | return null; |
| | | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | FileStream jsonStream = AsyncFile.OpenRead(path); |
| | 0 | 183 | | await using (jsonStream.ConfigureAwait(false)) |
| | | 184 | | { |
| | 0 | 185 | | var obj = await JsonSerializer.DeserializeAsync<RootObject>(jsonStream, _jsonOptions, cancellationToken) |
| | | 186 | | |
| | 0 | 187 | | if (obj is not null && obj.artists is not null && obj.artists.Count > 0) |
| | | 188 | | { |
| | 0 | 189 | | return obj.artists[0]; |
| | | 190 | | } |
| | | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | return null; |
| | 0 | 194 | | } |
| | | 195 | | |
| | | 196 | | private void ProcessResult(MusicArtist item, Artist result, string preferredLanguage) |
| | | 197 | | { |
| | 0 | 198 | | if (!string.IsNullOrWhiteSpace(result.strWebsite)) |
| | | 199 | | { |
| | 0 | 200 | | item.HomePageUrl = result.strWebsite; |
| | | 201 | | } |
| | | 202 | | |
| | 0 | 203 | | var genres = new List<string>(); |
| | 0 | 204 | | if (!string.IsNullOrWhiteSpace(result.strGenre)) |
| | | 205 | | { |
| | 0 | 206 | | genres.Add(result.strGenre); |
| | | 207 | | } |
| | | 208 | | |
| | 0 | 209 | | if (!string.IsNullOrWhiteSpace(result.strSubGenre)) |
| | | 210 | | { |
| | 0 | 211 | | genres.Add(result.strSubGenre); |
| | | 212 | | } |
| | | 213 | | |
| | 0 | 214 | | if (genres.Count > 0) |
| | | 215 | | { |
| | 0 | 216 | | item.Genres = genres.ToArray(); |
| | | 217 | | } |
| | | 218 | | |
| | 0 | 219 | | if (int.TryParse(result.intFormedYear, NumberStyles.Integer, CultureInfo.InvariantCulture, out var formedYea |
| | | 220 | | { |
| | 0 | 221 | | item.ProductionYear = formedYear; |
| | | 222 | | } |
| | | 223 | | |
| | 0 | 224 | | if (!string.IsNullOrWhiteSpace(result.strCountry)) |
| | | 225 | | { |
| | 0 | 226 | | item.ProductionLocations = new[] { result.strCountry }; |
| | | 227 | | } |
| | | 228 | | |
| | 0 | 229 | | item.SetProviderId(MetadataProvider.AudioDbArtist, result.idArtist); |
| | 0 | 230 | | item.SetProviderId(MetadataProvider.MusicBrainzArtist, result.strMusicBrainzID); |
| | | 231 | | |
| | 0 | 232 | | string overview = null; |
| | | 233 | | |
| | 0 | 234 | | if (string.Equals(preferredLanguage, "de", StringComparison.OrdinalIgnoreCase)) |
| | | 235 | | { |
| | 0 | 236 | | overview = result.strBiographyDE; |
| | | 237 | | } |
| | 0 | 238 | | else if (string.Equals(preferredLanguage, "fr", StringComparison.OrdinalIgnoreCase)) |
| | | 239 | | { |
| | 0 | 240 | | overview = result.strBiographyFR; |
| | | 241 | | } |
| | 0 | 242 | | else if (string.Equals(preferredLanguage, "nl", StringComparison.OrdinalIgnoreCase)) |
| | | 243 | | { |
| | 0 | 244 | | overview = result.strBiographyNL; |
| | | 245 | | } |
| | 0 | 246 | | else if (string.Equals(preferredLanguage, "ru", StringComparison.OrdinalIgnoreCase)) |
| | | 247 | | { |
| | 0 | 248 | | overview = result.strBiographyRU; |
| | | 249 | | } |
| | 0 | 250 | | else if (string.Equals(preferredLanguage, "it", StringComparison.OrdinalIgnoreCase)) |
| | | 251 | | { |
| | 0 | 252 | | overview = result.strBiographyIT; |
| | | 253 | | } |
| | 0 | 254 | | else if ((preferredLanguage ?? string.Empty).StartsWith("pt", StringComparison.OrdinalIgnoreCase)) |
| | | 255 | | { |
| | 0 | 256 | | overview = result.strBiographyPT; |
| | | 257 | | } |
| | | 258 | | |
| | 0 | 259 | | if (string.IsNullOrWhiteSpace(overview)) |
| | | 260 | | { |
| | 0 | 261 | | overview = string.IsNullOrWhiteSpace(result.strBiographyEN) |
| | 0 | 262 | | ? result.strBiography |
| | 0 | 263 | | : result.strBiographyEN; |
| | | 264 | | } |
| | | 265 | | |
| | 0 | 266 | | item.Overview = (overview ?? string.Empty).StripHtml(); |
| | 0 | 267 | | } |
| | | 268 | | |
| | | 269 | | internal async Task EnsureArtistInfo(string musicBrainzId, CancellationToken cancellationToken) |
| | | 270 | | { |
| | 0 | 271 | | var xmlPath = GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId); |
| | | 272 | | |
| | 0 | 273 | | var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath); |
| | | 274 | | |
| | 0 | 275 | | if (fileInfo.Exists |
| | 0 | 276 | | && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2) |
| | | 277 | | { |
| | 0 | 278 | | return; |
| | | 279 | | } |
| | | 280 | | |
| | 0 | 281 | | await DownloadArtistInfo(musicBrainzId, cancellationToken).ConfigureAwait(false); |
| | 0 | 282 | | } |
| | | 283 | | |
| | | 284 | | internal async Task DownloadArtistInfo(string musicBrainzId, CancellationToken cancellationToken) |
| | | 285 | | { |
| | 0 | 286 | | var url = BaseUrl + "/artist-mb.php?i=" + musicBrainzId; |
| | 0 | 287 | | await DownloadArtistInfo(url, GetArtistInfoPath(_config.ApplicationPaths, musicBrainzId), cancellationToken) |
| | 0 | 288 | | } |
| | | 289 | | |
| | | 290 | | internal async Task EnsureArtistInfoByAudioDbId(string audioDbId, CancellationToken cancellationToken) |
| | | 291 | | { |
| | 0 | 292 | | var xmlPath = GetArtistInfoPath(_config.ApplicationPaths, audioDbId); |
| | | 293 | | |
| | 0 | 294 | | var fileInfo = _fileSystem.GetFileSystemInfo(xmlPath); |
| | | 295 | | |
| | 0 | 296 | | if (fileInfo.Exists |
| | 0 | 297 | | && (DateTime.UtcNow - _fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays <= 2) |
| | | 298 | | { |
| | 0 | 299 | | return; |
| | | 300 | | } |
| | | 301 | | |
| | 0 | 302 | | var url = BaseUrl + "/artist.php?i=" + audioDbId; |
| | 0 | 303 | | await DownloadArtistInfo(url, xmlPath, cancellationToken).ConfigureAwait(false); |
| | 0 | 304 | | } |
| | | 305 | | |
| | | 306 | | private async Task DownloadArtistInfo(string url, string path, CancellationToken cancellationToken) |
| | | 307 | | { |
| | 0 | 308 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 309 | | |
| | 0 | 310 | | using var response = await _httpClientFactory.CreateClient(NamedClient.Default).GetAsync(url, cancellationTo |
| | 0 | 311 | | response.EnsureSuccessStatusCode(); |
| | 0 | 312 | | Directory.CreateDirectory(Path.GetDirectoryName(path)); |
| | | 313 | | |
| | 0 | 314 | | var fileStreamOptions = AsyncFile.WriteOptions; |
| | 0 | 315 | | fileStreamOptions.Mode = FileMode.Create; |
| | 0 | 316 | | var xmlFileStream = new FileStream(path, fileStreamOptions); |
| | 0 | 317 | | await using (xmlFileStream.ConfigureAwait(false)) |
| | | 318 | | { |
| | 0 | 319 | | await response.Content.CopyToAsync(xmlFileStream, cancellationToken).ConfigureAwait(false); |
| | | 320 | | } |
| | 0 | 321 | | } |
| | | 322 | | |
| | | 323 | | /// <summary> |
| | | 324 | | /// Gets the artist data path. |
| | | 325 | | /// </summary> |
| | | 326 | | /// <param name="appPaths">The application paths.</param> |
| | | 327 | | /// <param name="musicBrainzArtistId">The music brainz artist identifier.</param> |
| | | 328 | | /// <returns>System.String.</returns> |
| | | 329 | | private static string GetArtistDataPath(IApplicationPaths appPaths, string musicBrainzArtistId) |
| | 0 | 330 | | => Path.Combine(GetArtistDataPath(appPaths), musicBrainzArtistId); |
| | | 331 | | |
| | | 332 | | /// <summary> |
| | | 333 | | /// Gets the artist data path. |
| | | 334 | | /// </summary> |
| | | 335 | | /// <param name="appPaths">The application paths.</param> |
| | | 336 | | /// <returns>System.String.</returns> |
| | | 337 | | private static string GetArtistDataPath(IApplicationPaths appPaths) |
| | 0 | 338 | | => Path.Combine(appPaths.CachePath, "audiodb-artist"); |
| | | 339 | | |
| | | 340 | | internal static string GetArtistInfoPath(IApplicationPaths appPaths, string musicBrainzArtistId) |
| | | 341 | | { |
| | 0 | 342 | | var dataPath = GetArtistDataPath(appPaths, musicBrainzArtistId); |
| | | 343 | | |
| | 0 | 344 | | return Path.Combine(dataPath, "artist.json"); |
| | | 345 | | } |
| | | 346 | | |
| | | 347 | | /// <inheritdoc /> |
| | | 348 | | public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) |
| | | 349 | | { |
| | 0 | 350 | | throw new NotImplementedException(); |
| | | 351 | | } |
| | | 352 | | |
| | | 353 | | public class Artist |
| | | 354 | | { |
| | | 355 | | public string idArtist { get; set; } |
| | | 356 | | |
| | | 357 | | public string strArtist { get; set; } |
| | | 358 | | |
| | | 359 | | public string strArtistAlternate { get; set; } |
| | | 360 | | |
| | | 361 | | public object idLabel { get; set; } |
| | | 362 | | |
| | | 363 | | public string intFormedYear { get; set; } |
| | | 364 | | |
| | | 365 | | public string intBornYear { get; set; } |
| | | 366 | | |
| | | 367 | | public object intDiedYear { get; set; } |
| | | 368 | | |
| | | 369 | | public object strDisbanded { get; set; } |
| | | 370 | | |
| | | 371 | | public string strGenre { get; set; } |
| | | 372 | | |
| | | 373 | | public string strSubGenre { get; set; } |
| | | 374 | | |
| | | 375 | | public string strWebsite { get; set; } |
| | | 376 | | |
| | | 377 | | public string strFacebook { get; set; } |
| | | 378 | | |
| | | 379 | | public string strTwitter { get; set; } |
| | | 380 | | |
| | | 381 | | public string strBiography { get; set; } |
| | | 382 | | |
| | | 383 | | public string strBiographyEN { get; set; } |
| | | 384 | | |
| | | 385 | | public string strBiographyDE { get; set; } |
| | | 386 | | |
| | | 387 | | public string strBiographyFR { get; set; } |
| | | 388 | | |
| | | 389 | | public string strBiographyCN { get; set; } |
| | | 390 | | |
| | | 391 | | public string strBiographyIT { get; set; } |
| | | 392 | | |
| | | 393 | | public string strBiographyJP { get; set; } |
| | | 394 | | |
| | | 395 | | public string strBiographyRU { get; set; } |
| | | 396 | | |
| | | 397 | | public string strBiographyES { get; set; } |
| | | 398 | | |
| | | 399 | | public string strBiographyPT { get; set; } |
| | | 400 | | |
| | | 401 | | public string strBiographySE { get; set; } |
| | | 402 | | |
| | | 403 | | public string strBiographyNL { get; set; } |
| | | 404 | | |
| | | 405 | | public string strBiographyHU { get; set; } |
| | | 406 | | |
| | | 407 | | public string strBiographyNO { get; set; } |
| | | 408 | | |
| | | 409 | | public string strBiographyIL { get; set; } |
| | | 410 | | |
| | | 411 | | public string strBiographyPL { get; set; } |
| | | 412 | | |
| | | 413 | | public string strGender { get; set; } |
| | | 414 | | |
| | | 415 | | public string intMembers { get; set; } |
| | | 416 | | |
| | | 417 | | public string strCountry { get; set; } |
| | | 418 | | |
| | | 419 | | public string strCountryCode { get; set; } |
| | | 420 | | |
| | | 421 | | public string strArtistThumb { get; set; } |
| | | 422 | | |
| | | 423 | | public string strArtistLogo { get; set; } |
| | | 424 | | |
| | | 425 | | public string strArtistFanart { get; set; } |
| | | 426 | | |
| | | 427 | | public string strArtistFanart2 { get; set; } |
| | | 428 | | |
| | | 429 | | public string strArtistFanart3 { get; set; } |
| | | 430 | | |
| | | 431 | | public string strArtistBanner { get; set; } |
| | | 432 | | |
| | | 433 | | public string strMusicBrainzID { get; set; } |
| | | 434 | | |
| | | 435 | | public object strLastFMChart { get; set; } |
| | | 436 | | |
| | | 437 | | public string strLocked { get; set; } |
| | | 438 | | } |
| | | 439 | | |
| | | 440 | | #pragma warning disable CA2227 |
| | | 441 | | public class RootObject |
| | | 442 | | { |
| | | 443 | | public List<Artist> artists { get; set; } |
| | | 444 | | } |
| | | 445 | | } |
| | | 446 | | } |