kkdai/youtube

GetStreamURL() Returns URLs That Are Forbidden (403)

thenhawke opened this issue · 5 comments

The URLs returned from GetStreamURL() have started throwing an HTTP 403 when accessed. Code I'm using is below. This has been working up until very recently, perhaps YouTube has changed the way the URL must be crafted slightly?

package main

import (
	"fmt"
	"log"

	"github.com/kkdai/youtube/v2"
)

func main() {
	client := youtube.Client{}

	//A few videos that 403
	//"djV11Xbc914"
	//"ZrOKjDZOtkA"
	//"ENXvZ9YRjbo"
	//"eFjjO_lhf9c"
	//"gRYZijLZR-Q"
	//"CxKWTzr-k6s"

	video, err := client.GetVideo("djV11Xbc914")
	if err != nil {
		log.Fatal(err)
	}

	formats := video.Formats.WithAudioChannels()

	url, err := client.GetStreamURL(video, &formats[0])
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(url)
}
cybre commented

The interesting thing I've noticed is that it only happens with music. Other types of videos don't have this problem.

same problem

cybre commented

So I've found the problem. The actions object name in /player/*/player_ias.vflset/en_US/base.js is now $z. Because of this, this regex will fail. We interpolate $z into the regex expression, but $ is a special character. I have fixed this by escaping the parameters with regexp.QuoteMeta:

regex, err := regexp.Compile(fmt.Sprintf("(?:a=)?%s\\.(%s|%s|%s)\\(a,(\\d+)\\)", regexp.QuoteMeta(string(obj)), regexp.QuoteMeta(reverseKey), regexp.QuoteMeta(spliceKey), regexp.QuoteMeta(swapKey)))

@corny Is this an acceptable fix? If so, I would gladly open a PR.

In other news, there is now an issue where youtube will randomly disconnect your streaming session :/

cybre commented

I'll go ahead and open that PR and we can discuss the fix there.

corny commented

Thanks a lot for figuring this out!