Skip to main content

bpi_rs/live/
redpocket.rs

1use serde::{ Deserialize, Serialize };
2
3use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
4
5// ================= 数据结构 =================
6
7#[derive(Debug, Serialize, Clone, Deserialize)]
8pub struct RedPocketAward {
9    /// 礼物id
10    pub gift_id: i64,
11    /// 数量
12    pub num: i32,
13    /// 礼物名称
14    pub gift_name: String,
15    /// 礼物图片
16    pub gift_pic: String,
17}
18
19#[derive(Debug, Serialize, Clone, Deserialize)]
20pub struct PopularityRedPocket {
21    /// 红包id
22    pub lot_id: i64,
23    /// 红包发送者uid
24    pub sender_uid: i64,
25    /// 红包发送者昵称
26    pub sender_name: String,
27    /// 红包发送者头像
28    pub sender_face: String,
29    /// 参与条件
30    pub join_requirement: i32,
31    /// 参与红包时自动发送的弹幕内容
32    pub danmu: String,
33    /// 红包内容
34    pub awards: Vec<RedPocketAward>,
35    /// 开始时间
36    pub start_time: i64,
37    /// 结束时间
38    pub end_time: i64,
39    /// 持续时间
40    pub last_time: i64,
41    /// 移除时间
42    pub remove_time: i64,
43    /// 替换时间
44    pub replace_time: i64,
45    /// 当前时间
46    pub current_time: i64,
47    /// 红包状态
48    pub lot_status: i32,
49    /// 红包界面
50    pub h5_url: String,
51    /// 用户是否已参与
52    pub user_status: i32,
53    /// 红包配置id
54    pub lot_config_id: i64,
55    /// 红包总计价格
56    pub total_price: i64,
57}
58
59#[derive(Debug, Serialize, Clone, Deserialize)]
60pub struct ActivityBoxInfo {
61    // 根据实际情况添加字段
62    #[serde(flatten)]
63    pub extra: Option<serde_json::Value>,
64}
65
66#[derive(Debug, Serialize, Clone, Deserialize)]
67pub struct LotteryInfoData {
68    /// 人气红包信息
69    pub popularity_red_pocket: Option<Vec<PopularityRedPocket>>,
70    /// 活动盒子信息
71    pub activity_box_info: Option<ActivityBoxInfo>,
72    // 其他可能的字段
73    #[serde(flatten)]
74    pub extra: serde_json::Value,
75}
76
77pub type LotteryInfoResponse = BpiResponse<LotteryInfoData>;
78
79// ================= 实现 =================
80
81impl BpiClient {
82    /// 获取指定直播间的红包信息
83    ///
84
85    /// # 文档
86    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/live)
87    ///
88    /// # 参数
89    ///
90    /// | 名称 | 类型 | 说明 |
91    /// | ---- | ---- | ---- |
92    /// | `room_id` | i64 | 直播间 ID |
93    pub async fn live_lottery_info(&self, room_id: i64) -> Result<LotteryInfoResponse, BpiError> {
94        let params = [("roomid", room_id.to_string())];
95
96        let resp: LotteryInfoResponse = self
97            .get(
98                "https://api.live.bilibili.com/xlive/lottery-interface/v1/lottery/getLotteryInfoWeb"
99            )
100            .query(&params)
101            .send_bpi("获取指定直播间的红包信息").await?;
102
103        Ok(resp)
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110
111    #[tokio::test]
112    async fn test_get_live_lottery_info() -> Result<(), Box<BpiError>> {
113        let bpi = BpiClient::new();
114        bpi.live_lottery_info(23174842).await?;
115
116        // 注意:直播间可能没有红包,所以不做额外断言
117        Ok(())
118    }
119}