Skip to content

Commit

Permalink
Better legacy chat support (#960)
Browse files Browse the repository at this point in the history
* Smarter broadcaster id assumption

* Recover video id and broadcast date/time if possible

* Cleanup

* Prevent throwing on bad int string parse, add additional name assumption before falling back to API request

* Oops
  • Loading branch information
ScrubN authored Jan 28, 2024
1 parent 90fd32f commit 8ad34bb
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions TwitchDownloaderCore/Chat/ChatJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,37 @@ private static async Task<JsonDocument> GetJsonDocumentAsync(Stream stream, stri
private static async Task UpgradeChatJson(ChatRoot chatRoot)
{
const int MAX_STREAM_LENGTH = 172_800; // 48 hours in seconds. https://help.twitch.tv/s/article/broadcast-guidelines

var firstComment = chatRoot.comments.FirstOrDefault();
var lastComment = chatRoot.comments.LastOrDefault();

chatRoot.video ??= new Video
{
start = (int)Math.Floor(chatRoot.comments.FirstOrDefault()?.content_offset_seconds ?? 0),
end = (int)Math.Ceiling(chatRoot.comments.LastOrDefault()?.content_offset_seconds ?? MAX_STREAM_LENGTH)
start = (int)Math.Floor(firstComment?.content_offset_seconds ?? 0),
end = (int)Math.Ceiling(lastComment?.content_offset_seconds ?? MAX_STREAM_LENGTH)
};

chatRoot.video.id ??= firstComment?.content_id;

if (chatRoot.video.created_at == default)
chatRoot.video.created_at = firstComment?.created_at - TimeSpan.FromSeconds(firstComment?.content_offset_seconds ?? 0) ?? default;

if (chatRoot.streamer is null)
{
var assumedId = int.Parse(chatRoot.video.user_id ?? chatRoot.comments.FirstOrDefault()?.channel_id ?? "0");
var assumedName = chatRoot.video.user_name ?? await TwitchHelper.GetStreamerName(assumedId);
var broadcaster = new Lazy<Comment>(() =>
chatRoot.comments
.Where(x => x.message.user_badges != null)
.FirstOrDefault(x => x.message.user_badges.Any(b => b._id.Equals("broadcaster"))));

if (!int.TryParse(chatRoot.video.user_id, out var assumedId))
{
if (!int.TryParse(broadcaster.Value?.commenter._id, out assumedId))
{
assumedId = 0;
}
}

var assumedName = chatRoot.video.user_name ?? broadcaster.Value?.commenter.display_name ?? await TwitchHelper.GetStreamerName(assumedId);

chatRoot.streamer = new Streamer { id = assumedId, name = assumedName };
}
Expand Down

0 comments on commit 8ad34bb

Please sign in to comment.