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 --- bilibili/danmaku_server_info.go | 43 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) (limited to 'bilibili/danmaku_server_info.go') diff --git a/bilibili/danmaku_server_info.go b/bilibili/danmaku_server_info.go index 4d26873..8c79d51 100644 --- a/bilibili/danmaku_server_info.go +++ b/bilibili/danmaku_server_info.go @@ -1,11 +1,50 @@ package bilibili import ( + "errors" "fmt" "github.com/keuin/slbr/types" + "net/url" ) +type WebBannerResponse = BaseResponse[interface{}] + +const apiUrlPrefix = "https://api.live.bilibili.com" + func (b *Bilibili) GetDanmakuServerInfo(roomId types.RoomId) (resp types.DanmakuServerInfoResponse, err error) { - url := fmt.Sprintf("https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?id=%d&type=0", roomId) - return callGet[types.DanmakuServerInfoResponse](b, url) + u := fmt.Sprintf("https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?id=%d&type=0", roomId) + return callGet[types.DanmakuServerInfoResponse](b, u) +} + +// GetBUVID initializes cookie `buvid3`. If success, returns its value. +func (b *Bilibili) GetBUVID() (string, error) { + const u = "https://data.bilibili.com/v/web/web_page_view" + _, _, err := callGetRaw(b, u) + if err != nil { + return "", err + } + uu, _ := url.Parse(apiUrlPrefix) + cookies := b.http.Jar.Cookies(uu) + var buvid3 *string + for _, c := range cookies { + if c.Name == "buvid3" { + buvid3 = &c.Value + } + } + if buvid3 == nil { + return "", errors.New("failed to get buvid3") + } + return *buvid3, nil +} + +// GetLiveBUVID initializes cookie `LIVE_BUVID`. This should be called before GetDanmakuServerInfo. +func (b *Bilibili) GetLiveBUVID(roomId types.RoomId) (resp WebBannerResponse, err error) { + u := fmt.Sprintf("https://api.live.bilibili.com/activity/v1/Common/webBanner?"+ + "platform=web&position=6&roomid=%d&area_v2_parent_id=0&area_v2_id=0&from=", roomId) + resp, err = callGet[WebBannerResponse](b, u) + if err == nil { + uu, _ := url.Parse(apiUrlPrefix) + b.logger.Info("Cookie info: %v", b.http.Jar.Cookies(uu)) + } + return resp, err } -- cgit v1.2.3