Compare commits
5 Commits
6ae35ec636
...
v1-refacto
| Author | SHA1 | Date | |
|---|---|---|---|
|
600fde2e71
|
|||
|
a2a0aabb3a
|
|||
|
bd643344ef
|
|||
|
63a6c38079
|
|||
|
4f34872f10
|
105
app/download.go
Normal file
105
app/download.go
Normal 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, tempDir 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, tempDir, 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
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
11
app/go.mod
11
app/go.mod
@@ -2,12 +2,19 @@ module git.dubyatp.xyz/williamp/yt-dlp-bot
|
||||
|
||||
go 1.25.2
|
||||
|
||||
//replace github.com/lrstanley/go-ytdlp => /home/williamp/go-ytdlp
|
||||
|
||||
replace github.com/lrstanley/go-ytdlp => github.com/dubyatp/go-ytdlp v0.0.0-20260213041320-010f95f0f1d3
|
||||
|
||||
require (
|
||||
github.com/bwmarrin/discordgo v0.29.0
|
||||
github.com/lrstanley/go-ytdlp v1.2.7
|
||||
)
|
||||
|
||||
require (
|
||||
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/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/lrstanley/go-ytdlp v1.2.7 // indirect
|
||||
github.com/ulikunitz/xz v0.5.13 // indirect
|
||||
golang.org/x/crypto v0.41.0 // indirect
|
||||
golang.org/x/sys v0.35.0 // indirect
|
||||
|
||||
12
app/go.sum
12
app/go.sum
@@ -4,10 +4,16 @@ github.com/bwmarrin/discordgo v0.29.0 h1:FmWeXFaKUwrcL3Cx65c20bTRW+vOb6k8AnaP+Eg
|
||||
github.com/bwmarrin/discordgo v0.29.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||
github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0=
|
||||
github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dubyatp/go-ytdlp v0.0.0-20260213041320-010f95f0f1d3 h1:tGzvzV6fSzybhIpwU+wjvjOSq8RBNtHENm+G03E+mgo=
|
||||
github.com/dubyatp/go-ytdlp v0.0.0-20260213041320-010f95f0f1d3/go.mod h1:38IL64XM6gULrWtKTiR0+TTNCVbxesNSbTyaFG2CGTI=
|
||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/lrstanley/go-ytdlp v1.2.7 h1:YNDvKkd0OCJSZLZePZvJwcirBCfL8Yw3eCwrTCE5w7Q=
|
||||
github.com/lrstanley/go-ytdlp v1.2.7/go.mod h1:38IL64XM6gULrWtKTiR0+TTNCVbxesNSbTyaFG2CGTI=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/ulikunitz/xz v0.5.13 h1:ar98gWrjf4H1ev05fYP/o29PDZw9DrI3niHtnEqyuXA=
|
||||
github.com/ulikunitz/xz v0.5.13/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
@@ -20,3 +26,5 @@ golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
155
app/main.go
155
app/main.go
@@ -6,165 +6,26 @@ 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)
|
||||
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() {
|
||||
|
||||
out_dir := os.Getenv("OUT_PATH")
|
||||
temp_dir := os.Getenv("TEMP_PATH")
|
||||
bot_token := os.Getenv("DISCORD_TOKEN")
|
||||
|
||||
if out_dir == "" {
|
||||
panic("No output dir specified")
|
||||
}
|
||||
|
||||
if temp_dir == "" {
|
||||
panic("No temp dir specified")
|
||||
}
|
||||
|
||||
s, err := discordgo.New("Bot " + bot_token)
|
||||
if err != nil {
|
||||
log.Fatalf("Invalid bot parameters: %v", err)
|
||||
@@ -392,7 +253,7 @@ func main() {
|
||||
go func() {
|
||||
// Small delay to ensure response is sent first
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
startAsyncDownload(s, i, state.URL, state.VideoFormatID, state.AudioFormatID, out_dir)
|
||||
startAsyncDownload(s, i, state.URL, state.VideoFormatID, state.AudioFormatID, out_dir, temp_dir)
|
||||
}()
|
||||
|
||||
// Clean up state after starting download
|
||||
@@ -631,8 +492,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})
|
||||
}
|
||||
|
||||
6
app/misc.go
Normal file
6
app/misc.go
Normal 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
27
app/state.go
Normal 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
60
app/types.go
Normal 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
|
||||
}
|
||||
47
app/ytdlp.go
47
app/ytdlp.go
@@ -3,49 +3,17 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"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
|
||||
}
|
||||
var ytdlpBinary = os.Getenv("YTDLP_BIN")
|
||||
|
||||
func GetFormats(url string) (*FormatOptions, error) {
|
||||
dl := ytdlp.New().
|
||||
SetExecutable(ytdlpBinary).
|
||||
SkipDownload().
|
||||
DumpJSON()
|
||||
|
||||
@@ -127,13 +95,18 @@ func GetFormats(url string) (*FormatOptions, error) {
|
||||
return formatOpts, nil
|
||||
}
|
||||
|
||||
func downloadVideo(out_dir, url string, opts DownloadOptions, progressChan chan<- ProgressUpdate) {
|
||||
func DownloadVideo(out_dir, temp_dir, url string, opts DownloadOptions, progressChan chan<- ProgressUpdate) {
|
||||
defer close(progressChan)
|
||||
|
||||
homePath := "home:" + out_dir
|
||||
tempPath := "temp:" + temp_dir
|
||||
|
||||
var lastPhase string
|
||||
|
||||
dl := ytdlp.New().
|
||||
SetWorkDir(out_dir).
|
||||
SetExecutable(ytdlpBinary).
|
||||
Paths(homePath).
|
||||
Paths(tempPath).
|
||||
RecodeVideo("mp4").
|
||||
ProgressFunc(100*time.Millisecond, func(prog ytdlp.ProgressUpdate) {
|
||||
// Detect phase transition -- differentiate "downloading" as the main download
|
||||
|
||||
Reference in New Issue
Block a user