Skip to main content

bpi_rs/misc/
buvid.rs

1//! 获取 buvid3 (Web端)
2//!
3//! [文档](https://api.bilibili.com/x/web-frontend/getbuvid)
4
5use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
6use serde::{ Deserialize, Serialize };
7
8/// 获取 buvid3 - 响应数据
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct Buvid3Data {
11    /// buvid3,需要手动存放至 Cookie 中
12    pub buvid: String,
13}
14
15impl BpiClient {
16    /// 获取 buvid3
17    ///
18    /// # 文档
19    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/misc)
20    pub async fn misc_buvid3(&self) -> Result<BpiResponse<Buvid3Data>, BpiError> {
21        self.get("https://api.bilibili.com/x/web-frontend/getbuvid").send_bpi("获取 buvid3").await
22    }
23}
24
25/// 获取 buvid3/4 - 响应数据
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct BuvidData {
28    /// buvid3,需要手动存放至 Cookie 中
29    #[serde(rename = "b_3")]
30    pub buvid3: String,
31
32    /// buvid4,需要手动存放至 Cookie 中
33    #[serde(rename = "b_4")]
34    pub buvid4: String,
35}
36
37impl BpiClient {
38    /// 获取 buvid3 / buvid4
39    ///
40    /// # 文档
41    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/misc)
42    pub async fn misc_buvid(&self) -> Result<BpiResponse<BuvidData>, BpiError> {
43        self.get("https://api.bilibili.com/x/frontend/finger/spi").send_bpi("获取 buvid3/4").await
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[tokio::test]
52    async fn test_get_buvid3() {
53        let bpi = BpiClient::new();
54
55        match bpi.misc_buvid3().await {
56            Ok(resp) => {
57                if resp.code == 0 {
58                    let data = resp.data.unwrap();
59                    tracing::info!("获取 buvid3 成功: {}", data.buvid);
60                } else {
61                    tracing::info!("请求失败: code={}, message={}", resp.code, resp.message);
62                }
63            }
64            Err(err) => {
65                panic!("请求出错: {}", err);
66            }
67        }
68    }
69
70    #[tokio::test]
71    async fn test_get_buvid() {
72        let bpi = BpiClient::new();
73
74        match bpi.misc_buvid().await {
75            Ok(resp) => {
76                if resp.code == 0 {
77                    let data = resp.data.unwrap();
78                    tracing::info!("获取 buvid3 成功: {}", data.buvid3);
79                    tracing::info!("获取 buvid4 成功: {}", data.buvid4);
80                } else {
81                    tracing::info!("请求失败: code={}, message={}", resp.code, resp.message);
82                }
83            }
84            Err(err) => {
85                panic!("请求出错: {}", err);
86            }
87        }
88    }
89}