From 93e6b1b3715cda767c5da758db440d7650e4d254 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Mon, 30 Dec 2024 13:11:17 +0200 Subject: [PATCH 1/2] Warm up system playlists if they report themselves unavailable on the first request Related to #854 --- .../Playlists/PlaylistController.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/YoutubeExplode/Playlists/PlaylistController.cs b/YoutubeExplode/Playlists/PlaylistController.cs index 9d43bf98..117d4053 100644 --- a/YoutubeExplode/Playlists/PlaylistController.cs +++ b/YoutubeExplode/Playlists/PlaylistController.cs @@ -99,10 +99,27 @@ await response.Content.ReadAsStringAsync(cancellationToken) if (!playlistResponse.IsAvailable) { // Retry if this is not the first request, meaning that the previous requests were successful, - // and that the playlist is probably not actually unavailable. + // indicating that it's most likely a transient error. if (index > 0 && !string.IsNullOrWhiteSpace(visitorData) && retriesRemaining > 0) continue; + // Some system playlists are unavailable through this endpoint until their page is opened by + // at least one user. Retry if this is the first request and we haven't retried yet. + if (index <= 0 && string.IsNullOrWhiteSpace(visitorData) && retriesRemaining >= 5) + { + using ( + await http.GetAsync( + $"https://youtube.com/playlist?list={playlistId}", + cancellationToken + ) + ) + { + // We don't actually care about the outcome of this request + } + + continue; + } + throw new PlaylistUnavailableException( $"Playlist '{playlistId}' is not available." ); From a069f6aab5c0edb60d6b78e9be670a395b3a29df Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Mon, 30 Dec 2024 13:15:17 +0200 Subject: [PATCH 2/2] Review comments --- YoutubeExplode/Playlists/PlaylistController.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/YoutubeExplode/Playlists/PlaylistController.cs b/YoutubeExplode/Playlists/PlaylistController.cs index 117d4053..f94adbdb 100644 --- a/YoutubeExplode/Playlists/PlaylistController.cs +++ b/YoutubeExplode/Playlists/PlaylistController.cs @@ -61,7 +61,8 @@ public async ValueTask GetPlaylistNextResponseAsync( CancellationToken cancellationToken = default ) { - for (var retriesRemaining = 5; ; retriesRemaining--) + const int retriesCount = 5; + for (var retriesRemaining = retriesCount; ; retriesRemaining--) { using var request = new HttpRequestMessage( HttpMethod.Post, @@ -104,8 +105,9 @@ await response.Content.ReadAsStringAsync(cancellationToken) continue; // Some system playlists are unavailable through this endpoint until their page is opened by - // at least one user. Retry if this is the first request and we haven't retried yet. - if (index <= 0 && string.IsNullOrWhiteSpace(visitorData) && retriesRemaining >= 5) + // at least one user. If this is the first request, and we haven't retried yet, attempt to + // warm up the playlist by opening its page, and then retry. + if (index <= 0 && string.IsNullOrWhiteSpace(visitorData) && retriesRemaining >= retriesCount) { using ( await http.GetAsync(