From a9ed1cf4a838f114263652faeb8b88ee99b916af Mon Sep 17 00:00:00 2001 From: Scrub <72096833+ScrubN@users.noreply.github.com> Date: Mon, 18 Sep 2023 22:49:13 -0400 Subject: [PATCH] Fix incorrect message.bits_spent for chats between v5 shutdown and 1.51.2 (#817) --- TwitchDownloaderCore/Chat/ChatJson.cs | 14 ++++++++++++++ TwitchDownloaderCore/ChatDownloader.cs | 6 +----- TwitchDownloaderCore/Tools/TwitchRegex.cs | 3 +++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/TwitchDownloaderCore/Chat/ChatJson.cs b/TwitchDownloaderCore/Chat/ChatJson.cs index d301921e..1eeb3f4f 100644 --- a/TwitchDownloaderCore/Chat/ChatJson.cs +++ b/TwitchDownloaderCore/Chat/ChatJson.cs @@ -8,6 +8,7 @@ using System.Threading; using System.Threading.Tasks; using TwitchDownloaderCore.Extensions; +using TwitchDownloaderCore.Tools; using TwitchDownloaderCore.TwitchObjects; namespace TwitchDownloaderCore.Chat @@ -145,6 +146,19 @@ private static async ValueTask UpgradeChatJson(ChatRoot chatRoot) chatRoot.video.end = chatRoot.video.length; chatRoot.video.duration = null; } + + // Fix incorrect bits_spent value on chats between v5 shutdown and the lay295#520 fix + if (chatRoot.comments.All(c => c.message.bits_spent == 0)) + { + foreach (var comment in chatRoot.comments) + { + var bitMatch = TwitchRegex.BitsRegex.Match(comment.message.body); + if (bitMatch.Success && int.TryParse(bitMatch.ValueSpan, out var result)) + { + comment.message.bits_spent = result; + } + } + } } /// diff --git a/TwitchDownloaderCore/ChatDownloader.cs b/TwitchDownloaderCore/ChatDownloader.cs index e4f2a0dc..a0932324 100644 --- a/TwitchDownloaderCore/ChatDownloader.cs +++ b/TwitchDownloaderCore/ChatDownloader.cs @@ -5,7 +5,6 @@ using System.Net.Http; using System.Net.Http.Json; using System.Text; -using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using TwitchDownloaderCore.Chat; @@ -25,9 +24,6 @@ public sealed class ChatDownloader BaseAddress = new Uri("https://gql.twitch.tv/gql"), DefaultRequestHeaders = { { "Client-ID", "kd1unb4b3q4t58fwlpcbzcbnm76a8fp" } } }; - private static readonly Regex BitsRegex = new( - @"(?<=(?:\s|^)(?:4Head|Anon|Bi(?:bleThumb|tBoss)|bday|C(?:h(?:eer|arity)|orgo)|cheerwal|D(?:ansGame|oodleCheer)|EleGiggle|F(?:rankerZ|ailFish)|Goal|H(?:eyGuys|olidayCheer)|K(?:appa|reygasm)|M(?:rDestructoid|uxy)|NotLikeThis|P(?:arty|ride|JSalt)|RIPCheer|S(?:coops|h(?:owLove|amrock)|eemsGood|wiftRage|treamlabs)|TriHard|uni|VoHiYo))[1-9]\d?\d?\d?\d?\d?\d?(?=\s|$)", - RegexOptions.Compiled); private enum DownloadType { @@ -231,7 +227,7 @@ private static List ConvertComments(CommentVideo video, ChatFormat form message.body = bodyStringBuilder.ToString(); - var bitMatch = BitsRegex.Match(message.body); + var bitMatch = TwitchRegex.BitsRegex.Match(message.body); if (bitMatch.Success && int.TryParse(bitMatch.ValueSpan, out var result)) { message.bits_spent = result; diff --git a/TwitchDownloaderCore/Tools/TwitchRegex.cs b/TwitchDownloaderCore/Tools/TwitchRegex.cs index 8905a2e2..4005d5b5 100644 --- a/TwitchDownloaderCore/Tools/TwitchRegex.cs +++ b/TwitchDownloaderCore/Tools/TwitchRegex.cs @@ -11,6 +11,9 @@ public static class TwitchRegex private static readonly Regex ClipId = new(@"(?<=^|(?:clips\.)?twitch\.tv\/(?:\w+\/clip\/)?)[\w-]+?(?=$|\?|\s)", RegexOptions.Compiled); public static readonly Regex UrlTimeCode = new(@"(?<=(?:\?|&)t=)\d+h\d+m\d+s(?=$|\?|\s)", RegexOptions.Compiled); + public static readonly Regex BitsRegex = new( + @"(?<=(?:\s|^)(?:4Head|Anon|Bi(?:bleThumb|tBoss)|bday|C(?:h(?:eer|arity)|orgo)|cheerwal|D(?:ansGame|oodleCheer)|EleGiggle|F(?:rankerZ|ailFish)|Goal|H(?:eyGuys|olidayCheer)|K(?:appa|reygasm)|M(?:rDestructoid|uxy)|NotLikeThis|P(?:arty|ride|JSalt)|RIPCheer|S(?:coops|h(?:owLove|amrock)|eemsGood|wiftRage|treamlabs)|TriHard|uni|VoHiYo))[1-9]\d?\d?\d?\d?\d?\d?(?=\s|$)", + RegexOptions.Compiled); /// A of the video's id or . public static Match MatchVideoId(string text)