-
Notifications
You must be signed in to change notification settings - Fork 2
/
tk.go
392 lines (372 loc) · 10.1 KB
/
tk.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
package tk
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
type DB interface {
SetAccessToken(value string) error
GetAccessToken() (string, error)
}
type RoomManager interface {
GetRoomLastActiveTime(roomId string) (time.Time, error)
}
var accessToken string
var refreshTokenChan chan string
var stopchans = make(map[string]chan bool)
var refreshTokenMutex sync.Mutex
var database DB
var roomManager RoomManager
func Init(db DB, roomMgr RoomManager) {
database = db
roomManager = roomMgr
}
func NeedRefreshToken(reason string) {
refreshTokenMutex.Lock()
defer refreshTokenMutex.Unlock()
if len(refreshTokenChan) == 0 {
refreshTokenChan <- reason
}
}
// 新建一个golang func,参数appid secret grant_type 发送以json格式http请求返回asccess_token
func GetAccessToken(appid string, secret string, grant_type string) (string, error) {
url := "https://developer.toutiao.com/api/apps/v2/token"
data := map[string]string{
"appid": appid,
"secret": secret,
"grant_type": grant_type,
}
jsonData, err := json.Marshal(data)
if err != nil {
return "", err
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
return "", err
}
if result["err_no"].(float64) != 0 {
return "", errors.New(result["err_tips"].(string))
}
accessToken = result["data"].(map[string]interface{})["access_token"].(string)
database.SetAccessToken(accessToken)
return accessToken, nil
}
func RefreshAccessToken(appid string, secret string, grant_type string) {
go func() {
refreshTokenChan = make(chan string, 1)
for {
var err error
accessToken, err = GetAccessToken(appid, secret, grant_type)
if err != nil {
// handle error
log.Errorf("Failed to refresh access token: %v", err)
} else {
database.SetAccessToken(accessToken)
}
select {
case m := <-refreshTokenChan:
log.Info(m)
// Timeout
case <-time.After(time.Hour): //
log.Info("RefreshToken after a hour")
}
}
}()
}
func FakeRefreshAccessToken(dur time.Duration) {
go func() {
for {
var err error
accessToken, err = database.GetAccessToken()
if err != nil {
// handle error
log.Errorf("FakeRefreshAccessToken: %v", err)
}
time.Sleep(dur)
}
}()
}
func StartTask(roomid string, appid string, msg_type string) (string, error) {
url := "https://webcast.bytedance.com/api/live_data/task/start"
data := map[string]string{
"roomid": roomid,
"appid": appid,
"msg_type": msg_type,
}
jsonData, err := json.Marshal(data)
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return "", err
}
req.Header.Set("access-token", accessToken)
req.Header.Set("content-type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
return "", err
}
if result["err_no"].(float64) != 0 {
if int64(result["err_no"].(float64)) == 40022 {
NeedRefreshToken(fmt.Sprintf("RefreshToken:StartTask%f%v", result["err_no"].(float64), result["err_msg"].(string)))
}
return "", errors.New(result["err_msg"].(string))
}
//开启,会定时获取漏传数据
//FetchRoomFailData(roomid, appid)
return result["data"].(map[string]interface{})["task_id"].(string), nil
}
func StopTask(roomid string, appid string, msg_type string) (string, error) {
//开启,会关闭定时获取漏传数据
//stopchan, ok := stopchans[roomid]
//if ok {
// stopchan <- true
//}
url := "https://webcast.bytedance.com/api/live_data/task/stop"
data := map[string]string{
"roomid": roomid,
"appid": appid,
"msg_type": msg_type,
}
jsonData, err := json.Marshal(data)
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return "", err
}
req.Header.Set("access-token", accessToken)
req.Header.Set("content-type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
return "", err
}
if result["err_no"].(float64) != 0 {
if int64(result["err_no"].(float64)) == 40022 {
NeedRefreshToken(fmt.Sprintf("RefreshToken:StopTask%f%v", result["err_no"].(float64), result["err_msg"].(string)))
}
return "", errors.New(result["err_msg"].(string))
}
return "", nil
}
func SendGiftPostRequest(roomid string, appid string, sec_gift_id_list []string) ([]string, error) {
url := "https://webcast.bytedance.com/api/gift/top_gift"
data := map[string]interface{}{
"room_id": roomid,
"app_id": appid,
"sec_gift_id_list": sec_gift_id_list,
}
jsonData, err := json.Marshal(data)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
req.Header.Set("x-token", accessToken)
req.Header.Set("content-type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, err
}
if errcode, ok := result["errcode"]; ok {
if errcode.(float64) != 0 {
if int64(errcode.(float64)) == 40022 {
NeedRefreshToken(fmt.Sprintf("RefreshToken:SendGiftPostRequest%f%v", result["errcode"].(float64), result["err_msg"].(string)))
}
return nil, errors.New(result["errmsg"].(string))
}
}
success_top_gift_id_list := result["data"].(map[string]interface{})["success_top_gift_id_list"].([]interface{})
var res []string
for _, v := range success_top_gift_id_list {
res = append(res, v.(string))
}
return res, nil
}
type RoomInfo struct {
Data struct {
Info struct {
RoomId int64 `json:"room_id"`
Uid string `json:"anchor_open_id"`
Nickname string `json:"nick_name"`
} `json:"info"`
} `json:"data"`
ErrCode int64 `json:"errcode"`
ErrMsg string `json:"errmsg"`
}
func GetRoomId(token string) (int64, string, string, error) {
url := "http://webcast.bytedance.com/api/webcastmate/info"
data := map[string]string{
"token": token,
}
jsonData, err := json.Marshal(data)
if err != nil {
return 0, "", "", err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return 0, "", "", err
}
req.Header.Set("X-Token", accessToken)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return 0, "", "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, "", "", err
}
var result RoomInfo
err = json.Unmarshal(body, &result)
if err != nil {
return 0, "", "", err
}
if result.ErrCode != 0 {
if result.ErrCode == 40022 {
NeedRefreshToken(fmt.Sprintf("RefreshToken:GetRoomId%d%s", result.ErrCode, result.ErrMsg))
}
return 0, "", "", errors.New(result.ErrMsg)
}
if result.Data.Info.RoomId == 0 {
return 0, "", "", errors.New("no data in response")
}
roomId := result.Data.Info.RoomId
uid := result.Data.Info.Uid
nickname := result.Data.Info.Nickname
return roomId, uid, nickname, nil
}
/*此功能未验证*/
func FetchRoomFailData(roomid string, appid string) {
go func() {
stopchans[roomid] = make(chan bool)
preCnt := -1
pageSize := 10
for {
pagenum := 1
dataList, cnt, _, err := GetFailData(roomid, appid, "live_gift", pagenum, pageSize)
if err != nil {
// handle error
log.Errorf("Failed to fetch fail data: %v", err)
} else {
for index, data := range dataList {
if index >= preCnt {
item := data.(map[string]interface{})
if item["msg_type"].(string) == "live_gift" {
payload := item["payload"].(string)
log.Error(payload)
}
}
}
}
lastActiveTime, err := roomManager.GetRoomLastActiveTime(roomid)
if err != nil {
return
}
if time.Since(lastActiveTime).Minutes() >= 10 {
return
}
if cnt != pageSize { //未获取全部数据,继续获取该页
preCnt = cnt
select {
case <-stopchans[roomid]:
return
// 1 qps
case <-time.After(time.Second):
}
} else {
preCnt = -1
pagenum++
}
}
}()
}
/*此接口未验证*/
func GetFailData(roomid string, appid string, msg_type string, page_num int, page_size int) ([]interface{}, int, int, error) {
url := fmt.Sprintf("https://webcast.bytedance.com/api/live_data/task/fail_data/get?roomid=%s&appid=%s&msg_type=%s&page_num=%d&page_size=%d", roomid, appid, msg_type, page_num, page_size)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, 0, 0, err
}
req.Header.Set("access-token", accessToken)
req.Header.Set("content-type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, 0, 0, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, 0, 0, err
}
var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, 0, 0, err
}
if result["err_no"].(float64) != 0 {
if int64(result["err_no"].(float64)) == 40022 {
NeedRefreshToken(fmt.Sprintf("GetFailData%f%v", result["err_no"].(float64), result["err_msg"].(string)))
}
return nil, 0, 0, fmt.Errorf(result["err_msg"].(string))
}
dataList := result["data"].(map[string]interface{})["data_list"].([]interface{})
totalCount := int(result["data"].(map[string]interface{})["total_count"].(float64))
return dataList, len(dataList), totalCount, nil
}