Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unthrottle streams for all formats #244

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (c *Client) GetStreamURL(video *Video, format *Format) (string, error) {
// GetStreamURLContext returns the url for a specific format with a context
func (c *Client) GetStreamURLContext(ctx context.Context, video *Video, format *Format) (string, error) {
if format.URL != "" {
return format.URL, nil
return c.unThrottle(ctx, video.ID, format.URL)
}

cipher := format.Cipher
Expand Down
42 changes: 39 additions & 3 deletions decipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,55 @@ func (c *Client) decipherURL(ctx context.Context, videoID string, cipher string)
}
query.Add(params.Get("sp"), string(bs))

query, err = c.decryptNParam(ctx, config, query)
if err != nil {
return "", err
}

uri.RawQuery = query.Encode()

return uri.String(), nil
}

func (c *Client) unThrottle(ctx context.Context, videoID string, urlString string) (string, error) {
config, err := c.getPlayerConfig(ctx, videoID)
if err != nil {
return "", err
}
uri, err := url.Parse(urlString)
if err != nil {
return "", err
}

query, err := c.decryptNParam(ctx, config, uri.Query())
if err != nil {
return "", err
}

uri.RawQuery = query.Encode()

return uri.String(), nil

}

func (c *Client) decryptNParam(ctx context.Context, config playerConfig, query url.Values) (url.Values, error) {

// decrypt n-parameter
nSig := query.Get("n")
if nSig != "" {
nDecoded, err := config.decodeNsig(nSig)
if err != nil {
return "", fmt.Errorf("unable to decode nSig: %w", err)
return nil, fmt.Errorf("unable to decode nSig: %w", err)
}
query.Set("n", nDecoded)
}

uri.RawQuery = query.Encode()
if c.Debug {
log.Printf("[nParam] n: %s; nDecoded: %s\nQuery: %v\n", nSig, query.Get("n"), query)
}

return query, nil

return uri.String(), nil
}

const (
Expand Down