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    /// 文档: https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/dynamic
60    ///
61    /// 参数
62    ///
63    /// | 名称 | 类型 | 说明 |
64    /// | ---- | ---- | ---- |
65    /// | `size` | Option<u32> | 每页显示数,默认 10 |
66    pub async fn dynamic_live_users(
67        &self,
68        size: Option<u32>,
69    ) -> Result<BpiResponse<LiveUsersData>, BpiError> {
70        let mut req =
71            self.get("https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/w_live_users");
72
73        if let Some(s) = size {
74            req = req.query(&[("size", &s.to_string())]);
75        }
76
77        req.send_bpi("获取正在直播的已关注者").await
78    }
79
80    /// 获取发布新动态的已关注者
81    ///
82    /// 文档: https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/dynamic
83    ///
84    /// 参数
85    ///
86    /// | 名称 | 类型 | 说明 |
87    /// | ---- | ---- | ---- |
88    /// | `teenagers_mode` | Option<u8> | 是否开启青少年模式:0 否,1 是 |
89    pub async fn dynamic_up_users(
90        &self,
91        teenagers_mode: Option<u8>,
92    ) -> Result<BpiResponse<DynUpUsersData>, BpiError> {
93        let mut req =
94            self.get("https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/w_dyn_uplist");
95
96        if let Some(mode) = teenagers_mode {
97            req = req.query(&[("teenagers_mode", &mode.to_string())]);
98        } else {
99            // 默认值处理
100            req = req.query(&[("teenagers_mode", "0")]);
101        }
102
103        req.send_bpi("获取发布新动态的已关注者").await
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110    use tracing::info;
111
112    // 您需要在 `Cargo.toml` 中添加 `dotenvy` 和 `tracing` 依赖,并在 `main.rs` 或测试入口处初始化日志
113    // 例如: tracing_subscriber::fmt::init();
114
115    #[tokio::test]
116
117    async fn test_get_live_users() -> Result<(), BpiError> {
118        let bpi = BpiClient::new();
119        let resp = bpi.dynamic_live_users(Some(1)).await?;
120        let data = resp.into_data()?;
121
122        info!("直播中的关注者数量: {}", data.count);
123        info!("第一位直播中的关注者: {:?}", data.items.get(0));
124
125        Ok(())
126    }
127
128    #[tokio::test]
129
130    async fn test_get_dyn_up_users() -> Result<(), BpiError> {
131        let bpi = BpiClient::new();
132        let resp = bpi.dynamic_up_users(None).await?;
133        let data = resp.into_data()?;
134
135        info!("发布新动态的关注者列表: {:?}", data.items);
136        assert!(!data.items.is_empty());
137
138        Ok(())
139    }
140}