diff --git a/DeezNET/Directory.Build.props b/DeezNET/Directory.Build.props index 9a6f9f9..46bfe14 100644 --- a/DeezNET/Directory.Build.props +++ b/DeezNET/Directory.Build.props @@ -1,6 +1,6 @@  - 1.2.4 + 1.2.5 TrevTV Copyright (C) Trevor Jackson latest diff --git a/DeezNET/Downloader.cs b/DeezNET/Downloader.cs index 6ddc58a..f466d3e 100644 --- a/DeezNET/Downloader.cs +++ b/DeezNET/Downloader.cs @@ -343,7 +343,15 @@ private async Task ApplyMetadataToTagLibFile(TagLib.File track, long trackId, in JToken page = await _gw.GetTrackPage(trackId, token); long albumId = long.Parse(page["DATA"]!["ALB_ID"]!.ToString()); JToken albumPage = await _gw.GetAlbumPage(albumId, token); - JToken publicAlbum = await _publicApi.GetAlbum(albumId, token: token); + + // the public API is only needed for genres, so a quota error or missing genre list shouldn't fail the whole tag write + string[]? genres = null; + try + { + JToken publicAlbum = await _publicApi.GetAlbum(albumId, token: token); + genres = (publicAlbum["genres"]?["data"] as JArray)?.Select(a => a["name"]?.ToString()).OfType().ToArray(); + } + catch (Exception e) when (e is not OperationCanceledException) { } byte[]? albumArt = null; try { albumArt = await GetArtBytes(page["DATA"]!["ALB_PICTURE"]!.ToString(), coverResolution, token); } catch (UnavailableArtException) { } @@ -377,8 +385,8 @@ private async Task ApplyMetadataToTagLibFile(TagLib.File track, long trackId, in // the song list is incomplete or has no disc numbers, so per-disc counts can't be trusted; keep the album total track.Tag.TrackCount = albumTrackCount; } - // i have yet to find an album without a genre attached, but this may still break at some point - track.Tag.Genres = publicAlbum["genres"]!["data"]!.Select(a => a["name"]!.ToString()).ToArray(); + if (genres != null) + track.Tag.Genres = genres; if (albumArt != null) track.Tag.Pictures = [new TagLib.Picture(new TagLib.ByteVector(albumArt))]; diff --git a/DeezNET/MusicBrainzLookup.cs b/DeezNET/MusicBrainzLookup.cs index b0a27eb..bab2826 100644 --- a/DeezNET/MusicBrainzLookup.cs +++ b/DeezNET/MusicBrainzLookup.cs @@ -14,7 +14,7 @@ public class MusicBrainzLookup public MusicBrainzLookup(HttpClient? client = null, string? userAgent = null) { _client = client ?? new HttpClient(); - _userAgent = userAgent ?? "DeezNET/1.2.4 (https://github.com/jasonpatrickellykrause/DeezNET)"; + _userAgent = userAgent ?? "DeezNET/1.2.5 (https://github.com/jasonpatrickellykrause/DeezNET)"; } /// diff --git a/DeezNET/PublicApi.cs b/DeezNET/PublicApi.cs index c2f23b0..f5e8974 100644 --- a/DeezNET/PublicApi.cs +++ b/DeezNET/PublicApi.cs @@ -363,7 +363,10 @@ public async Task SearchTrack(string query, bool strict = false, SearchO public async Task SearchUser(string query, bool strict = false, SearchOrder? order = null, int index = 0, int limit = 25, CancellationToken token = default) => await Call("search/user", PublicApi.GenerateSearchArgs(query, strict, order, index, limit), token); - private async Task Call(string method, Dictionary? parameters = null, CancellationToken token = default) + private const int MAX_QUOTA_RETRIES = 3; + private static readonly TimeSpan QUOTA_RETRY_DELAY = TimeSpan.FromSeconds(5); + + private async Task Call(string method, Dictionary? parameters = null, CancellationToken token = default, int quotaRetries = 0) { parameters ??= []; @@ -397,8 +400,12 @@ private async Task Call(string method, Dictionary? param case 4: case 700: { - await Task.Delay(5000, token); - return await Call(method, parameters, token); + // quota exceeded; retrying without a limit kept a caller waiting forever while Deezer kept refusing + if (quotaRetries >= MAX_QUOTA_RETRIES) + throw new APIException($"Deezer's public API refused {method} with error {err} after {MAX_QUOTA_RETRIES} retries: {message}"); + + await Task.Delay(QUOTA_RETRY_DELAY, token); + return await Call(method, parameters, token, quotaRetries + 1); } case 100: throw new ItemsLimitExceededException(message ?? ""); diff --git a/README.md b/README.md index db43852..780c6b3 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,11 @@ The main differences from upstream: ## Changelog +### 1.2.5 +**Fixes** +- `PublicApi` retries a quota error (codes 4 and 700) up to three times, then throws `APIException`. Before, it retried every 5 seconds with no limit, so a caller waited until Deezer stopped refusing. +- Tagging no longer fails or stalls when the public API can't return the album's genres. The track is tagged without genres. Tagging calls the public API only for genres, once per track. + ### 1.2.4 **Fixes** - Track downloads stream from the CDN instead of loading the whole file before decrypting it. Before, `HttpClient`'s 100-second timeout covered the entire download, so large FLACs on slow connections failed, and `GetRawTrackStream` held the track in memory twice. A stalled connection now fails with `TimeoutException` after `Downloader.ReadTimeout` (60 seconds by default) without data.