Skip to main content

lnu_elytra/
lib.rs

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