Skip to content

Commit

Permalink
Unthrottle streams for all formats
Browse files Browse the repository at this point in the history
The n-param potentially requires processing even if there's no
deciphering needed.
Refactor decipherURL to reuse decryptNParam on any url.

closes #230 #232
  • Loading branch information
elProxy authored and corny committed Apr 25, 2022
1 parent f363472 commit 8255605
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
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
40 changes: 37 additions & 3 deletions decipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,53 @@ 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
}

// see https://github.com/kkdai/youtube/pull/244
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 uri.String(), nil
return query, nil
}

const (
Expand Down

0 comments on commit 8255605

Please sign in to comment.