Skip to main content

lnu_elytra/method/
init.rs

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