Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
Add retry logic to chat download
Browse files Browse the repository at this point in the history
  • Loading branch information
lay295 committed Jun 5, 2021
1 parent 8f984a5 commit 6c8cbf9
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions TwitchDownloaderCore/ChatDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public async Task DownloadAsync(IProgress<ProgressReport> progress, Cancellation
double videoStart = 0.0;
double videoEnd = 0.0;
double videoDuration = 0.0;
int errorCount = 0;

if (downloadType == DownloadType.Video)
{
Expand Down Expand Up @@ -76,10 +77,25 @@ public async Task DownloadAsync(IProgress<ProgressReport> progress, Cancellation
while (latestMessage < videoEnd)
{
string response;
if (isFirst)
response = await client.DownloadStringTaskAsync(String.Format("https://api.twitch.tv/v5/videos/{0}/comments?content_offset_seconds={1}", videoId, videoStart));
else
response = await client.DownloadStringTaskAsync(String.Format("https://api.twitch.tv/v5/videos/{0}/comments?cursor={1}", videoId, cursor));

try
{
if (isFirst)
response = await client.DownloadStringTaskAsync(String.Format("https://api.twitch.tv/v5/videos/{0}/comments?content_offset_seconds={1}", videoId, videoStart));
else
response = await client.DownloadStringTaskAsync(String.Format("https://api.twitch.tv/v5/videos/{0}/comments?cursor={1}", videoId, cursor));
errorCount = 0;
}
catch (WebException ex)
{
await Task.Delay(1000 * errorCount);
errorCount++;

if (errorCount >= 10)
throw ex;

continue;
}

CommentResponse commentResponse = JsonConvert.DeserializeObject<CommentResponse>(response);

Expand Down

0 comments on commit 6c8cbf9

Please sign in to comment.