code cleanup

This commit is contained in:
2026-02-10 21:59:30 -05:00
parent 6ae35ec636
commit 4f34872f10
5 changed files with 97 additions and 91 deletions

View File

@@ -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})
}