1use crate::{BilibiliRequest, BpiClient, BpiError, BpiResponse};
2use reqwest::multipart::Form;
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6#[derive(Debug, Clone, Deserialize, Serialize)]
10pub struct CreateRoomData {
11 #[serde(rename = "roomID")]
12 pub room_id: Option<String>,
13}
14
15#[derive(Debug, Clone, Deserialize, Serialize)]
17pub struct UpdateRoomData {
18 pub sub_session_key: String,
19 pub audit_info: Option<AuditInfo>,
20}
21
22#[derive(Debug, Clone, Deserialize, Serialize)]
24pub struct AuditInfo {
25 pub audit_title_reason: String,
26 pub audit_title_status: u8,
27 pub audit_title: Option<String>,
28 pub update_title: Option<String>,
29}
30
31#[derive(Debug, Clone, Deserialize, Serialize)]
33pub struct RtmpInfo {
34 pub addr: String,
35 pub code: String,
36}
37
38#[derive(Debug, Clone, Deserialize, Serialize)]
40pub struct StartLiveData {
41 pub change: u8,
42 pub status: String,
43 pub rtmp: RtmpInfo,
44 pub live_key: String,
45 pub sub_session_key: String,
46 pub need_face_auth: bool,
47 pub room_type: Value,
49 pub protocols: Value,
50 pub notice: Value,
51 pub qr: Value,
52 pub service_source: String,
53 pub rtmp_backup: Value,
54 pub up_stream_extra: Value,
55}
56
57#[derive(Debug, Clone, Deserialize, Serialize)]
59pub struct StopLiveData {
60 pub change: u8,
61 pub status: String,
62}
63
64#[derive(Debug, Clone, Deserialize, Serialize)]
66pub struct UpdatePreLiveInfoData {
67 pub audit_info: Option<AuditInfo>,
68}
69
70#[derive(Debug, Clone, Deserialize, Serialize)]
72pub struct PcLiveVersionData {
73 pub curr_version: String,
74 pub build: u64,
75 pub instruction: String,
76 pub file_size: String,
77 pub file_md5: String,
78 pub content: String,
79 pub download_url: String,
80 pub hdiffpatch_switch: u8,
81}
82
83impl BpiClient {
84 pub async fn live_create_room(&self) -> Result<BpiResponse<CreateRoomData>, BpiError> {
86 let csrf = self.csrf()?;
87 let form = Form::new()
88 .text("platform", "web")
89 .text("visit_id", "")
90 .text("csrf", csrf.clone())
91 .text("csrf_token", csrf);
92
93 self.post("https://api.live.bilibili.com/xlive/app-blink/v1/preLive/CreateRoom")
94 .multipart(form)
95 .send_bpi("开通直播间")
96 .await
97 }
98
99 pub async fn live_update_room_info(
108 &self,
109 room_id: u64,
110 title: Option<&str>,
111 area_id: Option<u64>,
112 add_tag: Option<&str>,
113 del_tag: Option<&str>,
114 ) -> Result<BpiResponse<UpdateRoomData>, BpiError> {
115 let csrf = self.csrf()?;
116 let mut form = Form::new()
117 .text("room_id", room_id.to_string())
118 .text("csrf", csrf.clone())
119 .text("csrf_token", csrf);
120
121 if let Some(t) = title {
122 form = form.text("title", t.to_string());
123 }
124 if let Some(a) = area_id {
125 form = form.text("area_id", a.to_string());
126 }
127 if let Some(a_tag) = add_tag {
128 form = form.text("add_tag", a_tag.to_string());
129 }
130 if let Some(d_tag) = del_tag {
131 form = form.text("del_tag", d_tag.to_string());
132 }
133
134 self.post("https://api.live.bilibili.com/room/v1/Room/update")
135 .multipart(form)
136 .send_bpi("更新直播间信息")
137 .await
138 }
139
140 #[allow(dead_code)]
147 async fn live_start(
148 &self,
149 room_id: u64,
150 area_v2: u64,
151 platform: &str,
152 ) -> Result<BpiResponse<StartLiveData>, BpiError> {
153 let csrf = self.csrf()?;
154 let form = Form::new()
155 .text("room_id", room_id.to_string())
156 .text("area_v2", area_v2.to_string())
157 .text("platform", platform.to_string())
158 .text("csrf", csrf.clone())
159 .text("csrf_token", csrf);
160
161 self.post("https://api.live.bilibili.com/room/v1/Room/startLive")
162 .multipart(form)
163 .send_bpi("开始直播")
164 .await
165 }
166
167 pub async fn live_stop(
173 &self,
174 room_id: u64,
175 platform: &str,
176 ) -> Result<BpiResponse<StopLiveData>, BpiError> {
177 let csrf = self.csrf()?;
178 let form = Form::new()
179 .text("platform", platform.to_string())
180 .text("room_id", room_id.to_string())
181 .text("csrf", csrf.clone())
182 .text("csrf_token", csrf);
183
184 self.post("https://api.live.bilibili.com/room/v1/Room/stopLive")
185 .multipart(form)
186 .send_bpi("关闭直播")
187 .await
188 }
189
190 pub async fn live_update_pre_live_info(
196 &self,
197 title: Option<&str>,
198 cover: Option<&str>,
199 ) -> Result<BpiResponse<UpdatePreLiveInfoData>, BpiError> {
200 let csrf = self.csrf()?;
201 let mut form = Form::new()
202 .text("platform", "web")
203 .text("mobi_app", "web")
204 .text("build", "1")
205 .text("csrf", csrf.clone())
206 .text("csrf_token", csrf);
207
208 if let Some(t) = title {
209 form = form.text("title", t.to_string());
210 }
211 if let Some(c) = cover {
212 form = form.text("cover", c.to_string());
213 }
214
215 self.post("https://api.live.bilibili.com/xlive/app-blink/v1/preLive/UpdatePreLiveInfo")
216 .multipart(form)
217 .send_bpi("预更新直播间信息")
218 .await
219 }
220
221 pub async fn live_update_room_news(
228 &self,
229 room_id: u64,
230 uid: u64,
231 content: &str,
232 ) -> Result<BpiResponse<Value>, BpiError> {
233 let csrf = self.csrf()?;
234 let form = Form::new()
235 .text("room_id", room_id.to_string())
236 .text("uid", uid.to_string())
237 .text("content", content.to_string())
238 .text("csrf", csrf.clone())
239 .text("csrf_token", csrf);
240
241 self.post("https://api.live.bilibili.com/xlive/app-blink/v1/index/updateRoomNews")
242 .multipart(form)
243 .send_bpi("更新直播间公告")
244 .await
245 }
246
247 pub async fn live_version(&self) -> Result<BpiResponse<PcLiveVersionData>, BpiError> {
249 self.get("https://api.live.bilibili.com/xlive/app-blink/v1/liveVersionInfo/getHomePageLiveVersion")
250 .query(&[("system_version", "2")])
251 .send_bpi("获取 PC 直播姬版本号")
252 .await
253 }
254}
255
256#[cfg(test)]
257mod tests {
258 use super::*;
259 use tracing::info;
260
261 #[tokio::test]
262
263 async fn test_live_create_room() -> Result<(), BpiError> {
264 let bpi = BpiClient::new();
265 match bpi.live_create_room().await {
266 Ok(resp) => resp,
267 Err(err) => {
268 if let Some(code) = err.code() {
270 if code == 1531193016 {
271 tracing::warn!("allowed special code: {}", code);
272 return Ok(());
273 }
274 }
275 return Err(err);
276 }
277 };
278 Ok(())
279 }
280
281 #[tokio::test]
282
283 async fn test_live_update_room_info() -> Result<(), BpiError> {
284 let bpi = BpiClient::new();
285 let room_id = 3818081;
286 let resp = bpi
287 .live_update_room_info(room_id, Some("测试新标题"), None, None, None)
288 .await?;
289
290 assert_eq!(resp.code, 0);
291 let data = resp.into_data()?;
292
293 info!("更新直播间信息返回:{:?}", data);
294
295 Ok(())
296 }
297
298 #[tokio::test]
299
300 async fn test_live_stop() -> Result<(), BpiError> {
301 let bpi = BpiClient::new();
302 let room_id = 3818081;
304 let resp = bpi.live_stop(room_id, "pc_link").await?;
305 assert_eq!(resp.code, 0);
306 let data = resp.into_data()?;
307
308 info!("关闭直播返回:{:?}", data);
309
310 Ok(())
311 }
312
313 #[tokio::test]
314
315 async fn test_live_update_pre_live_info() -> Result<(), BpiError> {
316 let bpi = BpiClient::new();
317 let resp = bpi
318 .live_update_pre_live_info(Some("测试预更新标题"), None)
319 .await?;
320 assert_eq!(resp.code, 0);
321
322 let data = resp.into_data()?;
323
324 info!("预更新直播间信息返回:{:?}", data);
325
326 Ok(())
327 }
328
329 #[tokio::test]
330
331 async fn test_live_update_room_news() -> Result<(), BpiError> {
332 let bpi = BpiClient::new();
333 let room_id = 3818081;
335 let uid = 4279370;
336 let content = "Rust 指南测试公告";
337 let resp = bpi.live_update_room_news(room_id, uid, content).await?;
338 assert_eq!(resp.code, 0);
339
340 info!("更新直播间公告返回:{:?}", resp);
341
342 Ok(())
343 }
344
345 #[tokio::test]
346 async fn test_live_version() -> Result<(), BpiError> {
347 let bpi = BpiClient::new();
348 let resp = bpi.live_version().await?;
349 assert_eq!(resp.code, 0);
350 let data = resp.into_data()?;
351
352 info!("PC直播姬版本号: {}", data.curr_version);
353
354 Ok(())
355 }
356}