Skip to main content

lnu_elytra/method/
init.rs

1use serde::Serialize;
2
3use crate::{
4    Client,
5    error::{Error, R},
6    utils::{ToHtml, macros::info},
7};
8
9impl Client {
10    /// 获取选课基本参数,每次选课只需要执行一次
11    pub async fn init(&mut self) -> R {
12        info!("正在获取选课基本参数...");
13
14        let index_doc = self
15            .get(&Client::SELECT_COURSE_HTML_URL)
16            .send()
17            .await?
18            .doc()
19            .await?;
20
21        for item in index_doc.select(&Client::S_INPUT_HIDDENT) {
22            let name = item.attr("name").unwrap_or("");
23            let value = item.attr("value").unwrap_or("");
24            self.store(name, value);
25        }
26
27        #[derive(Serialize, Debug)]
28        struct DisplayRequestData<'a> {
29            xkkz_id: &'a str, // N253512
30            xszxzt: &'a str,  // 1
31            kspage: &'a str,  // 1
32        }
33
34        let display_data = DisplayRequestData {
35            xkkz_id: self.stores.get("firstXkkzId").ok_or(Error::NotyetStarted)?,
36            xszxzt: "1".into(),
37            kspage: "1".into(),
38        };
39
40        let display_doc = self
41            .post(&Client::SELECT_COURSE_DISPLAY_URL)
42            .form(&display_data)
43            .send()
44            .await?
45            .doc()
46            .await?;
47
48        for item in display_doc.select(&Client::S_INPUT_HIDDENT) {
49            let name = item.attr("name").unwrap_or("");
50            let value = item.attr("value").unwrap_or("");
51            self.store(name, value);
52        }
53
54        info!("选课基本参数获取成功");
55
56        Ok(())
57    }
58}