bpi_rs/dynamic/
action.rs

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