Skip to content

Commit

Permalink
Add html5=1 parameter to avoid 404 responses
Browse files Browse the repository at this point in the history
closes #192
  • Loading branch information
corny committed May 18, 2021
1 parent f73d8e7 commit 6f5c9e4
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package youtube
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -40,19 +39,9 @@ func (c *Client) GetVideoContext(ctx context.Context, url string) (*Video, error
func (c *Client) videoFromID(ctx context.Context, id string) (*Video, error) {
// Circumvent age restriction to pretend access through googleapis.com
eurl := "https://youtube.googleapis.com/v/" + id
var retried bool
var errStatus ErrUnexpectedStatusCode

retry:
// get_video_info can fail sometimes:
// https://github.com/kkdai/youtube/issues/192
body, err := c.httpGetBodyBytes(ctx, "https://www.youtube.com/get_video_info?video_id="+id+"&eurl="+eurl)
body, err := c.httpGetBodyBytes(ctx, "https://www.youtube.com/get_video_info?video_id="+id+"&html5=1&eurl="+eurl)
if err != nil {
if !retried && errors.As(err, &errStatus) && int(errStatus) == http.StatusNotFound {
retried = true
goto retry
}

return nil, err
}

Expand Down Expand Up @@ -198,7 +187,13 @@ func (c *Client) httpDo(req *http.Request) (*http.Response, error) {
log.Println(req.Method, req.URL)
}

return client.Do(req)
res, err := client.Do(req)

if c.Debug && res != nil {
log.Println(res.Status)
}

return res, err
}

// httpGet does a HTTP GET request, checks the response to be a 200 OK and returns it
Expand Down

0 comments on commit 6f5c9e4

Please sign in to comment.