From 8c67bb67d226eb4bb9ea38aff53866a7f0027883 Mon Sep 17 00:00:00 2001 From: Popeye Lau Date: Wed, 22 Feb 2023 18:57:09 +0800 Subject: [PATCH] =?UTF-8?q?youtube=20=E6=94=B9=E4=B8=BA=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Golang/liveurls/youtube.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Golang/liveurls/youtube.go b/Golang/liveurls/youtube.go index 2e1b82b..e4193ba 100644 --- a/Golang/liveurls/youtube.go +++ b/Golang/liveurls/youtube.go @@ -8,14 +8,15 @@ package liveurls import ( + "bytes" "fmt" + "github.com/tidwall/gjson" "io" "net/http" "strconv" "sync" "time" - "github.com/dlclark/regexp2" "github.com/etherlabsio/go-m3u8/m3u8" ) @@ -40,22 +41,23 @@ func (y *Youtube) GetLiveUrl() any { }, //Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}, } - r, _ := http.NewRequest("GET", fmt.Sprintf("https://m.youtube.com/watch?v=%v", y.Rid), nil) - r.Header.Add("user-agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 16_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Mobile/15E148 Safari/604.1") + + json := []byte(fmt.Sprintf(`{"context": {"client": {"hl": "en","clientVersion": "2.20210721.00.00","clientName": "WEB"}},"videoId": "%s"}`, y.Rid)) + reqBody := bytes.NewBuffer(json) + r, _ := http.NewRequest("POST", "https://www.youtube.com/youtubei/v1/player", reqBody) + r.Header.Add("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36") resp, err := client.Do(r) if err != nil { return err } defer resp.Body.Close() body, _ := io.ReadAll(resp.Body) - str := string(body) - reg := regexp2.MustCompile(`(?<=hlsManifestUrl":").*\.m3u8`, regexp2.RE2) - res, _ := reg.FindStringMatch(str) - if res == nil { + stream := gjson.GetBytes(body, "streamingData.hlsManifestUrl").String() + if len(stream) < 1 { return nil } - stream := res.Captures[0].String() + quality := y.getResolution(stream) if quality != nil { return *quality