Skip to content

Commit ffc59c7

Browse files
committed
olivetv: fix douyin
1 parent 28c4d9e commit ffc59c7

2 files changed

Lines changed: 31 additions & 47 deletions

File tree

foundation/olivetv/douyin.go

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package olivetv
22

33
import (
44
"errors"
5-
"fmt"
6-
"net/url"
75
"strings"
86
"time"
97

108
"github.com/go-olive/olive/foundation/olivetv/model"
11-
"github.com/go-olive/olive/foundation/olivetv/util"
9+
"github.com/imroc/req/v3"
1210
jsoniter "github.com/json-iterator/go"
11+
"github.com/tidwall/gjson"
1312
)
1413

1514
var (
@@ -35,60 +34,45 @@ func (this *douyin) Snap(tv *TV) error {
3534
return this.set(tv)
3635
}
3736

37+
const CHROME = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"
38+
3839
func (this *douyin) set(tv *TV) error {
39-
if tv.cookie == "" {
40-
return ErrCookieNotSet
41-
}
42-
req := &util.HttpRequest{
43-
URL: fmt.Sprintf("https://live.douyin.com/%s", tv.RoomID),
44-
Method: "GET",
45-
ResponseData: *new(string),
46-
ContentType: "application/json",
47-
Header: map[string]string{
48-
"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",
49-
"referer": "https://live.douyin.com/",
50-
"cookie": tv.cookie,
51-
},
52-
}
53-
var err error
54-
if err = req.Send(); err != nil {
55-
return err
40+
const douyincookie = `ttwid=1%7CcfLDfqNkk8o-IKEppAbVXFkCcglSlTQbXQ-sOIZqeT0%7C1693836521%7C5a64077ccdaa38c03827e07363efbb814bd9000c50c9e3247146015388928089; home_can_add_dy_2_desktop=%220%22; strategyABtestKey=%221693836522.123%22; stream_recommend_feed_params=%22%7B%5C%22cookie_enabled%5C%22%3Atrue%2C%5C%22screen_width%5C%22%3A1920%2C%5C%22screen_height%5C%22%3A1080%2C%5C%22browser_online%5C%22%3Atrue%2C%5C%22cpu_core_num%5C%22%3A8%2C%5C%22device_memory%5C%22%3A8%2C%5C%22downlink%5C%22%3A10%2C%5C%22effective_type%5C%22%3A%5C%224g%5C%22%2C%5C%22round_trip_time%5C%22%3A50%7D%22; FORCE_LOGIN=%7B%22videoConsumedRemainSeconds%22%3A180%7D; volume_info=%7B%22isUserMute%22%3Afalse%2C%22isMute%22%3Afalse%2C%22volume%22%3A0.5%7D; passport_csrf_token=c5b117b9f262acf5d07f6a29b0425ab4; passport_csrf_token_default=c5b117b9f262acf5d07f6a29b0425ab4; odin_tt=d490a5b68981828937351d90d52d717fdcacd3f038f0f0505efb8a203d40e9744b5fbefe9c259879d1bffcc91a39d608a958d6584b53bb2fa9b6cc07b10777baa725728e6fa2f25940f68182b681f731; VIDEO_FILTER_MEMO_SELECT=%7B%22expireTime%22%3A1694441331554%2C%22type%22%3A1%7D; IsDouyinActive=false; msToken=ctKJrLjrxRieZ2vWcv0ZCTcFAtLdtAMSSSBw85YkTNegSzakztz0H8UIiJSAZ0CKv1gq5EqxVUe8lt1XDBKjpQ-JGwfhNpN3iVyb1ntT45xSYT5c6g==`
41+
if tv.cookie == "" || strings.Contains(tv.cookie, "ac_nonce") {
42+
tv.cookie = douyincookie
5643
}
57-
resp := fmt.Sprint(req.ResponseData)
58-
splits := strings.Split(resp, `<script id="RENDER_DATA" type="application/json">`)
59-
if len(splits) < 2 {
60-
// 优化日志输出
61-
// return fmt.Errorf("fail to find url")
62-
return nil
63-
}
64-
resp = splits[1]
65-
resp = strings.Split(resp, `</script>`)[0]
66-
resp, err = url.QueryUnescape(resp)
44+
api := `https://live.douyin.com/webcast/room/web/enter/?aid=6383&live_id=1&device_platform=web&language=zh-CN&enter_from=web_live&cookie_enabled=true&screen_width=1536&screen_height=864&browser_language=zh-CN&browser_platform=Win32&browser_name=Chrome&browser_version=94.0.4606.81&room_id_str=&enter_source=&web_rid=` +
45+
tv.RoomID
46+
resp, err := req.C().R().
47+
SetHeaders(map[string]string{
48+
"User-Agent": CHROME,
49+
"referer": "https://live.douyin.com/",
50+
"cookie": tv.cookie,
51+
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
52+
"Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
53+
}).
54+
Get(api)
6755
if err != nil {
6856
return err
6957
}
70-
var autoGenerated model.DouyinAutoGenerated
71-
err = jsoniter.UnmarshalFromString(resp, &autoGenerated)
72-
if err != nil {
73-
return err
74-
}
75-
58+
// log.Println(api)
59+
text := resp.String()
60+
text = gjson.Get(text, "data.data.0").String()
7661
// 抖音 status == 2 代表是开播的状态
77-
if autoGenerated.App.InitialState.RoomStore.RoomInfo.Room.Status != 2 {
62+
if gjson.Get(text, "status").String() != "2" {
7863
return nil
7964
}
8065

81-
streamDataStr := autoGenerated.App.InitialState.RoomStore.RoomInfo.Room.StreamURL.LiveCoreSdkData.PullData.StreamData
66+
streamDataStr := gjson.Get(text, "stream_url.live_core_sdk_data.pull_data.stream_data").String()
8267
var streamData model.DouyinStreamData
8368
err = jsoniter.UnmarshalFromString(streamDataStr, &streamData)
8469
if err != nil {
85-
// log.Println(err.Error())
86-
return nil
70+
return err
8771
}
88-
tv.streamURL = streamData.Data.Origin.Main.Flv
72+
flv := streamData.Data.Origin.Main.Flv
73+
tv.streamURL = flv
8974
tv.roomOn = true
90-
91-
tv.roomName = autoGenerated.App.InitialState.RoomStore.RoomInfo.Room.Title
75+
tv.roomName = gjson.Get(text, "title").String()
9276

9377
return nil
9478
}

foundation/olivetv/douyin_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package olivetv_test
33
import (
44
"testing"
55

6-
tv "github.com/go-olive/olive/foundation/olivetv"
6+
"github.com/go-olive/olive/foundation/olivetv"
77
)
88

99
func TestDouyin_Snap(t *testing.T) {
10-
u := "https://live.douyin.com/278246244716"
11-
cookie := "__ac_nonce=062f60f640020c76e5b7c; __ac_signature=_02B4Z6wo00f011BxI-gAAIDAxKd45w5q9dNQUSdAALb1WEJMf.7Ma1NuqG0oiO7cYko5mx60CrOZ7DEMeiImZWCkLuxGYUV7nL8NBVxgyWeCnQdWfhttJNV21omp7bThIi8SVu-58ihu3EiU32;"
12-
dy, err := tv.NewWithURL(u, tv.SetCookie(cookie))
10+
u := "https://live.douyin.com/80017709309"
11+
// cookie := `__ac_nonce=06487158a00d5c2d4634c; __ac_signature=_02B4Z6wo00f01CjqXDgAAIDDeJ0YwjlhZKAoyliAAG7EcZEUW2MdeGRISr2fobBUEzpAtX24xyQL5JQQzCHcosKQXhCMWI0W4MacAadtfeEO0QnuggwfMl6vwiakM6a3ROFyKBXeWrGE1FRZ87;`
12+
dy, err := olivetv.NewWithURL(u)
1313
if err != nil {
1414
println(err.Error())
1515
return

0 commit comments

Comments
 (0)