manaba_sdk/
cookie.rs

1use crate::error::{ManabaError, Result};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Deserialize, Serialize, Clone, Default)]
5pub struct Cookie(pub String);
6
7#[allow(unused)]
8impl Cookie {
9    pub fn new(cookie: &str) -> Self {
10        Cookie(cookie.to_owned())
11    }
12
13    pub fn load(domain: &str) -> Result<Self> {
14        let domains = vec![domain.to_owned()];
15        let domains = Some(domains);
16
17        let mut browsers = [
18            rookie::chrome,
19            rookie::chromium,
20            rookie::zen,
21            rookie::brave,
22            rookie::arc,
23        ];
24
25        for browser in browsers {
26            let mut cookies = match browser(domains.clone()) {
27                Ok(v) => v,
28                Err(_) => continue,
29            };
30
31            cookies
32                .iter_mut()
33                .for_each(|s| s.name = s.name.trim().to_string());
34
35            if cookies.is_empty() {
36                continue;
37            }
38
39            if !cookies.iter().any(|v| v.name == "sessionid") {
40                continue;
41            }
42
43            let cookie = cookies
44                .iter()
45                .map(|cookie| format!("{}={}", cookie.name, cookie.value))
46                .collect::<Vec<_>>()
47                .join(";");
48
49            return Ok(Cookie(cookie));
50        }
51
52        Err(ManabaError::LoadCookie("Cookie not found".to_owned()))
53    }
54}