add login

This commit is contained in:
Mylloon 2023-01-13 18:26:21 +01:00
parent c2c7e1f8ee
commit cb228930d9
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 63 additions and 6 deletions

View file

@ -69,8 +69,6 @@ async fn main() {
let user_agent = format!("uwm/{}", env!("CARGO_PKG_VERSION"));
let marks = marks::get_marks(config.username, config.password.unwrap(), &user_agent).await;
println!("{:#?}", marks);
marks::get_marks(config.username, config.password.unwrap(), &user_agent).await;
}
}

View file

@ -1,9 +1,68 @@
pub async fn get_marks(username: String, password: String, user_agent: &str) -> String {
format!("{}/{}/{}", username, password, user_agent)
use scraper::{Html, Selector};
use std::io::Write;
pub async fn get_marks(username: String, password: String, user_agent: &str) {
// Login
// https://cas.univ-paris8.fr/cas/login
let document = login(username, password, user_agent)
.await
.expect("Can't login");
let mut w = std::fs::File::create("temp.html").unwrap();
write!(&mut w, "{}", document.html()).unwrap();
// Marks
// https://scolarite-etudiant.univ-paris8.fr/mondossierweb/#!notesView
}
async fn login(
username: String,
password: String,
user_agent: &str,
) -> Result<Html, Box<dyn std::error::Error>> {
let login_url = "https://cas.univ-paris8.fr/cas/login";
let client = reqwest::Client::builder().user_agent(user_agent).build()?;
let login_page = Html::parse_document(&client.get(login_url).send().await?.text().await?);
// LT
let sel_lt = Selector::parse(r#"input[name="lt"]"#).unwrap();
let lt = login_page
.select(&sel_lt)
.next()
.unwrap()
.value()
.attr("value")
.unwrap();
// Execution
let sel_execution = Selector::parse(r#"input[name="execution"]"#).unwrap();
let execution = login_page
.select(&sel_execution)
.next()
.unwrap()
.value()
.attr("value")
.unwrap();
let params = [
("username", username.as_str()),
("password", password.as_str()),
("_eventID", "submit"),
("submit", "SE CONNECTER"),
("lt", lt),
("execution", execution),
];
let html = client
.post(login_url)
.form(&params)
.send()
.await?
.text()
.await?;
let document = Html::parse_document(&html);
Ok(document)
}