forked from hr3lxphr6j/bililive-go
/
twitch.go
163 lines (147 loc) · 4.03 KB
/
twitch.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
package twitch
import (
"fmt"
"math/rand"
"net/http"
"net/url"
"strings"
"github.com/tidwall/gjson"
"github.com/yuhaohwang/requests"
"github.com/yuhaohwang/bililive-go/src/live"
"github.com/yuhaohwang/bililive-go/src/live/internal"
)
const (
domain = "www.twitch.tv"
cnName = "twitch"
clientId = "jzkbprff40iqj646a697cyrvl0zt2m6"
channelApiUrl = "https://api.twitch.tv/kraken/channels/%s"
liveBaseUrl = "https://usher.ttvnw.net/api/channel/hls/%s.m3u8"
streamApiUrl = "https://api.twitch.tv/kraken/streams/%s"
tokenApiUrl = "https://api.twitch.tv/api/channels/%s/access_token"
v5Header = "application/vnd.twitchtv.v5+json"
userApiUrl = "https://api.twitch.tv/kraken/users?login=%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...),
}, nil
}
var headers = map[string]string{"client-id": clientId}
type Live struct {
internal.BaseLive
userId, hostName, roomName string
}
// 在hostName, roomName为空执行,在live有效时再从steam api解析
func (l *Live) parseInfo() error {
paths := strings.Split(l.Url.Path, "/")
if len(paths) < 2 {
return live.ErrRoomUrlIncorrect
}
chanId := paths[1]
resp, err := requests.Get(fmt.Sprintf(userApiUrl, chanId), live.CommonUserAgent,
requests.Header("client-id", clientId), requests.Header("Accept", v5Header))
if err != nil {
return err
}
body, err := resp.Bytes()
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return live.ErrRoomNotExist
}
if gjson.GetBytes(body, "_total").Int() < 1 {
return live.ErrRoomNotExist
}
l.userId = gjson.GetBytes(body, "users").Array()[0].Get("_id").String()
resp, err = requests.Get(fmt.Sprintf(channelApiUrl, l.userId), live.CommonUserAgent,
requests.Header("client-id", clientId), requests.Header("Accept", v5Header))
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return live.ErrRoomNotExist
}
body, err = resp.Bytes()
if err != nil {
return err
}
l.hostName = gjson.GetBytes(body, "name").String()
l.roomName = gjson.GetBytes(body, "status").String()
return nil
}
func (l *Live) GetInfo() (info *live.Info, err error) {
if l.hostName == "" || l.roomName == "" || l.userId == "" {
if err := l.parseInfo(); err != nil {
return nil, err
}
}
resp, err := requests.Get(fmt.Sprintf(streamApiUrl, l.userId), live.CommonUserAgent,
requests.Header("client-id", clientId), requests.Header("Accept", v5Header))
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, live.ErrRoomNotExist
}
body, err := resp.Bytes()
if err != nil {
return nil, err
}
status := gjson.GetBytes(body, "stream").String() != ""
if status {
l.roomName = gjson.GetBytes(body, "stream.channel.status").String()
}
info = &live.Info{
Live: l,
HostName: l.hostName,
RoomName: l.roomName,
Status: status,
}
return info, nil
}
func (l *Live) GetStreamUrls() (us []*url.URL, err error) {
if l.hostName == "" || l.roomName == "" {
if err := l.parseInfo(); err != nil {
return nil, err
}
}
resp, err := requests.Get(fmt.Sprintf(tokenApiUrl, l.hostName), live.CommonUserAgent, requests.Header("client-id", clientId))
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, live.ErrRoomNotExist
}
body, err := resp.Bytes()
if err != nil {
return nil, err
}
var (
token = gjson.GetBytes(body, "token").String()
sig = gjson.GetBytes(body, "sig").String()
p = fmt.Sprintf("%d", rand.Intn(9000000)+1000000)
)
u, err := url.Parse(fmt.Sprintf(liveBaseUrl, l.hostName))
if err != nil {
return nil, err
}
v := url.Values{}
v.Add("allow_source", "true")
v.Add("allow_audio_only", "true")
v.Add("allow_spectre", "true")
v.Add("p", p)
v.Add("player", "twitchweb")
v.Add("segment_preference", "4")
v.Add("sig", sig)
v.Add("token", token)
u.RawQuery = v.Encode()
return []*url.URL{u}, nil
}
func (l *Live) GetPlatformCNName() string {
return cnName
}