Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions YoutubeExplode/Playlists/PlaylistController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public async ValueTask<PlaylistNextResponse> 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,
Expand Down Expand Up @@ -99,10 +100,28 @@ 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. 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(
$"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."
);
Expand Down
Loading