Skip to main content

bpi_rs/video/
report.rs

1//! 视频观看进度上报相关接口
2//!
3//! [查看 API 文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/video)
4use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
5
6impl BpiClient {
7    /// 上报视频观看进度(双端)
8    ///
9    /// # 文档
10    /// [查看API文档](https://socialsisteryi.github.io/bilibili-API-collect/docs/video/report.html#上报视频观看进度)
11    ///
12    /// # 参数
13    /// | 名称      | 类型         | 说明                 |
14    /// | --------- | ------------| -------------------- |
15    /// | `aid`     | u64         | 稿件 avid            |
16    /// | `cid`     | u64         | 视频 cid             |
17    /// | `progress`| `Option<u64>` | 观看进度,单位为秒,可选,默认0 |
18    pub async fn video_report_watch_progress(
19        &self,
20        aid: u64,
21        cid: u64,
22        progress: Option<u64>
23    ) -> Result<BpiResponse<serde_json::Value>, BpiError> {
24        let csrf = self.csrf()?;
25
26        let mut form = reqwest::multipart::Form
27            ::new()
28            .text("aid", aid.to_string())
29            .text("cid", cid.to_string())
30            .text("csrf", csrf.to_string());
31
32        if let Some(p) = progress {
33            form = form.text("progress", p.to_string());
34        } else {
35            form = form.text("progress", "0");
36        }
37
38        self
39            .post("https://api.bilibili.com/x/v2/history/report")
40            .multipart(form)
41            .send_bpi("上报观看进度").await
42    }
43}
44
45// --- 测试模块 ---
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50    use tracing::info;
51
52    const TEST_AID: u64 = 10001;
53    const TEST_CID: u64 = 16546;
54
55    #[tokio::test]
56    async fn test_report_watch_progress() -> Result<(), BpiError> {
57        let bpi = BpiClient::new();
58        // 上报观看进度为 1248 秒
59        let resp = bpi.video_report_watch_progress(TEST_AID, TEST_CID, Some(120)).await?;
60
61        info!("上报观看进度结果: {:?}", resp);
62
63        Ok(())
64    }
65}