Skip to main content

bpi_rs/
auth.rs

1/// B站账号登录信息
2#[derive(Debug, Clone, serde::Deserialize)]
3pub struct Account {
4    pub dede_user_id: String,
5    pub dede_user_id_ckmd5: String,
6    pub sessdata: String,
7    pub bili_jct: String,
8    pub buvid3: String,
9}
10
11impl Account {
12    pub fn new(
13        dede_user_id: String,
14        dede_user_id_ckmd5: String,
15        sessdata: String,
16        bili_jct: String,
17        buvid3: String,
18    ) -> Self {
19        Self {
20            dede_user_id,
21            dede_user_id_ckmd5,
22            sessdata,
23            bili_jct,
24            buvid3,
25        }
26    }
27
28    pub fn is_complete(&self) -> bool {
29        !self.dede_user_id.is_empty()
30            && !self.sessdata.is_empty()
31            && !self.bili_jct.is_empty()
32            && !self.buvid3.is_empty()
33    }
34}
35
36impl Account {
37    #[cfg(any(test, debug_assertions))]
38    pub fn load_test_account() -> Result<Account, Box<dyn std::error::Error>> {
39        use config::{Config, File};
40        use std::path::Path;
41
42        let config_path = "account.toml";
43
44        // 如果测试配置文件不存在,创建一个模板
45        if !Path::new(config_path).exists() {
46            create_test_account_template(config_path)?;
47            return Err("测试账号配置文件已创建,请填写后重新运行".into());
48        }
49
50        let settings = Config::builder()
51            .add_source(File::with_name("account"))
52            .build()?;
53
54        Ok(settings.try_deserialize()?)
55    }
56}
57
58#[cfg(any(test, debug_assertions))]
59fn create_test_account_template(path: &str) -> Result<(), Box<dyn std::error::Error>> {
60    use std::fs;
61
62    let template = r#"# 测试账号配置文件
63# 请填写您的 B站 账号信息用于测试
64
65bili_jct = "your_bili_jct_here"
66dede_user_id = "your_dede_user_id_here"
67dede_user_id_ckmd5 = "your_dede_user_id_ckmd5_here"  
68sessdata = "your_sessdata_here"
69buvid3 = "your_buvid3_here"
70
71# 注意: 这个文件包含敏感信息,请不要提交到版本控制系统
72"#;
73
74    fs::write(path, template)?;
75    Ok(())
76}