diff options
author | Keuin <[email protected]> | 2023-07-29 19:43:27 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2023-07-29 20:21:17 +0800 |
commit | 9b5c3913989754370bd7d03ac8cf2e32a6172afb (patch) | |
tree | 9e2caca8feb9ca5a9c14a96424d23e20db4314bc /recording | |
parent | a153bf02d1cf05f020d263e9670a76ae99cfeb02 (diff) |
Simulate real web app cookies & WebSocket fields
Diffstat (limited to 'recording')
-rw-r--r-- | recording/runner.go | 46 | ||||
-rw-r--r-- | recording/watcher.go | 15 |
2 files changed, 41 insertions, 20 deletions
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 { diff --git a/recording/watcher.go b/recording/watcher.go index 515def1..ff9bde1 100644 --- a/recording/watcher.go +++ b/recording/watcher.go @@ -4,7 +4,8 @@ import ( "context" "encoding/base64" "encoding/json" - errs "github.com/keuin/slbr/bilibili/errors" + errs "github.com/keuin/slbr/bilibili" + "github.com/keuin/slbr/bilibili/errors" "github.com/keuin/slbr/danmaku" "github.com/keuin/slbr/danmaku/dmmsg" "github.com/keuin/slbr/danmaku/dmpkg" @@ -41,19 +42,19 @@ func watch( ctx context.Context, t TaskConfig, url string, - authKey string, + authKey, buvid3 string, liveStatusChecker func() (bool, error), logger logging.Logger, + bi *bilibili.Bilibili, ) error { var err error - dm := danmaku.NewDanmakuClient() - - // connect to danmaku server for live online/offline notifications - err = dm.Connect(ctx, url) + ws, err := bi.DialWebSocket(ctx, url) if err != nil { return errs.NewError(errs.DanmakuServerConnection, err) } + + dm := danmaku.NewClient(ctx, ws) defer func() { // this operation may be time-consuming, so run in another goroutine go func() { @@ -63,7 +64,7 @@ func watch( // the danmaku server requires an auth token and room id when connected logger.Info("ws connected. Authenticating...") - err = dm.Authenticate(t.RoomId, authKey) + err = dm.Authenticate(t.RoomId, authKey, buvid3) if err != nil { return errs.NewError(errs.InvalidAuthProtocol, err) } |