Skip to main content

bpi_rs/dynamic/
action.rs

1use serde_json::json;
2
3use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
4
5impl BpiClient {
6    /// 点赞动态
7    ///
8    /// # 文档
9    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/dynamic)
10    ///
11    /// # 参数
12    ///
13    /// | 名称 | 类型 | 说明 |
14    /// | ---- | ---- | ---- |
15    /// | `dyn_id_str` | &str | 动态 ID |
16    /// | `up` | u8 | 点赞状态:0 切换,1 点赞,2 取消 |
17    pub async fn dynamic_like(
18        &self,
19        dyn_id_str: &str,
20        up: u8
21    ) -> Result<BpiResponse<serde_json::Value>, BpiError> {
22        let csrf = self.csrf()?;
23
24        let json_body =
25            json!({
26            "dyn_id_str": dyn_id_str,
27            "up": up ,
28            "spmid":"333.1369.0.0",
29            "from_spmid":"333.999.0.0"
30
31        });
32
33        self
34            .post("https://api.bilibili.com/x/dynamic/feed/dyn/thumb")
35            .query(&[("csrf", csrf)])
36            .json(&json_body)
37            .send_bpi("点赞动态").await
38    }
39
40    /// 删除定时发布动态
41    ///
42    /// # 文档
43    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/dynamic)
44    ///
45    /// # 参数
46    ///
47    /// | 名称 | 类型 | 说明 |
48    /// | ---- | ---- | ---- |
49    /// | `draft_id` | &str | 定时发布动态 ID |
50    pub async fn dynamic_remove_draft(
51        &self,
52        draft_id: &str
53    ) -> Result<BpiResponse<serde_json::Value>, BpiError> {
54        let csrf = self.csrf()?;
55
56        self
57            .post("https://api.vc.bilibili.com/dynamic_draft/v1/dynamic_draft/rm_draft")
58            .form(
59                &[
60                    ("draft_id", draft_id),
61                    ("csrf", csrf.as_str()),
62                ]
63            )
64            .send_bpi("删除定时发布动态").await
65    }
66
67    /// 设置置顶动态
68    ///
69    /// # 文档
70    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/dynamic)
71    ///
72    /// # 参数
73    ///
74    /// | 名称 | 类型 | 说明 |
75    /// | ---- | ---- | ---- |
76    /// | `dyn_str` | &str | 动态 ID |
77    pub async fn dynamic_set_top(
78        &self,
79        dyn_str: &str
80    ) -> Result<BpiResponse<serde_json::Value>, BpiError> {
81        let csrf = self.csrf()?;
82        let json_body = json!({
83            "dyn_str": dyn_str,
84        });
85
86        self
87            .post("https://api.bilibili.com/x/dynamic/feed/space/set_top")
88            .query(&[("csrf", csrf)])
89            .json(&json_body)
90            .send_bpi("设置置顶动态").await
91    }
92
93    /// 取消置顶动态
94    ///
95    /// # 文档
96    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/dynamic)
97    ///
98    /// # 参数
99    ///
100    /// | 名称 | 类型 | 说明 |
101    /// | ---- | ---- | ---- |
102    /// | `dyn_str` | &str | 动态 ID |
103    pub async fn dynamic_remove_top(
104        &self,
105        dyn_str: &str
106    ) -> Result<BpiResponse<serde_json::Value>, BpiError> {
107        let csrf = self.csrf()?;
108        let json_body = json!({
109            "dyn_str": dyn_str,
110        });
111
112        self
113            .post("https://api.bilibili.com/x/dynamic/feed/space/rm_top")
114            .query(&[("csrf", csrf)])
115            .json(&json_body)
116            .send_bpi("取消置顶动态").await
117    }
118}
119
120#[cfg(test)]
121mod tests {
122    use super::*;
123
124    #[tokio::test]
125    async fn test_dynamic_like() {
126        let bpi = BpiClient::new();
127        let dynamic_id = "1099138163191840776";
128
129        // 测试新版点赞 API
130        let resp_new = bpi.dynamic_like(dynamic_id, 1).await;
131        assert!(resp_new.is_ok());
132    }
133
134    #[tokio::test]
135    #[ignore]
136    async fn test_dynamic_top() {
137        let bpi = BpiClient::new();
138        // 替换为你需要置顶或取消置顶的动态ID
139        let dynamic_id = "1099138163191840776";
140
141        // 测试置顶
142        let resp_set_top = bpi.dynamic_set_top(dynamic_id).await;
143        assert!(resp_set_top.is_ok());
144
145        // 测试取消置顶
146        let resp_rm_top = bpi.dynamic_remove_top(dynamic_id).await;
147        if resp_rm_top.is_err() {
148            tracing::info!("取消置顶失败: {}", resp_rm_top.err().unwrap());
149        }
150    }
151}