From 3b03fc42994a1862886868f1c8f82b23e03f59ce Mon Sep 17 00:00:00 2001 From: Popeye Lau Date: Fri, 17 Feb 2023 16:45:41 +0800 Subject: [PATCH] Add youtube.go --- Golang/liveurls/youtube.go | 48 ++++++++++++++++++++++++++++++++++++++ Golang/main.go | 4 ++++ main/youtube.m3u | 3 +++ 3 files changed, 55 insertions(+) create mode 100644 Golang/liveurls/youtube.go create mode 100644 main/youtube.m3u diff --git a/Golang/liveurls/youtube.go b/Golang/liveurls/youtube.go new file mode 100644 index 0000000..a31bbfa --- /dev/null +++ b/Golang/liveurls/youtube.go @@ -0,0 +1,48 @@ +// Package liveurls +// @Time:2023/02/17 16:32 +// @File:youtube.go +// @SoftWare:Goland +// @Author:Popeye +// @Contact:TG@popeyelau + +package liveurls + +import ( + "fmt" + "github.com/dlclark/regexp2" + "io" + "net/http" +) + +type Youtube struct { + //https://www.youtube.com/watch?v=cK4LemjoFd0 + //Rid: cK4LemjoFd0 + Rid string +} + +func (y *Youtube) GetLiveUrl() any { + //proxyUrl, err := url.Parse("http://127.0.0.1:8888") + client := &http.Client{ + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + }, + //Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}, + } + r, _ := http.NewRequest("GET", fmt.Sprintf("https://www.youtube.com/watch?v=%v", y.Rid), nil) + 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") + r.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") + 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 { + return nil + } + return res.Captures[0].String() +} diff --git a/Golang/main.go b/Golang/main.go index a876908..88507e1 100644 --- a/Golang/main.go +++ b/Golang/main.go @@ -63,6 +63,10 @@ func setupRouter(adurl string) *gin.Engine { biliobj.Quality = c.DefaultQuery("quality", "10000") biliobj.Line = c.DefaultQuery("line", "second") c.Redirect(http.StatusMovedPermanently, duanyan(adurl, biliobj.GetPlayUrl())) + case "youtube": + ytbObj := &liveurls.Youtube{} + ytbObj.Rid = rid + c.Redirect(http.StatusMovedPermanently, duanyan(adurl, ytbObj.GetLiveUrl())) } }) return r diff --git a/main/youtube.m3u b/main/youtube.m3u new file mode 100644 index 0000000..f4b8343 --- /dev/null +++ b/main/youtube.m3u @@ -0,0 +1,3 @@ +#EXTM3U +#EXTINF:-1,tvg-id="" tvg-name="" tvg-logo="" group-title="Youtube",平靜祥和的咖啡廳鋼琴曲 +http://192.168.10.1:35455/youtube/cK4LemjoFd0 \ No newline at end of file