-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
douyin.go
91 lines (79 loc) · 2.04 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
package tv
import (
"errors"
"fmt"
"net/url"
"strings"
"time"
"github.com/go-olive/tv/util"
jsoniter "github.com/json-iterator/go"
)
var (
ErrCookieNotSet = errors.New("cookie not configured")
)
func init() {
registerSite("douyin", &douyin{})
}
type douyin struct {
base
}
func (this *douyin) Name() string {
return "抖音"
}
func (this *douyin) Snap(tv *Tv) error {
tv.Info = &Info{
Timestamp: time.Now().Unix(),
}
return this.set(tv)
}
func (this *douyin) set(tv *Tv) error {
if tv.cookie == "" {
return ErrCookieNotSet
}
req := &util.HttpRequest{
URL: fmt.Sprintf("https://live.douyin.com/%s", tv.RoomID),
Method: "GET",
ResponseData: *new(string),
ContentType: "application/form-data",
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.douyin.com/",
"cookie": tv.cookie,
},
}
var err error
if err = req.Send(); err != nil {
return err
}
resp := fmt.Sprint(req.ResponseData)
splits := strings.Split(resp, `<script id="RENDER_DATA" type="application/json">`)
if len(splits) < 2 {
return fmt.Errorf("fail to find url")
}
resp = splits[1]
resp = strings.Split(resp, `</script>`)[0]
resp, err = url.QueryUnescape(resp)
if err != nil {
return err
}
var autoGenerated douyinAutoGenerated
err = jsoniter.UnmarshalFromString(resp, &autoGenerated)
if err != nil {
return err
}
// 抖音 status == 2 代表是开播的状态
if autoGenerated.InitialState.RoomStore.RoomInfo.Room.Status != 2 {
return nil
}
streamDataStr := autoGenerated.InitialState.RoomStore.RoomInfo.Room.StreamURL.LiveCoreSdkData.PullData.StreamData
var streamData douyinStreamData
err = jsoniter.UnmarshalFromString(streamDataStr, &streamData)
if err != nil {
// log.Println(err.Error())
return nil
}
tv.streamUrl = streamData.Data.Origin.Main.Flv
tv.roomOn = true
tv.roomName = autoGenerated.InitialState.RoomStore.RoomInfo.Room.Title
return nil
}