|
1 | 1 | package bilibili
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "strconv" |
| 5 | + "time" |
| 6 | + |
4 | 7 | "github.com/go-resty/resty/v2"
|
5 | 8 | )
|
6 | 9 |
|
@@ -117,13 +120,13 @@ type StartLiveParam struct {
|
117 | 120 | AreaV2 int `json:"area_v2"` // 直播分区id(子分区id)。详见[直播分区]
|
118 | 121 | Platform string `json:"platform"` // 直播平台。直播姬(pc):pc_link。web在线直播:web_link(已下线)。bililink:android_link。
|
119 | 122 |
|
120 |
| - // 下面四个参数详见:https://github.com/SocialSisterYi/bilibili-API-collect/pull/1351/files |
121 |
| - Version string `json:"version"` // 直播姬版本号 |
122 |
| - Build int `json:"build"` // 直播姬构建号 |
123 |
| - Appkey string `json:"appkey"` // APP密钥 |
124 |
| - Sign string `json:"sign"` // APP API签名得到的sign |
| 123 | + // 下面四个参数详见:https://github.com/SocialSisterYi/bilibili-API-collect/pull/1351/files 。 |
| 124 | + // 可以调用 GetHomePageLiveVersion 方法获取 Version 和 Build 参数。 |
| 125 | + Version string `json:"version"` // 直播姬版本号,2025.7.20后对于某些用户必填 |
| 126 | + Build int `json:"build"` // 直播姬构建号,2025.7.20后对于某些用户必填 |
| 127 | + Appkey string `json:"appkey,omitempty"` // APP密钥,不填会自动计算 |
| 128 | + Sign string `json:"sign,omitempty"` // APP API签名得到的sign,不填会自动计算 |
125 | 129 |
|
126 |
| - // 还需要一个ts,详见:https://github.com/SocialSisterYi/bilibili-API-collect/issues/1349 |
127 | 130 | Ts int `json:"ts,omitempty" request:"query,omitempty"` // 10位时间戳
|
128 | 131 | }
|
129 | 132 |
|
@@ -163,11 +166,41 @@ type StartLiveResult struct {
|
163 | 166 | }
|
164 | 167 |
|
165 | 168 | // StartLive 开始直播
|
| 169 | +// |
| 170 | +// 注意:为了方便使用,这个函数的很多参数做了自动填入,使用例子可以参考:https://github.com/CuteReimu/bilibili/issues/121 |
166 | 171 | func (c *Client) StartLive(param StartLiveParam) (*StartLiveResult, error) {
|
167 | 172 | const (
|
168 | 173 | method = resty.MethodPost
|
169 | 174 | url = "https://api.live.bilibili.com/room/v1/Room/startLive"
|
170 | 175 | )
|
| 176 | + |
| 177 | + // 如果没有提供签名,自动计算 |
| 178 | + if param.Sign == "" && param.Appkey == "" { |
| 179 | + // 已知的 Bilibili 直播姬的密钥和对应的秘钥 |
| 180 | + // 这些是公开的常量,用于计算 API 签名 |
| 181 | + const ( |
| 182 | + appKey = "aae92bc66f3edfab" |
| 183 | + appSecret = "af125a0d5279fd576c1b4418a3e8276d" //nolint:gosec |
| 184 | + ) |
| 185 | + if param.Ts == 0 { |
| 186 | + param.Ts = int(time.Now().Unix()) |
| 187 | + } |
| 188 | + param.Appkey = appKey |
| 189 | + csrf := c.getCookie("bili_jct") |
| 190 | + signParams := map[string]string{ |
| 191 | + "appkey": param.Appkey, |
| 192 | + "build": strconv.Itoa(param.Build), |
| 193 | + "platform": param.Platform, |
| 194 | + "room_id": strconv.Itoa(param.RoomId), |
| 195 | + "area_v2": strconv.Itoa(param.AreaV2), |
| 196 | + "ts": strconv.Itoa(param.Ts), |
| 197 | + "version": param.Version, |
| 198 | + "csrf": csrf, |
| 199 | + "csrf_token": csrf, |
| 200 | + } |
| 201 | + param.Sign = calculateAppSign(signParams, appSecret) |
| 202 | + } |
| 203 | + |
171 | 204 | return execute[*StartLiveResult](c, method, url, param, fillCsrf(c))
|
172 | 205 | }
|
173 | 206 |
|
|
0 commit comments