@@ -50,6 +50,11 @@ type Client struct {
5050 client * clientInfo
5151
5252 consentID string
53+
54+ visitorId struct {
55+ value string
56+ updated time.Time
57+ }
5358}
5459
5560func (c * Client ) assureClient () {
@@ -605,7 +610,7 @@ func (c *Client) httpPost(ctx context.Context, url string, body interface{}) (*h
605610 req .Header .Set ("Content-Type" , "application/json" )
606611 req .Header .Set ("Accept" , "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" )
607612
608- if xgoogvisitorid , err := getVisitorId (); err != nil {
613+ if xgoogvisitorid , err := c . getVisitorId (); err != nil {
609614 return nil , err
610615 } else {
611616 req .Header .Set ("x-goog-visitor-id" , xgoogvisitorid )
@@ -625,36 +630,36 @@ func (c *Client) httpPost(ctx context.Context, url string, body interface{}) (*h
625630}
626631
627632var VisitorIdMaxAge = 10 * time .Hour
628- var VisitorId struct {
629- Id string
630- Ctime time.Time
631- }
632633
633- func getVisitorId () (string , error ) {
634- if VisitorId .Id != "" && time .Since (VisitorId .Ctime ) < VisitorIdMaxAge {
635- return VisitorId .Id , nil
634+ func (c * Client ) getVisitorId () (string , error ) {
635+ var err error
636+ if c .visitorId .value == "" || time .Since (c .visitorId .updated ) > VisitorIdMaxAge {
637+ err = c .refreshVisitorId ()
636638 }
637639
640+ return c .visitorId .value , err
641+ }
642+
643+ func (c * Client ) refreshVisitorId () error {
638644 const sep = "\n ytcfg.set("
639645
640- var req http.Request
641- req .Header = http.Header {}
642- req .Header .Set ("User-Agent" , "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.4 Safari/605.1.15" )
643- req .URL = & url.URL {}
644- req .URL .Host = "www.youtube.com"
645- req .URL .Scheme = "https"
646- resp , err := http .DefaultClient .Do (& req )
646+ req , err := http .NewRequest (http .MethodGet , "https://www.youtube.com" , nil )
647647 if err != nil {
648- return "" , err
648+ return err
649+ }
650+
651+ resp , err := c .httpDo (req )
652+ if err != nil {
653+ return err
649654 }
650655 defer resp .Body .Close ()
651656 data , err := io .ReadAll (resp .Body )
652657 if err != nil {
653- return "" , err
658+ return err
654659 }
655660 _ , data1 , found := strings .Cut (string (data ), sep )
656661 if ! found {
657- return "" , err
662+ return err
658663 }
659664 var value struct {
660665 InnertubeContext struct {
@@ -664,16 +669,15 @@ func getVisitorId() (string, error) {
664669 } `json:"INNERTUBE_CONTEXT"`
665670 }
666671 if err := json .NewDecoder (strings .NewReader (data1 )).Decode (& value ); err != nil {
667- return "" , err
672+ return err
668673 }
669674
670- if VisitorId . Id , err = url .PathUnescape (value .InnertubeContext .Client .VisitorData ); err != nil {
671- return "" , err
675+ if c . visitorId . value , err = url .PathUnescape (value .InnertubeContext .Client .VisitorData ); err != nil {
676+ return err
672677 }
673678
674- VisitorId .Ctime = time .Now ()
675-
676- return VisitorId .Id , nil
679+ c .visitorId .updated = time .Now ()
680+ return nil
677681}
678682
679683// httpPostBodyBytes reads the whole HTTP body and returns it
0 commit comments