Skip to main content

bpi_rs/video/info/
desc.rs

1//! 查询稿件简介相关接口
2//!
3//! [查看 API 文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/video)
4
5use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
6use serde::{ Deserialize, Serialize };
7
8/// 稿件简介响应
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct VideoDescResponse {
11    /// 返回码
12    /// - 0:成功
13    /// - -400:请求错误
14    /// - 62002:稿件不可见
15    pub code: i32,
16    /// 错误信息,默认为 "0"
17    pub message: String,
18    /// ttl,一般为1
19    pub ttl: i32,
20    /// 简介内容
21    pub data: String,
22}
23
24impl BpiClient {
25    /// 查询稿件简介
26    ///
27    /// # 文档
28    /// [查看API文档](https://socialsisteryi.github.io/bilibili-API-collect/docs/video/info.html#获取稿件简介)
29    ///
30    /// # 参数
31    /// | 名称   | 类型         | 说明                 |
32    /// | ------ | ------------| -------------------- |
33    /// | `aid`  | `Option<u64>` | 稿件 avid,可选      |
34    /// | `bvid` | `Option<&str>`| 稿件 bvid,可选      |
35    ///
36    /// 两者任选一个
37    pub async fn video_desc(
38        &self,
39        aid: Option<u64>,
40        bvid: Option<&str>
41    ) -> Result<BpiResponse<String>, BpiError> {
42        let mut builder = self.get("http://api.bilibili.com/x/web-interface/archive/desc");
43
44        if let Some(aid) = aid {
45            builder = builder.query(&[("aid", aid.to_string())]);
46        }
47        if let Some(bvid) = bvid {
48            builder = builder.query(&[("bvid", bvid)]);
49        }
50
51        builder.send_bpi("获取稿件简介").await
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[tokio::test]
60    async fn test_video_desc() {
61        let bpi = BpiClient::new();
62
63        match bpi.video_desc(Some(10001), None).await {
64            Ok(resp) => {
65                if resp.code == 0 {
66                    tracing::info!("稿件简介: {}", resp.data.unwrap());
67                } else {
68                    tracing::info!("请求失败: code={}, message={}", resp.code, resp.message);
69                }
70            }
71            Err(err) => {
72                panic!("请求出错: {}", err);
73            }
74        }
75    }
76}