Skip to content

Commit

Permalink
Cleanup (#1168)
Browse files Browse the repository at this point in the history
* Don't send chat download requests as arrays

* Use EnumerationOptions instead of regex matching
  • Loading branch information
ScrubN authored Aug 1, 2024
1 parent c8be760 commit 810387b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
16 changes: 8 additions & 8 deletions TwitchDownloaderCore/ChatDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static async Task<List<Comment>> DownloadSection(Range downloadRange, st
{
cancellationToken.ThrowIfCancellationRequested();

GqlCommentResponse[] commentResponse;
GqlCommentResponse commentResponse;
try
{
var request = new HttpRequestMessage()
Expand All @@ -68,20 +68,20 @@ private static async Task<List<Comment>> DownloadSection(Range downloadRange, st
if (isFirst)
{
request.Content = new StringContent(
"[{\"operationName\":\"VideoCommentsByOffsetOrCursor\",\"variables\":{\"videoID\":\"" + videoId + "\",\"contentOffsetSeconds\":" + videoStart + "},\"extensions\":{\"persistedQuery\":{\"version\":1,\"sha256Hash\":\"b70a3591ff0f4e0313d126c6a1502d79a1c02baebb288227c582044aa76adf6a\"}}}]",
"{\"operationName\":\"VideoCommentsByOffsetOrCursor\",\"variables\":{\"videoID\":\"" + videoId + "\",\"contentOffsetSeconds\":" + videoStart + "},\"extensions\":{\"persistedQuery\":{\"version\":1,\"sha256Hash\":\"b70a3591ff0f4e0313d126c6a1502d79a1c02baebb288227c582044aa76adf6a\"}}}",
Encoding.UTF8, "application/json");
}
else
{
request.Content = new StringContent(
"[{\"operationName\":\"VideoCommentsByOffsetOrCursor\",\"variables\":{\"videoID\":\"" + videoId + "\",\"cursor\":\"" + cursor + "\"},\"extensions\":{\"persistedQuery\":{\"version\":1,\"sha256Hash\":\"b70a3591ff0f4e0313d126c6a1502d79a1c02baebb288227c582044aa76adf6a\"}}}]",
"{\"operationName\":\"VideoCommentsByOffsetOrCursor\",\"variables\":{\"videoID\":\"" + videoId + "\",\"cursor\":\"" + cursor + "\"},\"extensions\":{\"persistedQuery\":{\"version\":1,\"sha256Hash\":\"b70a3591ff0f4e0313d126c6a1502d79a1c02baebb288227c582044aa76adf6a\"}}}",
Encoding.UTF8, "application/json");
}

using (var httpResponse = await HttpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false))
{
httpResponse.EnsureSuccessStatusCode();
commentResponse = await httpResponse.Content.ReadFromJsonAsync<GqlCommentResponse[]>(options: null, cancellationToken);
commentResponse = await httpResponse.Content.ReadFromJsonAsync<GqlCommentResponse>(options: null, cancellationToken);
}

errorCount = 0;
Expand All @@ -97,13 +97,13 @@ private static async Task<List<Comment>> DownloadSection(Range downloadRange, st
continue;
}

if (commentResponse[0].data.video.comments?.edges is null)
if (commentResponse.data.video.comments?.edges is null)
{
// video.comments can be null for some dumb reason, skip
continue;
}

var convertedComments = ConvertComments(commentResponse[0].data.video, format);
var convertedComments = ConvertComments(commentResponse.data.video, format);
foreach (var comment in convertedComments)
{
if (comment.content_offset_seconds >= videoStart && comment.content_offset_seconds < videoEnd)
Expand All @@ -117,10 +117,10 @@ private static async Task<List<Comment>> DownloadSection(Range downloadRange, st
}
}

if (!commentResponse[0].data.video.comments.pageInfo.hasNextPage)
if (!commentResponse.data.video.comments.pageInfo.hasNextPage)
break;

cursor = commentResponse[0].data.video.comments.edges.Last().cursor;
cursor = commentResponse.data.video.comments.edges.Last().cursor;

var percent = (int)Math.Floor((latestMessage - videoStart) / videoDuration * 100);
progress.Report(percent);
Expand Down
9 changes: 3 additions & 6 deletions TwitchDownloaderCore/TwitchHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -680,13 +680,11 @@ public static async Task<Dictionary<string, SKBitmap>> GetEmojis(string cacheFol
return returnCache;

var emojiFolder = Path.Combine(cacheFolder, "emojis", emojiVendor.EmojiFolder());
var emojiExtensions = new Regex(@"\.(?:png|PNG)$", RegexOptions.RightToLeft); // Extensions are case sensitive on Linux and Mac

if (!Directory.Exists(emojiFolder))
CreateDirectory(emojiFolder);

var emojiFiles = Directory.GetFiles(emojiFolder)
.Where(i => emojiExtensions.IsMatch(i)).ToArray();
var enumerationOptions = new EnumerationOptions { MatchType = MatchType.Simple, MatchCasing = MatchCasing.CaseInsensitive };
var emojiFiles = Directory.GetFiles(emojiFolder, "*.png", enumerationOptions);

if (emojiFiles.Length < emojiVendor.EmojiCount())
{
Expand Down Expand Up @@ -719,8 +717,7 @@ public static async Task<Dictionary<string, SKBitmap>> GetEmojis(string cacheFol
}
}

emojiFiles = Directory.GetFiles(emojiFolder)
.Where(i => emojiExtensions.IsMatch(i)).ToArray();
emojiFiles = Directory.GetFiles(emojiFolder, "*.png", enumerationOptions);
}
finally
{
Expand Down

0 comments on commit 810387b

Please sign in to comment.