|
|
|
|
@ -9,20 +9,26 @@ package liveurls |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"fmt" |
|
|
|
|
"github.com/dlclark/regexp2" |
|
|
|
|
"io" |
|
|
|
|
"net/http" |
|
|
|
|
"strconv" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"github.com/dlclark/regexp2" |
|
|
|
|
"github.com/etherlabsio/go-m3u8/m3u8" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type Youtube struct { |
|
|
|
|
//https://www.youtube.com/watch?v=cK4LemjoFd0
|
|
|
|
|
//Rid: cK4LemjoFd0
|
|
|
|
|
Rid string |
|
|
|
|
Rid string |
|
|
|
|
Quality string |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (y *Youtube) GetLiveUrl() any { |
|
|
|
|
//proxyUrl, err := url.Parse("http://127.0.0.1:8888")
|
|
|
|
|
client := &http.Client{ |
|
|
|
|
Timeout: time.Second * 5, |
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error { |
|
|
|
|
return http.ErrUseLastResponse |
|
|
|
|
}, |
|
|
|
|
@ -44,5 +50,38 @@ func (y *Youtube) GetLiveUrl() any { |
|
|
|
|
if res == nil { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
return res.Captures[0].String() |
|
|
|
|
stream := res.Captures[0].String() |
|
|
|
|
quality := getResolution(stream, y.Quality) |
|
|
|
|
if quality != nil { |
|
|
|
|
return *quality |
|
|
|
|
} |
|
|
|
|
return stream |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func getResolution(liveurl string, quality string) *string { |
|
|
|
|
client := &http.Client{Timeout: time.Second * 5} |
|
|
|
|
r, _ := http.NewRequest("GET", liveurl, 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") |
|
|
|
|
resp, _ := client.Do(r) |
|
|
|
|
playlist, err := m3u8.Read(resp.Body) |
|
|
|
|
defer resp.Body.Close() |
|
|
|
|
if err != nil { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
size := playlist.ItemSize() |
|
|
|
|
|
|
|
|
|
if size < 1 { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mapping := map[string]string{} |
|
|
|
|
for _, item := range playlist.Playlists() { |
|
|
|
|
mapping[strconv.Itoa(item.Resolution.Height)] = item.URI |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if stream, ok := mapping[quality]; ok { |
|
|
|
|
return &stream |
|
|
|
|
} |
|
|
|
|
return &playlist.Playlists()[size-1].URI |
|
|
|
|
} |
|
|
|
|
|