2 Commits

Author SHA1 Message Date
williamp 3cac63ba82 implement progress bar 2026-03-04 21:06:41 -05:00
williamp b3d779374b have progress messages edit each other 2026-03-04 20:26:31 -05:00
5 changed files with 53 additions and 29 deletions
+26 -17
View File
@@ -50,7 +50,7 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur
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{
Content: ptr(fmt.Sprintf("🔄 Processing download...\nURL: %s\nVideo: %s\nAudio: %s", url, videoFormatID, audioFormatID)), Content: ptr(fmt.Sprintf("Downloading video: %s", renderProgressBar(0))),
}) })
if err != nil { if err != nil {
log.Printf("Error updating interaction: %v", err) log.Printf("Error updating interaction: %v", err)
@@ -63,24 +63,33 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur
progressChan = nil progressChan = nil
continue continue
} }
// Update message w/ phase and real time progress
phaseEmoji := "⏬"
if prog.Phase == "post-processing" { if prog.Phase == "post-processing" {
phaseEmoji = "⚙️" phaseEmoji := "⚙️"
} content := fmt.Sprintf("%s %s",
content := fmt.Sprintf("%s %s\n%s @ %s [eta: %s]\n📄 %s", phaseEmoji,
phaseEmoji, prog.Phase)
prog.Phase, _, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
prog.Status, Content: ptr(content),
prog.Percent, })
prog.ETA, if err != nil {
prog.Filename) log.Printf("Error updating progress: %v", err)
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ }
Content: ptr(content), } else {
}) phaseEmoji := "⏬"
if err != nil { content := fmt.Sprintf("%s %s: %s [eta: %s]\n📄 %s",
log.Printf("Error updating progress: %v", err) phaseEmoji,
prog.Phase,
renderProgressBar(prog.PercentFloat),
prog.ETA,
prog.Filename)
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Content: ptr(content),
})
if err != nil {
log.Printf("Error updating progress: %v", err)
}
} }
case result := <-resultChan: case result := <-resultChan:
// Handle completion // Handle completion
if result.Success { if result.Success {
+2 -2
View File
@@ -224,7 +224,7 @@ func main() {
} }
if state == nil { if state == nil {
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseUpdateMessage,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: "Error: No video URL found. Please start over with /download.", Content: "Error: No video URL found. Please start over with /download.",
Flags: discordgo.MessageFlagsEphemeral, Flags: discordgo.MessageFlagsEphemeral,
@@ -262,7 +262,7 @@ func main() {
response = "I don't see a video here :(" response = "I don't see a video here :("
} }
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseUpdateMessage,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: response, Content: response,
Flags: discordgo.MessageFlagsEphemeral, Flags: discordgo.MessageFlagsEphemeral,
+13
View File
@@ -1,6 +1,19 @@
package main package main
import (
"fmt"
"strings"
)
// Helper function to create string pointer // Helper function to create string pointer
func ptr(s string) *string { func ptr(s string) *string {
return &s return &s
} }
const progressBarLength = 20
func renderProgressBar(percent float64) string {
filled := int(float64(progressBarLength) * percent / 100)
bar := strings.Repeat("█", filled) + strings.Repeat(" ", progressBarLength-filled)
return fmt.Sprintf("[%s] %.0f%%", bar, percent)
}
+6 -5
View File
@@ -35,11 +35,12 @@ type FormatOptions struct {
} }
type ProgressUpdate struct { type ProgressUpdate struct {
Status ytdlp.ProgressStatus Status ytdlp.ProgressStatus
Percent string Percent string
ETA time.Duration PercentFloat float64
Filename string ETA time.Duration
Phase string Filename string
Phase string
} }
// InteractionState holds the state for a specific interaction // InteractionState holds the state for a specific interaction
+6 -5
View File
@@ -124,11 +124,12 @@ func DownloadVideo(out_dir, temp_dir, url string, opts DownloadOptions, progress
lastPhase = phase lastPhase = phase
progressChan <- ProgressUpdate{ progressChan <- ProgressUpdate{
Status: prog.Status, Status: prog.Status,
Percent: prog.PercentString(), Percent: prog.PercentString(),
ETA: prog.ETA(), PercentFloat: prog.Percent(),
Filename: prog.Filename, ETA: prog.ETA(),
Phase: phase, Filename: prog.Filename,
Phase: phase,
} }
}). }).
Output("%(title)s.%(ext)s") Output("%(title)s.%(ext)s")