pass download status to discord
This commit is contained in:
73
app/main.go
73
app/main.go
@@ -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,31 +70,49 @@ 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
|
||||||
})
|
}
|
||||||
if err != nil {
|
// Update message w/ phase and real time progress
|
||||||
log.Printf("Error updating interaction: %v", err)
|
phaseEmoji := "⏬"
|
||||||
}
|
if prog.Phase == "post-processing" {
|
||||||
|
phaseEmoji = "⚙️"
|
||||||
// Send non-ephemeral completion message
|
}
|
||||||
_, err = s.FollowupMessageCreate(i.Interaction, false, &discordgo.WebhookParams{
|
content := fmt.Sprintf("%s %s\n%s @ %s [eta: %s]\n📄 %s",
|
||||||
Content: "📥 Video downloaded: " + result.URL,
|
phaseEmoji,
|
||||||
})
|
prog.Phase,
|
||||||
if err != nil {
|
prog.Status,
|
||||||
log.Printf("Error sending public completion message: %v", err)
|
prog.Percent,
|
||||||
}
|
prog.ETA,
|
||||||
} else {
|
prog.Filename)
|
||||||
// Update ephemeral message with error
|
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||||
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
Content: ptr(content),
|
||||||
Content: ptr("❌ Download failed: " + result.Message + "\nURL: " + result.URL + "\nAudio: " + result.Format),
|
})
|
||||||
})
|
if err != nil {
|
||||||
if err != nil {
|
log.Printf("Error updating progress: %v", err)
|
||||||
log.Printf("Error updating interaction: %v", err)
|
}
|
||||||
|
case result := <-resultChan:
|
||||||
|
// Handle completion
|
||||||
|
if result.Success {
|
||||||
|
_, err = s.FollowupMessageCreate(i.Interaction, false, &discordgo.WebhookParams{
|
||||||
|
Content: "📥 Video downloaded: " + result.URL,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error updating interaction: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
_, err = s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
|
||||||
|
Content: ptr("❌ Download failed: " + result.Message),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Error updating interaction: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
43
app/ytdlp.go
43
app/ytdlp.go
@@ -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")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user