summaryrefslogtreecommitdiff
path: root/bilibili/danmaku_server_info.go
blob: 8c79d51cb53eb7ee73f813d3efc8f00950fe90bd (plain)
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
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) {
	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
}