Skip to main content

lnu_elytra/
lib.rs

1#[cfg(feature = "blocking")]
2pub mod blocking;
3
4#[cfg(feature = "__pyo3")]
5pub mod pyo3;
6
7mod course;
8mod error;
9mod method;
10mod utils;
11
12pub use course::{Course, Jxb};
13pub use error::Error;
14pub use method::SelectCourseResponse;
15
16use reqwest::Url;
17use scraper::Selector;
18use std::{collections::HashMap, sync::LazyLock};
19
20#[derive(Debug, Clone)]
21pub struct Client {
22    base_url: Url,
23    client: reqwest::Client,
24    stores: HashMap<String, String>, // input[type="hidden"]
25    #[cfg(feature = "cookie_override")]
26    cookie_override: Option<String>, // 覆盖cookie
27}
28
29impl Client {
30    const LOGIN_URL: &str = "/xtgl/login_slogin.html";
31    const PUBLIC_KEY_URL: &str = "/xtgl/login_getPublicKey.html";
32
33    const SELECT_COURSE_URL: &str = "/xsxk/zzxkyzb_xkBcZyZzxkYzb.html?gnmkdm=N253512";
34    const SELECT_COURSE_HTML_URL: &str = "/xsxk/zzxkyzb_cxZzxkYzbIndex.html?gnmkdm=N253512";
35    const SELECT_COURSE_DISPLAY_URL: &str = "/xsxk/zzxkyzb_cxZzxkYzbDisplay.html?gnmkdm=N253512";
36    const SELECT_COURSE_PART_DISPLAY_URL: &str =
37        "/xsxk/zzxkyzb_cxZzxkYzbPartDisplay.html?gnmkdm=N253512";
38    const SELECT_COURSE_QUERY_DO_WITH_COURSE_ID_URL: &str =
39        "/xsxk/zzxkyzbjk_cxJxbWithKchZzxkYzb.html?gnmkdm=N253512";
40
41    const S_CSRFTOKEN: LazyLock<Selector> =
42        LazyLock::new(|| Selector::parse("#csrftoken").unwrap());
43    const S_SESSION_USER_KEY: LazyLock<Selector> =
44        LazyLock::new(|| Selector::parse("#sessionUserKey").unwrap());
45    const S_INPUT_HIDDENT: LazyLock<Selector> =
46        LazyLock::new(|| Selector::parse("input[type='hidden']").unwrap());
47
48    pub fn new() -> Self {
49        Self {
50            base_url: Url::parse("http://jw.lingnan.edu.cn").unwrap(),
51            client: reqwest::Client::builder()
52                .cookie_store(true)
53                .build()
54                .unwrap(),
55            stores: HashMap::new(),
56            #[cfg(feature = "cookie_override")]
57            cookie_override: None,
58        }
59    }
60}