WIP: refactor and release v1 #30

Draft
williamp wants to merge 16 commits from v1-refactor into master
5 changed files with 97 additions and 91 deletions
Showing only changes of commit 4f34872f10 - Show all commits

View File

@@ -6,30 +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 // startAsyncDownload initiates a download in a goroutine and handles progress updates
func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, url, videoFormatID, audioFormatID, outputDir string) { func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, url, videoFormatID, audioFormatID, outputDir string) {
progressChan := make(chan ProgressUpdate, 1) progressChan := make(chan ProgressUpdate, 1)
@@ -40,7 +22,7 @@ func startAsyncDownload(s *discordgo.Session, i *discordgo.InteractionCreate, ur
defer close(resultChan) defer close(resultChan)
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
// Handle panic from downloadVideo // Handle panic from DownloadVideo
resultChan <- DownloadResult{ resultChan <- DownloadResult{
Success: false, Success: false,
Message: fmt.Sprintf("Download failed: %v", r), 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) // Call DownloadVideo (it panics on error instead of returning error)
downloadVideo(outputDir, url, DownloadOptions{ DownloadVideo(outputDir, url, DownloadOptions{
EmbedThumbnail: true, EmbedThumbnail: true,
IncludeSubtitles: true, IncludeSubtitles: true,
VideoFormatID: videoFormatID, 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() { func main() {
out_dir := os.Getenv("OUT_PATH") out_dir := os.Getenv("OUT_PATH")
@@ -631,8 +584,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