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    /// 文档: https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/live
86    ///
87    /// 参数
88    ///
89    /// | 名称 | 类型 | 说明 |
90    /// | ---- | ---- | ---- |
91    /// | `room_id` | i64 | 直播间 ID |
92    pub async fn live_lottery_info(&self, room_id: i64) -> Result<LotteryInfoResponse, BpiError> {
93        let params = [("roomid", room_id.to_string())];
94
95        let resp: LotteryInfoResponse = self
96      .get("https://api.live.bilibili.com/xlive/lottery-interface/v1/lottery/getLotteryInfoWeb")
97      .query(&params)
98      .send_bpi("获取指定直播间的红包信息").await?;
99
100        Ok(resp)
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107
108    #[tokio::test]
109    async fn test_get_live_lottery_info() -> Result<(), Box<BpiError>> {
110        let bpi = BpiClient::new();
111        bpi.live_lottery_info(23174842).await?;
112
113        // 注意:直播间可能没有红包,所以不做额外断言
114        Ok(())
115    }
116}