-
Notifications
You must be signed in to change notification settings - Fork 499
/
douyin.go
405 lines (368 loc) · 10.7 KB
/
douyin.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package douyin
import (
"fmt"
"net/http"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"github.com/hr3lxphr6j/requests"
"github.com/tidwall/gjson"
"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.douyin.com"
cnName = "抖音"
randomCookieChars = "1234567890abcdef"
roomIdCatcherRegex = `{\\"webrid\\":\\"([^"]+)\\"}`
mainInfoLineCatcherRegex = `self.__pace_f.push\(\[1,\s*"[^:]*:([^<]*,null,\{\\"state\\"[^<]*\])\\n"\]\)`
commonInfoLineCatcherRegex = `self.__pace_f.push\(\[1,\s*\"(\{.*\})\"\]\)`
)
var roomInfoApiForSprintf = "https://live.douyin.com/webcast/room/web/enter/?aid=6383&app_name=douyin_web&live_id=1&device_platform=web&language=zh-CN&browser_language=zh-CN&browser_platform=Win32&browser_name=Chrome&browser_version=116.0.0.0&web_rid=%s"
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...),
responseCookies: make(map[string]string),
isUsingLegacy: false,
}, nil
}
func createRandomCookie() string {
return utils.GenRandomString(21, randomCookieChars)
}
func createRandomOdintt() string {
return utils.GenRandomString(160, randomCookieChars)
}
type Live struct {
internal.BaseLive
responseCookies map[string]string
LastAvailableStringUrlInfos []live.StreamUrlInfo
isUsingLegacy bool
}
func (l *Live) getLiveRoomWebPageResponse() (body string, err error) {
cookies := l.Options.Cookies.Cookies(l.Url)
cookieKVs := make(map[string]string)
cookieKVs["__ac_nonce"] = createRandomCookie()
for key, value := range l.responseCookies {
cookieKVs[key] = value
}
for _, item := range cookies {
cookieKVs[item.Name] = item.Value
}
// proxy, _ := url.Parse("http://localhost:8888")
requestSession := requests.NewSession(&http.Client{
// Transport: &http.Transport{
// Proxy: http.ProxyURL(proxy),
// },
})
req, err := requests.NewRequest(
http.MethodGet,
l.Url.String(),
live.CommonUserAgent,
requests.Cookies(cookieKVs),
requests.Headers(map[string]interface{}{
"Cache-Control": "no-cache",
}),
)
if err != nil {
return
}
cookieWithOdinTt := fmt.Sprintf("odin_tt=%s; %s", createRandomOdintt(), req.Header.Get("Cookie"))
req.Header.Set("Cookie", cookieWithOdinTt)
resp, err := requestSession.Do(req)
if err != nil {
return
}
switch code := resp.StatusCode; code {
case http.StatusOK:
for _, cookie := range resp.Cookies() {
l.responseCookies[cookie.Name] = cookie.Value
}
default:
err = fmt.Errorf("failed to get page, code: %v, %w", code, live.ErrInternalError)
return
}
body, err = resp.Text()
return
}
func getMainInfoLine(body string) (json *gjson.Result, err error) {
reg, err := regexp.Compile(mainInfoLineCatcherRegex)
if err != nil {
return
}
match := reg.FindAllStringSubmatch(body, -1)
if match == nil {
err = fmt.Errorf("0 match for mainInfoLineCatcherRegex: %s", mainInfoLineCatcherRegex)
return
}
for _, item := range match {
if len(item) < 2 {
// err = fmt.Errorf("len(item) = %d", len(item))
continue
}
mainInfoLine := item[1]
// 获取房间信息
mainJson := gjson.Parse(fmt.Sprintf(`"%s"`, mainInfoLine))
if !mainJson.Exists() {
// err = fmt.Errorf(errorMessageForErrorf+". Invalid json: %s", stepNumberForLog, mainInfoLine)
continue
}
mainJson = gjson.Parse(mainJson.String()).Get("3")
if !mainJson.Exists() {
// err = fmt.Errorf(errorMessageForErrorf+". Main json does not exist: %s", stepNumberForLog, mainInfoLine)
continue
}
if mainJson.Get("state.roomStore.roomInfo.room.status_str").Exists() {
json = &mainJson
return
}
}
return nil, fmt.Errorf("MainInfoLine not found")
}
func (l *Live) getRoomInfoFromBody(body string) (info *live.Info, streamUrlInfos []live.StreamUrlInfo, err error) {
const errorMessageForErrorf = "getRoomInfoFromBody() failed on step %d"
stepNumberForLog := 1
mainJson, err := getMainInfoLine(body)
if err != nil {
err = fmt.Errorf(errorMessageForErrorf+". %s", stepNumberForLog, err.Error())
return
}
isStreaming := mainJson.Get("state.roomStore.roomInfo.room.status_str").String() == "2"
info = &live.Info{
Live: l,
HostName: mainJson.Get("state.roomStore.roomInfo.anchor.nickname").String(),
RoomName: mainJson.Get("state.roomStore.roomInfo.room.title").String(),
Status: isStreaming,
}
if !isStreaming {
return
}
stepNumberForLog++
// 获取直播流信息
streamIdPath := "state.streamStore.streamData.H264_streamData.common.stream"
streamId := mainJson.Get(streamIdPath).String()
if streamId == "" {
err = fmt.Errorf(errorMessageForErrorf+". %s does not exist", stepNumberForLog, streamIdPath)
return
}
stepNumberForLog++
streamUrlInfos = make([]live.StreamUrlInfo, 0, 4)
reg2, err := regexp.Compile(commonInfoLineCatcherRegex)
if err != nil {
return
}
match2 := reg2.FindAllStringSubmatch(body, -1)
if match2 == nil {
err = fmt.Errorf(errorMessageForErrorf, stepNumberForLog)
return
}
stepNumberForLog++
for _, item := range match2 {
if len(item) < 2 {
err = fmt.Errorf(errorMessageForErrorf+". len(item) = %d", stepNumberForLog, len(item))
return
}
commonJson := gjson.Parse(gjson.Parse(fmt.Sprintf(`"%s"`, item[1])).String())
if !commonJson.Exists() {
err = fmt.Errorf(errorMessageForErrorf+". Not valid json: %s", stepNumberForLog, item[1])
return
}
if !commonJson.Get("common").Exists() {
continue
}
commonStreamId := commonJson.Get("common.stream").String()
if commonStreamId == "" {
err = fmt.Errorf(errorMessageForErrorf+". No valid common stream ID: %s", stepNumberForLog, item[1])
return
}
if commonStreamId != streamId {
continue
}
commonJson.Get("data").ForEach(func(key, value gjson.Result) bool {
flv := value.Get("main.flv").String()
var Url *url.URL
Url, err = url.Parse(flv)
if err != nil {
return true
}
paramsString := value.Get("main.sdk_params").String()
paramsJson := gjson.Parse(paramsString)
var description strings.Builder
paramsJson.ForEach(func(key, value gjson.Result) bool {
description.WriteString(key.String())
description.WriteString(": ")
description.WriteString(value.String())
description.WriteString("\n")
return true
})
Resolution := 0
resolution := strings.Split(paramsJson.Get("resolution").String(), "x")
if len(resolution) == 2 {
x, err := strconv.Atoi(resolution[0])
if err != nil {
return true
}
y, err := strconv.Atoi(resolution[1])
if err != nil {
return true
}
Resolution = x * y
}
Vbitrate := int(paramsJson.Get("vbitrate").Int())
streamUrlInfos = append(streamUrlInfos, live.StreamUrlInfo{
Name: key.String(),
Description: description.String(),
Url: Url,
Resolution: Resolution,
Vbitrate: Vbitrate,
})
return true
})
}
sort.Slice(streamUrlInfos, func(i, j int) bool {
if streamUrlInfos[i].Resolution != streamUrlInfos[j].Resolution {
return streamUrlInfos[i].Resolution > streamUrlInfos[j].Resolution
} else {
return streamUrlInfos[i].Vbitrate > streamUrlInfos[j].Vbitrate
}
})
stepNumberForLog++
return
}
func (l *Live) GetInfo() (info *live.Info, err error) {
l.isUsingLegacy = false
body, err := l.getLiveRoomWebPageResponse()
if err != nil {
l.LastAvailableStringUrlInfos = nil
return
}
var streamUrlInfos []live.StreamUrlInfo
info, streamUrlInfos, err = l.getRoomInfoFromBody(body)
if err == nil {
l.LastAvailableStringUrlInfos = streamUrlInfos
return
}
// fallback
l.isUsingLegacy = true
return l.legacy_GetInfo(body)
}
func (l *Live) GetStreamUrls() (us []*url.URL, err error) {
if !l.isUsingLegacy {
if l.LastAvailableStringUrlInfos != nil {
us = make([]*url.URL, 0, len(l.LastAvailableStringUrlInfos))
for _, urlInfo := range l.LastAvailableStringUrlInfos {
us = append(us, urlInfo.Url)
}
return
}
return nil, fmt.Errorf("failed douyin GetStreamUrls()")
} else {
return l.legacy_GetStreamUrls()
}
}
func (l *Live) GetPlatformCNName() string {
return cnName
}
// ================ legacy functions ================
func (l *Live) legacy_getRoomId(body string) (string, error) {
roomId := utils.Match1(roomIdCatcherRegex, body)
if roomId == "" {
return "", fmt.Errorf("failed to get RoomId from page, %w", live.ErrInternalError)
}
return roomId, nil
}
func (l *Live) legacy_GetStreamUrls() (us []*url.URL, err error) {
var body string
body, err = l.getLiveRoomWebPageResponse()
if err != nil {
l.LastAvailableStringUrlInfos = nil
return
}
data, err := l.legacy_getRoomInfo(body)
if err != nil {
return nil, err
}
var urls []string
data.Get("data.0.stream_url.flv_pull_url").ForEach(func(key, value gjson.Result) bool {
urls = append(urls, value.String())
return true
})
streamData := gjson.Parse(data.Get("data.0.stream_url.live_core_sdk_data.pull_data.stream_data").String())
if streamData.Exists() {
url := streamData.Get("origin.main.flv")
if url.Exists() {
urls = append([]string{url.String()}, urls...)
}
}
return utils.GenUrls(urls...)
}
func (l *Live) legacy_getRoomInfo(body string) (*gjson.Result, error) {
roomId, err := l.legacy_getRoomId(body)
if err != nil {
return nil, err
}
cookies := l.Options.Cookies.Cookies(l.Url)
cookieKVs := make(map[string]string)
cookieKVs["__ac_nonce"] = createRandomCookie()
for _, item := range cookies {
cookieKVs[item.Name] = item.Value
}
for key, value := range l.responseCookies {
cookieKVs[key] = value
}
roomInfoApi := fmt.Sprintf(roomInfoApiForSprintf, roomId)
resp, err := requests.Get(
roomInfoApi,
live.CommonUserAgent,
requests.Cookies(cookieKVs),
requests.Headers(map[string]interface{}{
"Cache-Control": "no-cache",
}))
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
}
result := gjson.Parse(body)
return &result, nil
}
func (l *Live) legacy_GetInfo(body string) (info *live.Info, err error) {
data, err := l.legacy_getRoomInfo(body)
if err != nil {
return nil, err
}
nickname := data.Get("data.user.nickname").String()
title := data.Get("data.data.0.title").String()
if title == "" {
title = nickname
}
isLiving := false
statusJson := data.Get("data.data.0.status")
if statusJson.Exists() {
isLiving = statusJson.Int() == 2
} else {
isLiving = data.Get("data.room_status").Int() == 0
}
info = &live.Info{
Live: l,
HostName: nickname,
RoomName: title,
Status: isLiving,
}
return
}