Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better legacy chat support #960

Merged
merged 5 commits into from
Jan 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading