/
kuaishou.go
115 lines (99 loc) · 2.92 KB
/
kuaishou.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package kuaishou
import (
"fmt"
"github.com/hr3lxphr6j/requests"
"github.com/tidwall/gjson"
"net/http"
"net/url"
"github.com/hr3lxphr6j/bililive-go/src/live"
"github.com/hr3lxphr6j/bililive-go/src/live/internal"
"github.com/hr3lxphr6j/bililive-go/src/pkg/utils"
)
const (
domain = "live.kuaishou.com"
cnName = "快手"
regRenderData = `window\.__INITIAL_STATE__ *= *(.*?) *; *\(`
)
func init() {
live.Register(domain, new(builder))
}
type builder struct{}
func (b *builder) Build(url *url.URL, opt ...live.Option) (live.Live, error) {
return &Live{
BaseLive: internal.NewBaseLive(url, opt...),
}, nil
}
type Live struct {
internal.BaseLive
}
func (l *Live) getData() (*gjson.Result, error) {
cookies := l.Options.Cookies.Cookies(l.Url)
cookieKVs := make(map[string]string)
for _, item := range cookies {
cookieKVs[item.Name] = item.Value
}
resp, err := requests.Get(l.Url.String(), live.CommonUserAgent, requests.Cookies(cookieKVs))
if err != nil {
return nil, err
}
switch code := resp.StatusCode; code {
case http.StatusOK:
case http.StatusNotFound:
return nil, live.ErrRoomNotExist
default:
return nil, fmt.Errorf("failed to get page, code: %v, %w", code, live.ErrInternalError)
}
body, err := resp.Text()
if err != nil {
return nil, err
}
rawData := utils.Match1(regRenderData, body)
if rawData == "" {
return nil, fmt.Errorf("failed to get RENDER_DATA from page, %w", live.ErrInternalError)
}
unescapedRawData, err := url.QueryUnescape(rawData)
if err != nil {
return nil, err
}
result := gjson.Parse(unescapedRawData)
return &result, nil
}
func (l *Live) GetInfo() (info *live.Info, err error) {
data, err := l.getData()
if err != nil {
return nil, err
}
info = &live.Info{
Live: l,
HostName: data.Get("liveroom.author.name").String(),
RoomName: data.Get("liveroom.liveStream.caption").String(),
Status: data.Get("liveroom.isLiving").Bool(),
}
return
}
func (l *Live) GetStreamUrls() (us []*url.URL, err error) {
data, err := l.getData()
if err != nil {
return nil, err
}
var urls []string
addr := ""
addr = "liveroom.liveStream.playUrls.0.adaptationSet.representation.0.url"
// 由于更高清晰度需要cookie,暂时无法传,先注释
//maxQuality := len(data.Get("liveroom.liveStream.playUrls.0.adaptationSet.representation").Array()) - 1
//if l.Options.Quality != 0 && maxQuality >= l.Options.Quality {
// addr = "liveroom.liveStream.playUrls.0.adaptationSet.representation." + strconv.Itoa(l.Options.Quality) + ".url"
//} else if l.Options.Quality != 0 {
// addr = "liveroom.liveStream.playUrls.0.adaptationSet.representation." + strconv.Itoa(maxQuality) + ".url"
//} else {
// addr = "liveroom.liveStream.playUrls.0.adaptationSet.representation.0.url"
//}
data.Get(addr).ForEach(func(key, value gjson.Result) bool {
urls = append(urls, value.String())
return true
})
return utils.GenUrls(urls...)
}
func (l *Live) GetPlatformCNName() string {
return cnName
}