Skip to main content

bpi_rs/live/
info.rs

1use serde::{ Deserialize, Serialize };
2
3use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
4
5// ================= 数据结构 =================
6
7#[derive(Debug, Serialize, Clone, Deserialize)]
8pub struct RoomPendantFrame {
9    /// 名称
10    pub name: String,
11    /// 值
12    pub value: String,
13    /// 位置
14    pub position: i32,
15    /// 描述
16    pub desc: String,
17    /// 分区
18    pub area: i32,
19    /// 旧分区
20    pub area_old: i32,
21    /// 背景色
22    pub bg_color: String,
23    /// 背景图
24    pub bg_pic: String,
25    /// 是否旧分区号
26    pub use_old_area: bool,
27}
28
29#[derive(Debug, Serialize, Clone, Deserialize)]
30pub struct RoomPendantBadge {
31    /// 类型
32    pub name: String,
33    /// 位置
34    pub position: i32,
35    /// 值
36    pub value: String,
37    /// 描述
38    pub desc: String,
39}
40
41#[derive(Debug, Serialize, Clone, Deserialize)]
42pub struct RoomPendants {
43    /// 头像框
44    pub frame: RoomPendantFrame,
45    /// 手机版头像框
46    pub mobile_frame: Option<RoomPendantFrame>,
47    /// 大v
48    pub badge: RoomPendantBadge,
49    /// 手机版大v
50    pub mobile_badge: Option<RoomPendantBadge>,
51}
52
53#[derive(Debug, Serialize, Clone, Deserialize)]
54pub struct RoomStudioInfo {
55    // 根据实际情况添加字段
56    #[serde(flatten)]
57    pub extra: serde_json::Value,
58}
59
60#[derive(Debug, Serialize, Clone, Deserialize)]
61pub struct RoomInfoData {
62    /// 主播mid
63    pub uid: i64,
64    /// 直播间长号
65    pub room_id: i64,
66    /// 直播间短号
67    pub short_id: i64,
68    /// 关注数量
69    pub attention: i64,
70    /// 观看人数
71    pub online: i64,
72    /// 是否竖屏
73    pub is_portrait: bool,
74    /// 描述
75    pub description: String,
76    /// 直播状态
77    pub live_status: i32,
78    /// 分区id
79    pub area_id: i32,
80    /// 父分区id
81    pub parent_area_id: i32,
82    /// 父分区名称
83    pub parent_area_name: String,
84    /// 旧版分区id
85    pub old_area_id: i32,
86    /// 背景图片链接
87    pub background: String,
88    /// 标题
89    pub title: String,
90    /// 封面
91    pub user_cover: String,
92    /// 关键帧
93    pub keyframe: String,
94    /// 直播开始时间
95    pub live_time: String,
96    /// 标签
97    pub tags: String,
98    /// 禁言状态
99    pub room_silent_type: String,
100    /// 禁言等级
101    pub room_silent_level: i32,
102    /// 禁言时间
103    pub room_silent_second: i64,
104    /// 分区名称
105    pub area_name: String,
106    /// 热词
107    pub hot_words: Vec<String>,
108    /// 热词状态
109    pub hot_words_status: i32,
110    /// 头像框\大v
111    pub new_pendants: RoomPendants,
112    /// pk状态
113    pub pk_status: i32,
114    /// pk id
115    pub pk_id: i64,
116    /// 允许更改分区时间
117    pub allow_change_area_time: i64,
118    /// 允许上传封面时间
119    pub allow_upload_cover_time: i64,
120    /// 工作室信息
121    pub studio_info: Option<RoomStudioInfo>,
122}
123
124impl BpiClient {
125    /// 获取直播间信息
126    ///
127
128    /// # 文档
129    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/live)
130    pub async fn live_room_info(
131        &self,
132        room_id: i64
133    ) -> Result<BpiResponse<RoomInfoData>, BpiError> {
134        let params = [("room_id", room_id.to_string())];
135
136        let resp = self
137            .get("https://api.live.bilibili.com/room/v1/Room/get_info")
138            .query(&params)
139            .send_bpi("获取直播间信息").await?;
140
141        Ok(resp)
142    }
143}
144
145#[cfg(test)]
146mod tests {
147    use super::*;
148
149    #[tokio::test]
150    async fn test_get_room_info() -> Result<(), Box<BpiError>> {
151        let bpi = BpiClient::new();
152        let result = bpi.live_room_info(23174842).await?;
153
154        let data = result.data.unwrap();
155        assert_eq!(data.room_id, 23174842);
156        Ok(())
157    }
158}