Skip to main content

bpi_rs/dynamic/
content.rs

1use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
2use serde::{ Deserialize, Serialize };
3
4/// 直播的已关注者列表项
5#[derive(Debug, Clone, Deserialize, Serialize)]
6pub struct LiveUser {
7    /// 直播者头像 URL
8    pub face: String,
9    /// 直播链接
10    pub link: String,
11    /// 直播标题
12    pub title: String,
13    /// 直播者 ID
14    pub uid: u64,
15    /// 直播者昵称
16    pub uname: String,
17}
18
19/// 正在直播的已关注者响应数据
20#[derive(Debug, Clone, Deserialize, Serialize)]
21pub struct LiveUsersData {
22    /// 直播者数量
23    pub count: u64,
24    /// 作用尚不明确
25    pub group: String,
26    /// 直播者列表
27    pub items: Vec<LiveUser>,
28}
29
30/// 发布新动态的已关注者列表项
31#[derive(Debug, Clone, Deserialize, Serialize)]
32pub struct DynUpUser {
33    pub user_profile: UserProfile,
34}
35#[derive(Debug, Clone, Deserialize, Serialize)]
36pub struct UserProfile {
37    pub info: UserInfo,
38}
39
40#[derive(Debug, Clone, Deserialize, Serialize)]
41pub struct UserInfo {
42    pub uid: u64,
43    pub uname: String,
44    pub face: String,
45}
46
47/// 发布新动态的已关注者响应数据
48#[derive(Debug, Clone, Deserialize, Serialize)]
49pub struct DynUpUsersData {
50    /// 作用尚不明确
51    pub button_statement: String,
52    /// 更新者列表
53    pub items: Vec<DynUpUser>,
54}
55
56impl BpiClient {
57    /// 获取正在直播的已关注者
58    ///
59    /// # 文档
60    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/dynamic)
61    ///
62    /// # 参数
63    ///
64    /// | 名称 | 类型 | 说明 |
65    /// | ---- | ---- | ---- |
66    /// | `size` | `Option<u32>` | 每页显示数,默认 10 |
67    pub async fn dynamic_live_users(
68        &self,
69        size: Option<u32>
70    ) -> Result<BpiResponse<LiveUsersData>, BpiError> {
71        let mut req = self.get(
72            "https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/w_live_users"
73        );
74
75        if let Some(s) = size {
76            req = req.query(&[("size", &s.to_string())]);
77        }
78
79        req.send_bpi("获取正在直播的已关注者").await
80    }
81
82    /// 获取发布新动态的已关注者
83    ///
84    /// # 文档
85    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/dynamic)
86    ///
87    /// # 参数
88    ///
89    /// | 名称 | 类型 | 说明 |
90    /// | ---- | ---- | ---- |
91    /// | `teenagers_mode` | `Option<u8>` | 是否开启青少年模式:0 否,1 是 |
92    pub async fn dynamic_up_users(
93        &self,
94        teenagers_mode: Option<u8>
95    ) -> Result<BpiResponse<DynUpUsersData>, BpiError> {
96        let mut req = self.get(
97            "https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/w_dyn_uplist"
98        );
99
100        if let Some(mode) = teenagers_mode {
101            req = req.query(&[("teenagers_mode", &mode.to_string())]);
102        } else {
103            // 默认值处理
104            req = req.query(&[("teenagers_mode", "0")]);
105        }
106
107        req.send_bpi("获取发布新动态的已关注者").await
108    }
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114    use tracing::info;
115
116    // 您需要在 `Cargo.toml` 中添加 `dotenvy` 和 `tracing` 依赖,并在 `main.rs` 或测试入口处初始化日志
117    // 例如: tracing_subscriber::fmt::init();
118
119    #[tokio::test]
120    async fn test_get_live_users() -> Result<(), BpiError> {
121        let bpi = BpiClient::new();
122        let resp = bpi.dynamic_live_users(Some(1)).await?;
123        let data = resp.into_data()?;
124
125        info!("直播中的关注者数量: {}", data.count);
126        info!("第一位直播中的关注者: {:?}", data.items.get(0));
127
128        Ok(())
129    }
130
131    #[tokio::test]
132    async fn test_get_dyn_up_users() -> Result<(), BpiError> {
133        let bpi = BpiClient::new();
134        let resp = bpi.dynamic_up_users(None).await?;
135        let data = resp.into_data()?;
136
137        info!("发布新动态的关注者列表: {:?}", data.items);
138        assert!(!data.items.is_empty());
139
140        Ok(())
141    }
142}