Skip to content

Commit

Permalink
Fix incorrect message.bits_spent for chats between v5 shutdown and 1.…
Browse files Browse the repository at this point in the history
…51.2 (#817)
  • Loading branch information
ScrubN authored Sep 19, 2023
1 parent c1ccdfe commit a9ed1cf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
14 changes: 14 additions & 0 deletions TwitchDownloaderCore/Chat/ChatJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading;
using System.Threading.Tasks;
using TwitchDownloaderCore.Extensions;
using TwitchDownloaderCore.Tools;
using TwitchDownloaderCore.TwitchObjects;

namespace TwitchDownloaderCore.Chat
Expand Down Expand Up @@ -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;
}
}
}
}

/// <summary>
Expand Down
6 changes: 1 addition & 5 deletions TwitchDownloaderCore/ChatDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -231,7 +227,7 @@ private static List<Comment> 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;
Expand Down
3 changes: 3 additions & 0 deletions TwitchDownloaderCore/Tools/TwitchRegex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/// <returns>A <see cref="Match"/> of the video's id or <see langword="null"/>.</returns>
public static Match MatchVideoId(string text)
Expand Down

0 comments on commit a9ed1cf

Please sign in to comment.