Skip to content

Commit

Permalink
Update video info if possible when updating chats
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Oct 15, 2023
1 parent 81579a6 commit f42c2d2
Showing 1 changed file with 76 additions and 2 deletions.
78 changes: 76 additions & 2 deletions TwitchDownloaderCore/ChatUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using TwitchDownloaderCore.Options;
using TwitchDownloaderCore.Tools;
using TwitchDownloaderCore.TwitchObjects;
using TwitchDownloaderCore.TwitchObjects.Gql;

namespace TwitchDownloaderCore
{
Expand Down Expand Up @@ -36,10 +37,13 @@ public async Task UpdateAsync(IProgress<ProgressReport> progress, CancellationTo

// Dynamic step count setup
int currentStep = 0;
int totalSteps = 1;
int totalSteps = 2;
if (_updateOptions.CropBeginning || _updateOptions.CropEnding) totalSteps++;
if (_updateOptions.EmbedMissing || _updateOptions.ReplaceEmbeds) totalSteps++;

currentStep++;
await UpdateVideoInfo(totalSteps, currentStep, progress, cancellationToken);

// If we are editing the chat crop
if (_updateOptions.CropBeginning || _updateOptions.CropEnding)
{
Expand Down Expand Up @@ -74,6 +78,76 @@ public async Task UpdateAsync(IProgress<ProgressReport> progress, CancellationTo
}
}

private async Task UpdateVideoInfo(int totalSteps, int currentStep, IProgress<ProgressReport> progress, CancellationToken cancellationToken)
{
progress.Report(new ProgressReport(ReportType.SameLineStatus, $"Updating Video Info [{currentStep}/{totalSteps}]"));
progress.Report(new ProgressReport(currentStep * 100 / totalSteps));

if (chatRoot.video.id.All(char.IsDigit))
{
var videoId = int.Parse(chatRoot.video.id);
VideoInfo videoInfo = null;
try
{
videoInfo = (await TwitchHelper.GetVideoInfo(videoId)).data.video;
}
catch { /* Eat the exception */ }

if (videoInfo is null)
{
progress.Report(new ProgressReport(ReportType.SameLineStatus, "Unable to fetch video info, deleted/expired VOD possibly?"));
return;
}

chatRoot.video.title = videoInfo.title;
chatRoot.video.description = videoInfo.description;
chatRoot.video.created_at = videoInfo.createdAt;
chatRoot.video.length = videoInfo.lengthSeconds;
chatRoot.video.viewCount = videoInfo.viewCount;
chatRoot.video.game = videoInfo.game.displayName;

var chaptersInfo = (await TwitchHelper.GetVideoChapters(videoId)).data.video.moments.edges;
foreach (var responseChapter in chaptersInfo)
{
chatRoot.video.chapters.Add(new VideoChapter
{
id = responseChapter.node.id,
startMilliseconds = responseChapter.node.positionMilliseconds,
lengthMilliseconds = responseChapter.node.durationMilliseconds,
_type = responseChapter.node._type,
description = responseChapter.node.description,
subDescription = responseChapter.node.subDescription,
thumbnailUrl = responseChapter.node.thumbnailURL,
gameId = responseChapter.node.details.game?.id,
gameDisplayName = responseChapter.node.details.game?.displayName,
gameBoxArtUrl = responseChapter.node.details.game?.boxArtURL
});
}
}
else
{
var clipId = chatRoot.video.id;
Clip clipInfo = null;
try
{
clipInfo = (await TwitchHelper.GetClipInfo(clipId)).data.clip;
}
catch { /* Eat the exception */ }

if (clipInfo is null)
{
progress.Report(new ProgressReport(ReportType.SameLineStatus, "Unable to fetch clip info, deleted possibly?"));
return;
}

chatRoot.video.title = clipInfo.title;
chatRoot.video.created_at = clipInfo.createdAt;
chatRoot.video.length = clipInfo.durationSeconds;
chatRoot.video.viewCount = clipInfo.viewCount;
chatRoot.video.game = clipInfo.game.displayName;
}
}

private async Task UpdateChatCrop(int totalSteps, int currentStep, IProgress<ProgressReport> progress, CancellationToken cancellationToken)
{
progress.Report(new ProgressReport(ReportType.SameLineStatus, $"Updating Chat Crop [{currentStep}/{totalSteps}]"));
Expand All @@ -82,7 +156,7 @@ private async Task UpdateChatCrop(int totalSteps, int currentStep, IProgress<Pro
bool cropTaskVodExpired = false;
var cropTaskProgress = new Progress<ProgressReport>(report =>
{
if (((string)report.Data).ToLower().Contains("vod is expired"))
if (((string)report.Data).Contains("vod is expired", StringComparison.OrdinalIgnoreCase))
{
// If the user is moving both crops in one command, we only want to propagate a 'vod expired/id corrupt' report once
if (cropTaskVodExpired)
Expand Down

0 comments on commit f42c2d2

Please sign in to comment.