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