-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
kuaishou.go
162 lines (144 loc) · 3.53 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package tv
import (
"io"
"net/http"
"strconv"
"strings"
"time"
"github.com/go-olive/tv/model"
"github.com/go-olive/tv/util"
)
const (
KsLiveDetailQuery = `
query LiveDetail($principalId: String) {
liveDetail(principalId: $principalId) {
liveStream
}
}
`
KsUserInfoQuery = `
query userInfoQuery($principalId: String) {
userInfo(principalId: $principalId) {
name
living
}
}
`
)
func init() {
registerSite("kuaishou", &kuaishou{})
}
type kuaishou struct {
base
}
func (this *kuaishou) Name() string {
return "快手"
}
func (this *kuaishou) Snap(tv *Tv) (err error) {
tv.Info = &Info{
Timestamp: time.Now().Unix(),
}
return this.setV2(tv)
}
func (this *kuaishou) setV1(tv *Tv) error {
if err := this.setRoomOnV1(tv); err != nil {
return err
}
return this.setStreamUrlV1(tv)
}
func (this *kuaishou) setStreamUrlV1(tv *Tv) error {
if tv.cookie == "" {
return ErrCookieNotSet
}
// if !tv.roomOn {
// return nil
// }
ksAG := new(model.KsLiveDetailAutoGenerated)
req := &util.HttpRequest{
URL: "https://live.kuaishou.com/graphql",
Method: "POST",
RequestData: map[string]interface{}{
"operationName": "LiveDetail",
"variables": map[string]string{
"principalId": tv.RoomID,
},
"query": KsLiveDetailQuery,
},
ResponseData: ksAG,
ContentType: "application/json",
Header: map[string]string{
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Edg/94.0.992.38",
"referer": "https://live.kuaishou.com/",
"cookie": tv.cookie,
},
}
if err := req.Send(); err != nil {
return err
}
if len(ksAG.Data.WebLiveDetail.LiveStream.PlayUrls) > 0 {
tv.roomOn = true
tv.streamUrl = ksAG.Data.WebLiveDetail.LiveStream.PlayUrls[0].URL
}
tv.roomName = ksAG.Data.WebLiveDetail.LiveStream.Caption
return nil
}
func (this *kuaishou) setRoomOnV1(tv *Tv) error {
if tv.cookie == "" {
return ErrCookieNotSet
}
ksAG := new(model.KsUserInfoAutoGenerated)
req := &util.HttpRequest{
URL: "https://live.kuaishou.com/graphql",
Method: "POST",
RequestData: map[string]interface{}{
"operationName": "userInfoQuery",
"variables": map[string]string{
"principalId": tv.RoomID,
},
"query": KsUserInfoQuery,
},
ResponseData: ksAG,
ContentType: "application/json",
Header: map[string]string{
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Edg/94.0.992.38",
"referer": "https://live.kuaishou.com/",
"cookie": tv.cookie,
},
}
if err := req.Send(); err != nil {
return err
}
tv.roomOn = ksAG.Data.UserInfo.Living
tv.streamerName = ksAG.Data.UserInfo.Name
return nil
}
func (this *kuaishou) setV2(tv *Tv) error {
if tv.cookie == "" {
return ErrCookieNotSet
}
req, err := http.NewRequest("GET", "https://live.kuaishou.com/profile/"+tv.RoomID, nil)
if err != nil {
return err
}
req.Header.Add("cookie", tv.cookie)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
content := string(body)
if !strings.Contains(content, "直播中") {
return nil
}
tv.streamerName, _ = util.Match(`title="([^"]+)" target="_blank"`, content)
tv.roomName, _ = util.Match(`title="([^"]+)" class="router-link-exact-active`, content)
tv.streamUrl, _ = util.Match(`"url":"([^"]+)"`, content)
tv.streamUrl, _ = strconv.Unquote(`"` + tv.streamUrl + `"`)
if tv.streamUrl != "" {
tv.roomOn = true
}
return nil
}