Skip to content

Commit 9f50a69

Browse files
author
Maximilian Brune
committed
Fix regex to find youtube json
1 parent e2448a6 commit 9f50a69

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

gomble/audiosources/youtube/youtube.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ type YoutubeVideo struct {
3333

3434
func NewYoutubeVideo(path string) (*YoutubeVideo, error) { // {{{
3535
var jsonstr string
36-
for len(jsonstr) == 0 {
36+
// The following loop is for trying up to 3 times to download the youtube page and find a specific json string in it. You might not believe but it doesn't always work on the first try...
37+
var i int
38+
for i=0; i < 3 && len(jsonstr) == 0; i++ {
3739
resp, err := http.Get(path)
3840
//resp, err := http.Get("https://www.youtube.com/watch?v=YO1GBsuzTWU")
3941
if err != nil {
@@ -58,16 +60,21 @@ func NewYoutubeVideo(path string) (*YoutubeVideo, error) { // {{{
5860
// return nil, fmt.Errorf("NewYoutubeVideo(%s:%d): %w", file, line, err)
5961
//}
6062
html := string(body)
61-
regex, err := regexp.Compile("ytplayer.config = {.*};ytplayer.load")
63+
regex, err := regexp.Compile("ytplayer.config = {.*};ytplayer.web_player_context_config")
6264
if err != nil {
6365
_, file, line, _ := runtime.Caller(0)
6466
return nil, fmt.Errorf("NewYoutubeVideo(%s:%d): %w", file, line, err)
6567
}
6668
jsonstr = regex.FindString(html)
6769
}
70+
if i == 3 {
71+
_, file, line, _ := runtime.Caller(0)
72+
return nil, fmt.Errorf("NewYoutubeVideo(%s:%d): Tried 3 times to Download and find ytplayer.config jsonstr. Done trying... ", file, line)
73+
}
6874

6975
// parse JSON
70-
jsonstr = jsonstr[18 : len(jsonstr)-14]
76+
jsonstr = jsonstr[18 : len(jsonstr)-35]
77+
7178
//file, err := os.OpenFile("youtube.json", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0777)
7279
//if err != nil {
7380
// _, file, line, _ := runtime.Caller(0)

0 commit comments

Comments
 (0)