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
+16 -7
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,16 +63,23 @@ 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",
phaseEmoji,
prog.Phase)
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Content: ptr(content),
})
if err != nil {
log.Printf("Error updating progress: %v", err)
} }
content := fmt.Sprintf("%s %s\n%s @ %s [eta: %s]\n📄 %s", } else {
phaseEmoji := "⏬"
content := fmt.Sprintf("%s %s: %s [eta: %s]\n📄 %s",
phaseEmoji, phaseEmoji,
prog.Phase, prog.Phase,
prog.Status, renderProgressBar(prog.PercentFloat),
prog.Percent,
prog.ETA, prog.ETA,
prog.Filename) prog.Filename)
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{ _, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
@@ -81,6 +88,8 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur
if err != nil { if err != nil {
log.Printf("Error updating progress: %v", err) 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)
}
+1
View File
@@ -37,6 +37,7 @@ type FormatOptions struct {
type ProgressUpdate struct { type ProgressUpdate struct {
Status ytdlp.ProgressStatus Status ytdlp.ProgressStatus
Percent string Percent string
PercentFloat float64
ETA time.Duration ETA time.Duration
Filename string Filename string
Phase string Phase string
+1
View File
@@ -126,6 +126,7 @@ func DownloadVideo(out_dir, temp_dir, url string, opts DownloadOptions, progress
progressChan <- ProgressUpdate{ progressChan <- ProgressUpdate{
Status: prog.Status, Status: prog.Status,
Percent: prog.PercentString(), Percent: prog.PercentString(),
PercentFloat: prog.Percent(),
ETA: prog.ETA(), ETA: prog.ETA(),
Filename: prog.Filename, Filename: prog.Filename,
Phase: phase, Phase: phase,