From 4f34872f10a86fde42bca57661249d4958a3d05c Mon Sep 17 00:00:00 2001 From: William P Date: Tue, 10 Feb 2026 21:59:30 -0500 Subject: [PATCH] code cleanup --- app/main.go | 57 +++---------------------------------------------- app/misc.go | 6 ++++++ app/state.go | 27 +++++++++++++++++++++++ app/types.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ app/ytdlp.go | 38 +-------------------------------- 5 files changed, 97 insertions(+), 91 deletions(-) create mode 100644 app/misc.go create mode 100644 app/state.go create mode 100644 app/types.go diff --git a/app/main.go b/app/main.go index a40de2e..77d8780 100644 --- a/app/main.go +++ b/app/main.go @@ -6,30 +6,12 @@ import ( "os" "os/signal" "sort" - "sync" "syscall" "time" "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) @@ -40,7 +22,7 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur defer close(resultChan) defer func() { if r := recover(); r != nil { - // Handle panic from downloadVideo + // Handle panic from DownloadVideo resultChan <- DownloadResult{ Success: false, Message: fmt.Sprintf("Download failed: %v", r), @@ -51,8 +33,8 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur } }() - // Call downloadVideo (it panics on error instead of returning error) - downloadVideo(outputDir, url, DownloadOptions{ + // Call DownloadVideo (it panics on error instead of returning error) + DownloadVideo(outputDir, url, DownloadOptions{ EmbedThumbnail: true, IncludeSubtitles: true, VideoFormatID: videoFormatID, @@ -127,35 +109,6 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur }() } -// 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() { out_dir := os.Getenv("OUT_PATH") @@ -631,8 +584,4 @@ func main() { <-sc s.Close() - - //var url string = "https://www.youtube.com/watch?v=WpBWSFF03eI" - - //downloadVideo(out_dir, url, DownloadOptions{EmbedThumbnail: true, IncludeSubtitles: true}) } diff --git a/app/misc.go b/app/misc.go new file mode 100644 index 0000000..2afa308 --- /dev/null +++ b/app/misc.go @@ -0,0 +1,6 @@ +package main + +// Helper function to create string pointer +func ptr(s string) *string { + return &s +} diff --git a/app/state.go b/app/state.go new file mode 100644 index 0000000..08220e2 --- /dev/null +++ b/app/state.go @@ -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) +} diff --git a/app/types.go b/app/types.go new file mode 100644 index 0000000..a0deeda --- /dev/null +++ b/app/types.go @@ -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 +} diff --git a/app/ytdlp.go b/app/ytdlp.go index 3d3a5ee..e388a57 100644 --- a/app/ytdlp.go +++ b/app/ytdlp.go @@ -8,42 +8,6 @@ import ( "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) { dl := ytdlp.New(). SkipDownload(). @@ -127,7 +91,7 @@ func GetFormats(url string) (*FormatOptions, error) { 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) var lastPhase string