From 9b5c3913989754370bd7d03ac8cf2e32a6172afb Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 29 Jul 2023 19:43:27 +0800 Subject: Simulate real web app cookies & WebSocket fields --- recording/runner.go | 46 +++++++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 13 deletions(-) (limited to 'recording/runner.go') diff --git a/recording/runner.go b/recording/runner.go index 9ef2cfd..9e5f83c 100644 --- a/recording/runner.go +++ b/recording/runner.go @@ -76,15 +76,9 @@ func tryRunTask(t *RunningTask) error { t.logger.Info("Getting notification server info...") - type dmServerInfo struct { - AuthKey string - DmUrl string - } - dmInfo, err := AutoRetryWithTask( - t, func() (info dmServerInfo, err error) { - info.AuthKey, info.DmUrl, err = getDanmakuServer(&t.TaskConfig, bi) - return + t, func() (*danmakuServerInfo, error) { + return getDanmakuServer(&t.TaskConfig, bi) }, ) if err != nil { @@ -124,13 +118,17 @@ func tryRunTask(t *RunningTask) error { } loop: for run { + t.logger.Info("Start watching, ws url: %v, auth key: %v, buvid3: %v", + dmInfo.DanmakuWebsocketUrl, dmInfo.AuthKey, dmInfo.BUVID3) err = watch( ctxWatcher, t.TaskConfig, - dmInfo.DmUrl, + dmInfo.DanmakuWebsocketUrl, dmInfo.AuthKey, + dmInfo.BUVID3, liveStatusChecker, t.logger, + &bi, ) // the context is cancelled if errors.Is(err, context.Canceled) { @@ -361,23 +359,45 @@ func record( return errs.NewError(errs.StreamCopy, err) } +type danmakuServerInfo struct { + DanmakuWebsocketUrl string + AuthKey string + BUVID3 string +} + func getDanmakuServer( task *TaskConfig, bi *bilibili.Bilibili, -) (string, string, error) { +) (*danmakuServerInfo, error) { + buvid3, err := bi.GetBUVID() + if err != nil { + return nil, fmt.Errorf("failed to get buvid: %w", err) + } + + resp, err := bi.GetLiveBUVID(task.RoomId) + if err != nil || resp.Code != 0 { + if err != nil { + return nil, fmt.Errorf("failed to get LIVE_BUVID with api `webBanner`: %w", err) + } + return nil, fmt.Errorf("failed to get LIVE_BUVID with api `webBanner`: invalid response: %v", resp) + } dmInfo, err := bi.GetDanmakuServerInfo(task.RoomId) if err != nil { - return "", "", fmt.Errorf("failed to read stream server info: %w", err) + return nil, fmt.Errorf("failed to read stream server info: %w", err) } if len(dmInfo.Data.HostList) == 0 { - return "", "", fmt.Errorf("no available stream server") + return nil, fmt.Errorf("no available stream server") } // get authkey and ws url authKey := dmInfo.Data.Token host := dmInfo.Data.HostList[0] url := fmt.Sprintf("wss://%s:%d/sub", host.Host, host.WssPort) - return authKey, url, nil + return &danmakuServerInfo{ + DanmakuWebsocketUrl: url, + AuthKey: authKey, + BUVID3: buvid3, + }, nil } func GenerateFileName(roomName string, t time.Time) string { -- cgit v1.2.3