Compare commits

..

3 Commits

Author SHA1 Message Date
bd643344ef direct requirements 2026-02-11 20:40:43 -05:00
63a6c38079 create download.go for async download function 2026-02-10 22:00:34 -05:00
4f34872f10 code cleanup 2026-02-10 21:59:30 -05:00
7 changed files with 204 additions and 187 deletions

105
app/download.go Normal file
View File

@@ -0,0 +1,105 @@
package main
import (
"fmt"
"log"
"github.com/bwmarrin/discordgo"
)
// startAsyncDownload initiates a download in a goroutine and handles progress updates
func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, url, videoFormatID, audioFormatID, outputDir string) {
progressChan := make(chan ProgressUpdate, 1)
resultChan := make(chan DownloadResult, 1)
// Start download in goroutine
go func() {
defer close(resultChan)
defer func() {
if r := recover(); r != nil {
// Handle panic from DownloadVideo
resultChan <- DownloadResult{
Success: false,
Message: fmt.Sprintf("Download failed: %v", r),
URL: url,
Format: fmt.Sprintf("video: %s, audio: %s", videoFormatID, audioFormatID),
Error: fmt.Errorf("%v", r),
}
}
}()
// Call DownloadVideo (it panics on error instead of returning error)
DownloadVideo(outputDir, url, DownloadOptions{
EmbedThumbnail: true,
IncludeSubtitles: true,
VideoFormatID: videoFormatID,
AudioFormatID: audioFormatID,
}, progressChan)
// If we reach here, download was successful
resultChan <- DownloadResult{
Success: true,
Message: "Video Downloaded Successfully!",
URL: url,
Format: fmt.Sprintf("video: %s, audio: %s", videoFormatID, audioFormatID),
Error: nil,
}
}()
// Handle progress and results asynchronously
go func() {
// First update the original ephemeral message with "Processing..."
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Content: ptr(fmt.Sprintf("🔄 Processing download...\nURL: %s\nVideo: %s\nAudio: %s", url, videoFormatID, audioFormatID)),
})
if err != nil {
log.Printf("Error updating interaction: %v", err)
}
for {
select {
case prog, ok := <-progressChan:
if !ok {
progressChan = nil
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 {
log.Printf("Error updating progress: %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
}
}
}()
}

View File

@@ -2,12 +2,15 @@ module git.dubyatp.xyz/williamp/yt-dlp-bot
go 1.25.2 go 1.25.2
require (
github.com/bwmarrin/discordgo v0.29.0
github.com/lrstanley/go-ytdlp v1.2.7
)
require ( require (
github.com/ProtonMail/go-crypto v1.3.0 // indirect github.com/ProtonMail/go-crypto v1.3.0 // indirect
github.com/bwmarrin/discordgo v0.29.0 // indirect
github.com/cloudflare/circl v1.6.1 // indirect github.com/cloudflare/circl v1.6.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect github.com/gorilla/websocket v1.4.2 // indirect
github.com/lrstanley/go-ytdlp v1.2.7 // indirect
github.com/ulikunitz/xz v0.5.13 // indirect github.com/ulikunitz/xz v0.5.13 // indirect
golang.org/x/crypto v0.41.0 // indirect golang.org/x/crypto v0.41.0 // indirect
golang.org/x/sys v0.35.0 // indirect golang.org/x/sys v0.35.0 // indirect

View File

@@ -6,156 +6,12 @@ import (
"os" "os"
"os/signal" "os/signal"
"sort" "sort"
"sync"
"syscall" "syscall"
"time" "time"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
// InteractionState holds the state for a specific interaction
type InteractionState struct {
URL string
FormatOptions *FormatOptions
VideoFormatID string
AudioFormatID string
}
// DownloadResult represents the result of an async download operation
type DownloadResult struct {
Success bool
Message string
URL string
Format string
Error error
}
// startAsyncDownload initiates a download in a goroutine and handles progress updates
func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, url, videoFormatID, audioFormatID, outputDir string) {
progressChan := make(chan ProgressUpdate, 1)
resultChan := make(chan DownloadResult, 1)
// Start download in goroutine
go func() {
defer close(resultChan)
defer func() {
if r := recover(); r != nil {
// Handle panic from downloadVideo
resultChan <- DownloadResult{
Success: false,
Message: fmt.Sprintf("Download failed: %v", r),
URL: url,
Format: fmt.Sprintf("video: %s, audio: %s", videoFormatID, audioFormatID),
Error: fmt.Errorf("%v", r),
}
}
}()
// Call downloadVideo (it panics on error instead of returning error)
downloadVideo(outputDir, url, DownloadOptions{
EmbedThumbnail: true,
IncludeSubtitles: true,
VideoFormatID: videoFormatID,
AudioFormatID: audioFormatID,
}, progressChan)
// If we reach here, download was successful
resultChan <- DownloadResult{
Success: true,
Message: "Video Downloaded Successfully!",
URL: url,
Format: fmt.Sprintf("video: %s, audio: %s", videoFormatID, audioFormatID),
Error: nil,
}
}()
// Handle progress and results asynchronously
go func() {
// First update the original ephemeral message with "Processing..."
_, err := s.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Content: ptr(fmt.Sprintf("🔄 Processing download...\nURL: %s\nVideo: %s\nAudio: %s", url, videoFormatID, audioFormatID)),
})
if err != nil {
log.Printf("Error updating interaction: %v", err)
}
for {
select {
case prog, ok := <-progressChan:
if !ok {
progressChan = nil
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 {
log.Printf("Error updating progress: %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
}
}
}()
}
// Helper function to create string pointer
func ptr(s string) *string {
return &s
}
// Global state management
var (
interactionStates = make(map[string]*InteractionState)
interactionStatesMutex = sync.RWMutex{}
)
func getInteractionState(token string) *InteractionState {
interactionStatesMutex.RLock()
defer interactionStatesMutex.RUnlock()
return interactionStates[token]
}
func setInteractionState(token string, state *InteractionState) {
interactionStatesMutex.Lock()
defer interactionStatesMutex.Unlock()
interactionStates[token] = state
}
func deleteInteractionState(token string) {
interactionStatesMutex.Lock()
defer interactionStatesMutex.Unlock()
delete(interactionStates, token)
}
func main() { func main() {
out_dir := os.Getenv("OUT_PATH") out_dir := os.Getenv("OUT_PATH")
@@ -631,8 +487,4 @@ func main() {
<-sc <-sc
s.Close() s.Close()
//var url string = "https://www.youtube.com/watch?v=WpBWSFF03eI"
//downloadVideo(out_dir, url, DownloadOptions{EmbedThumbnail: true, IncludeSubtitles: true})
} }

6
app/misc.go Normal file
View File

@@ -0,0 +1,6 @@
package main
// Helper function to create string pointer
func ptr(s string) *string {
return &s
}

27
app/state.go Normal file
View File

@@ -0,0 +1,27 @@
package main
import "sync"
// Global state management
var (
interactionStates = make(map[string]*InteractionState)
interactionStatesMutex = sync.RWMutex{}
)
func getInteractionState(token string) *InteractionState {
interactionStatesMutex.RLock()
defer interactionStatesMutex.RUnlock()
return interactionStates[token]
}
func setInteractionState(token string, state *InteractionState) {
interactionStatesMutex.Lock()
defer interactionStatesMutex.Unlock()
interactionStates[token] = state
}
func deleteInteractionState(token string) {
interactionStatesMutex.Lock()
defer interactionStatesMutex.Unlock()
delete(interactionStates, token)
}

60
app/types.go Normal file
View File

@@ -0,0 +1,60 @@
package main
import (
"time"
"github.com/lrstanley/go-ytdlp"
)
type DownloadOptions struct {
EmbedThumbnail bool
IncludeSubtitles bool
VideoFormatID string
AudioFormatID string
}
type VideoOption struct {
Height *int `json:"height,omitempty"`
Resolution string `json:"resolution,omitempty"`
FormatID string `json:"format_id"`
Ext string `json:"ext"`
TBR *float64 `json:"tbr,omitempty"`
}
type AudioOption struct {
Format string `json:"format"`
FormatID string `json:"format_id"`
Ext string `json:"ext"`
TBR *float64 `json:"tbr,omitempty"`
Language *string `json:"language,omitempty"`
}
type FormatOptions struct {
VideoOptions []VideoOption `json:"video_options"`
AudioOptions []AudioOption `json:"audio_options"`
}
type ProgressUpdate struct {
Status ytdlp.ProgressStatus
Percent string
ETA time.Duration
Filename string
Phase string
}
// InteractionState holds the state for a specific interaction
type InteractionState struct {
URL string
FormatOptions *FormatOptions
VideoFormatID string
AudioFormatID string
}
// DownloadResult represents the result of an async download operation
type DownloadResult struct {
Success bool
Message string
URL string
Format string
Error error
}

View File

@@ -8,42 +8,6 @@ import (
"github.com/lrstanley/go-ytdlp" "github.com/lrstanley/go-ytdlp"
) )
type DownloadOptions struct {
EmbedThumbnail bool
IncludeSubtitles bool
VideoFormatID string
AudioFormatID string
}
type VideoOption struct {
Height *int `json:"height,omitempty"`
Resolution string `json:"resolution,omitempty"`
FormatID string `json:"format_id"`
Ext string `json:"ext"`
TBR *float64 `json:"tbr,omitempty"`
}
type AudioOption struct {
Format string `json:"format"`
FormatID string `json:"format_id"`
Ext string `json:"ext"`
TBR *float64 `json:"tbr,omitempty"`
Language *string `json:"language,omitempty"`
}
type FormatOptions struct {
VideoOptions []VideoOption `json:"video_options"`
AudioOptions []AudioOption `json:"audio_options"`
}
type ProgressUpdate struct {
Status ytdlp.ProgressStatus
Percent string
ETA time.Duration
Filename string
Phase string
}
func GetFormats(url string) (*FormatOptions, error) { func GetFormats(url string) (*FormatOptions, error) {
dl := ytdlp.New(). dl := ytdlp.New().
SkipDownload(). SkipDownload().
@@ -127,7 +91,7 @@ func GetFormats(url string) (*FormatOptions, error) {
return formatOpts, nil return formatOpts, nil
} }
func downloadVideo(out_dir, url string, opts DownloadOptions, progressChan chan<- ProgressUpdate) { func DownloadVideo(out_dir, url string, opts DownloadOptions, progressChan chan<- ProgressUpdate) {
defer close(progressChan) defer close(progressChan)
var lastPhase string var lastPhase string