pass download status to discord

This commit is contained in:
2026-01-26 20:16:13 -05:00
parent 4bad5f72da
commit aa9dc58259
2 changed files with 80 additions and 36 deletions

View File

@@ -28,6 +28,7 @@ type DownloadResult struct {
// startAsyncDownload initiates a download in a goroutine and handles progress updates // startAsyncDownload initiates a download in a goroutine and handles progress updates
func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, url, audioFormat, outputDir string) { func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, url, audioFormat, outputDir string) {
progressChan := make(chan ProgressUpdate, 1)
resultChan := make(chan DownloadResult, 1) resultChan := make(chan DownloadResult, 1)
// Start download in goroutine // Start download in goroutine
@@ -47,7 +48,7 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur
}() }()
// Call downloadVideo (it panics on error instead of returning error) // Call downloadVideo (it panics on error instead of returning error)
downloadVideo(outputDir, url, DownloadOptions{EmbedThumbnail: true, IncludeSubtitles: true}) downloadVideo(outputDir, url, DownloadOptions{EmbedThumbnail: true, IncludeSubtitles: true}, progressChan)
// If we reach here, download was successful // If we reach here, download was successful
resultChan <- DownloadResult{ resultChan <- DownloadResult{
@@ -59,7 +60,7 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur
} }
}() }()
// Handle results asynchronously // Handle progress and results asynchronously
go func() { go func() {
// First update the original ephemeral message with "Processing..." // First update the original ephemeral message with "Processing..."
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ _, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
@@ -69,33 +70,51 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur
log.Printf("Error updating interaction: %v", err) log.Printf("Error updating interaction: %v", err)
} }
result := <-resultChan for {
select {
if result.Success { case prog, ok := <-progressChan:
// Update ephemeral message with completion status if !ok {
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ progressChan = nil
Content: ptr("✅ Download completed successfully!\nURL: " + result.URL + "\nAudio: " + result.Format), continue
}
// Update message w/ phase and real time progress
phaseEmoji := "⏬"
if prog.Phase == "post-processing" {
phaseEmoji = "⚙️"
}
content := fmt.Sprintf("%s %s\n%s @ %s [eta: %s]\n📄 %s",
phaseEmoji,
prog.Phase,
prog.Status,
prog.Percent,
prog.ETA,
prog.Filename)
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Content: ptr(content),
}) })
if err != nil { if err != nil {
log.Printf("Error updating interaction: %v", err) log.Printf("Error updating progress: %v", err)
} }
case result := <-resultChan:
// Send non-ephemeral completion message // Handle completion
if result.Success {
_, err = s.FollowupMessageCreate(i.Interaction, false, &discordgo.WebhookParams{ _, err = s.FollowupMessageCreate(i.Interaction, false, &discordgo.WebhookParams{
Content: "📥 Video downloaded: " + result.URL, Content: "📥 Video downloaded: " + result.URL,
}) })
if err != nil { if err != nil {
log.Printf("Error sending public completion message: %v", err) log.Printf("Error updating interaction: %v", err)
} }
} else { } else {
// Update ephemeral message with error
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ _, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Content: ptr("❌ Download failed: " + result.Message + "\nURL: " + result.URL + "\nAudio: " + result.Format), Content: ptr("❌ Download failed: " + result.Message),
}) })
if err != nil { if err != nil {
log.Printf("Error updating interaction: %v", err) log.Printf("Error updating interaction: %v", err)
} }
} }
return
}
}
}() }()
} }

View File

@@ -2,7 +2,6 @@ package main
import ( import (
"context" "context"
"fmt"
"time" "time"
"github.com/lrstanley/go-ytdlp" "github.com/lrstanley/go-ytdlp"
@@ -13,19 +12,45 @@ type DownloadOptions struct {
IncludeSubtitles bool IncludeSubtitles bool
} }
func downloadVideo(out_dir, url string, opts DownloadOptions) { type ProgressUpdate struct {
Status ytdlp.ProgressStatus
Percent string
ETA time.Duration
Filename string
Phase string
}
func downloadVideo(out_dir, url string, opts DownloadOptions, progressChan chan<- ProgressUpdate) {
defer close(progressChan)
var lastPhase string
dl := ytdlp.New(). dl := ytdlp.New().
SetWorkDir(out_dir). SetWorkDir(out_dir).
FormatSort("res,ext:mp4:m4a"). FormatSort("res,ext:mp4:m4a").
RecodeVideo("mp4"). RecodeVideo("mp4").
ProgressFunc(100*time.Millisecond, func(prog ytdlp.ProgressUpdate) { ProgressFunc(100*time.Millisecond, func(prog ytdlp.ProgressUpdate) {
fmt.Printf( // Detect phase transition -- differentiate "downloading" as the main download
"%s @ %s [eta: %s] :: %s\n", // and "post processing" when the file name changes, preventing it from appearing "reset"
prog.Status, phase := "downloading"
prog.PercentString(), if prog.Status == ytdlp.ProgressStatusDownloading && prog.Percent() == 0.0 {
prog.ETA(), // If we already had progress, it's likely post-processing
prog.Filename, if lastPhase == "downloading" {
) phase = "post-processing"
}
} else if prog.Status != ytdlp.ProgressStatusDownloading {
phase = "post-processing"
}
lastPhase = phase
progressChan <- ProgressUpdate{
Status: prog.Status,
Percent: prog.PercentString(),
ETA: prog.ETA(),
Filename: prog.Filename,
Phase: phase,
}
}). }).
Output("%(title)s.%(ext)s") Output("%(title)s.%(ext)s")