Skip to content

Commit 81a6cf9

Browse files
committed
Enhance song downloading functionality: add support for YouTube cookies and update related methods
1 parent 688e81f commit 81a6cf9

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

internal/music/music.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/kkdai/youtube/v2"
99
"io"
1010
"log"
11+
"net/http"
1112
"os"
1213
"path/filepath"
1314
"sync"
@@ -30,14 +31,28 @@ type Song struct {
3031
}
3132

3233
// DownloadSong downloads the song from the given URL and saves it to a file. returns the file path to the downloaded song.
33-
func DownloadSong(url string) (filePath string, err error) {
34+
func DownloadSong(url string, ytbCookie string) (filePath string, err error) {
3435
err = os.MkdirAll("./songCache", os.ModePerm)
3536
if err != nil {
3637
return "", fmt.Errorf("error creating directory: %w", err)
3738
}
3839

39-
client := youtube.Client{}
40-
video, err := client.GetVideo(url)
40+
ytbClient := youtube.Client{}
41+
42+
if ytbCookie == "" {
43+
return "", errors.New("no cookies supplied")
44+
}
45+
46+
httpClient := http.Client{}
47+
req, err := http.NewRequest("GET", "", nil)
48+
if err != nil {
49+
return "", fmt.Errorf("error creating request: %w", err)
50+
}
51+
req.Header.Set("Cookie", ytbCookie)
52+
53+
ytbClient.HTTPClient = &httpClient
54+
55+
video, err := ytbClient.GetVideo(url)
4156

4257
if err != nil {
4358
return "", fmt.Errorf("error getting video: %w", err)
@@ -52,7 +67,7 @@ func DownloadSong(url string) (filePath string, err error) {
5267
// Get the best audio format
5368
audioFormat := video.Formats.WithAudioChannels()
5469

55-
stream, _, err := client.GetStream(video, &audioFormat[0])
70+
stream, _, err := ytbClient.GetStream(video, &audioFormat[0])
5671
if err != nil {
5772
panic(err)
5873
}
@@ -90,7 +105,7 @@ func DownloadSong(url string) (filePath string, err error) {
90105
}
91106

92107
// PlaySong plays the audio file using the voice connection. gid and cid are the guild and channel IDs.
93-
func (s *SongList) PlaySong(gid string, cid string, bot *bot.Bot) error {
108+
func (s *SongList) PlaySong(gid string, cid string, bot *bot.Bot, ytbCookie string) error {
94109
// check if the song list is empty
95110
if len(s.Songs) <= 0 {
96111
return fmt.Errorf("no songs in the list")
@@ -103,7 +118,7 @@ func (s *SongList) PlaySong(gid string, cid string, bot *bot.Bot) error {
103118
}
104119

105120
currSong := s.Songs[0]
106-
filePath, err := DownloadSong(currSong.Url)
121+
filePath, err := DownloadSong(currSong.Url, ytbCookie)
107122
if err != nil {
108123
return fmt.Errorf("error downloading song: %w", err)
109124
}
@@ -144,7 +159,7 @@ func (s *SongList) PlaySong(gid string, cid string, bot *bot.Bot) error {
144159

145160
// play the next song
146161
go func() {
147-
err = s.PlaySong(gid, cid, bot)
162+
err = s.PlaySong(gid, cid, bot, ytbCookie)
148163
if err != nil {
149164
fmt.Println("Error playing song:", err)
150165
}

internal/music/music_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package music
22

33
import (
4+
"github.com/joho/godotenv"
45
"os"
56
"testing"
67
)
78

89
func TestDownloadSong(t *testing.T) {
10+
err := godotenv.Load("../../.env")
11+
912
if err := os.Remove("songCache\\d3J3uJpCgos.mp3"); err != nil {
1013
if !os.IsNotExist(err) {
1114
t.Errorf("Failed to remove test file: %v", err)
1215
}
1316
}
1417

15-
song, err := DownloadSong("https://www.youtube.com/watch?v=d3J3uJpCgos&list=PLwCTYY94JxbZHrJ-anoUuFkNHSFQqe438&index=6&pp=gAQBiAQB8AUB")
18+
song, err := DownloadSong("https://www.youtube.com/watch?v=d3J3uJpCgos&list=PLwCTYY94JxbZHrJ-anoUuFkNHSFQqe438&index=6&pp=gAQBiAQB8AUB", os.Getenv("YOUTUBE_COOKIE"))
1619
if err != nil {
17-
return
20+
t.Errorf("Expected no error, got: %v", err)
1821
}
1922

2023
if song == "" {
@@ -27,7 +30,7 @@ func TestDownloadSong(t *testing.T) {
2730
}
2831

2932
func TestDownloadSongError(t *testing.T) {
30-
song, err := DownloadSong("https://www.youtube.com/watch?v=")
33+
song, err := DownloadSong("https://www.youtube.com/watch?v=", os.Getenv("YOUTUBE_COOKIE"))
3134
if err == nil {
3235
t.Errorf("Expected error, got nil")
3336
}
@@ -38,7 +41,7 @@ func TestDownloadSongError(t *testing.T) {
3841
}
3942

4043
func TestDownloadSongAlreadyExists(t *testing.T) {
41-
song, err := DownloadSong("https://www.youtube.com/watch?v=d3J3uJpCgos&list=PLwCTYY94JxbZHrJ-anoUuFkNHSFQqe438&index=6&pp=gAQBiAQB8AUB")
44+
song, err := DownloadSong("https://www.youtube.com/watch?v=d3J3uJpCgos&list=PLwCTYY94JxbZHrJ-anoUuFkNHSFQqe438&index=6&pp=gAQBiAQB8AUB", os.Getenv("YOUTUBE_COOKIE"))
4245
if err != nil {
4346
return
4447
}

internal/storage/settings.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Settings struct {
1616
NewsAPIToken string `json:"news_api_key"`
1717
YoutubeToken string `json:"youtube_api_key"`
1818
ChatHistoryFilePath string `json:"chat_history_file_path"`
19+
YoutubeCookies string `json:"youtube_cookies"`
1920
}
2021

2122
var Setting Settings

internal/tools/musicfunctions.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"log"
88
"untitled/internal/bot"
99
"untitled/internal/music"
10+
"untitled/internal/storage"
1011
)
1112

1213
type AddSongArgs struct {
@@ -143,7 +144,7 @@ func skipSong(call openai.ToolCall, s *map[string]map[string]*music.SongList, my
143144
currSongList.Songs = currSongList.Songs[1:]
144145
currSongList.Mu.Unlock()
145146

146-
err = currSongList.PlaySong(args.GID, args.CID, myBot)
147+
err = currSongList.PlaySong(args.GID, args.CID, myBot, storage.Setting.YoutubeCookies)
147148

148149
if err != nil {
149150
log.Printf("Error playing skipped song: %v", err)
@@ -174,7 +175,7 @@ func playSong(call openai.ToolCall, s *map[string]map[string]*music.SongList, my
174175
return `Cannot play song while already playing`, fmt.Errorf("cannot play song while already playing")
175176
}
176177

177-
err = currSongList.PlaySong(args.GID, args.CID, myBot)
178+
err = currSongList.PlaySong(args.GID, args.CID, myBot, storage.Setting.YoutubeCookies)
178179

179180
if err != nil {
180181
log.Printf("Error playing song: %v", err)

0 commit comments

Comments
 (0)