manaba_sdk/
cookie.rs

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