Archived
1
0
Fork 0
forked from Anri/cal8tor

add http error detection

This commit is contained in:
Mylloon 2022-08-16 15:48:13 +02:00
parent 806f210c0f
commit 63f5b6dc34
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 33 additions and 14 deletions

View file

@ -59,12 +59,13 @@ pub async fn info() -> HashMap<usize, Vec<(DateTime<Utc>, i64)>> {
/// Get info webpage
async fn get_webpage() -> Result<Html, Box<dyn std::error::Error>> {
/* let html = reqwest::get("https://informatique.up8.edu/licence-iv/edt").await?.text().await?;
let url = "https://informatique.up8.edu/licence-iv/edt";
let html = reqwest::get(url).await?.text().await?;
Ok(Html::parse_document(&html)) */
// Panic on error
crate::utils::check_errors(&html, url);
let html = include_str!("../target/debug2.html");
Ok(Html::parse_document(html))
Ok(Html::parse_document(&html))
}
/// Turn a french date to an english one

View file

@ -1,13 +1,17 @@
mod ics;
mod info;
mod timetable;
mod utils;
#[tokio::main]
async fn main() {
println!("Fetch the timetable...");
let timetable = timetable::timetable(3, 1, None).await;
println!("Fetch informations about the year...");
let info = info::info().await;
println!("Build the ICS file...");
let builded_timetable = timetable::build(timetable, info);
ics::export(builded_timetable, "target/debug.ics");

View file

@ -111,7 +111,7 @@ async fn get_webpage(
semester: i8,
letter: Option<char>,
) -> Result<Html, Box<dyn std::error::Error>> {
/* let url = {
let url = {
let panic_semester_message = "Unknown semester.";
let panic_letter_message = "Unknown letter.";
@ -153,14 +153,13 @@ async fn get_webpage(
};
// Get raw html
let html = reqwest::get(url).await?.text().await?;
let html = reqwest::get(&url).await?.text().await?;
// Panic on error
crate::utils::check_errors(&html, &url);
// Parse document
let document = Html::parse_document(&html); */
println!("Fetch 'L{}{} (s{})'", year, letter.unwrap_or(' '), semester);
let html = include_str!("../target/debug.html");
let document = Html::parse_document(html);
let document = Html::parse_document(&html);
Ok(document)
}
@ -242,8 +241,8 @@ pub fn build(timetable: T, dates: D) -> Vec<models::Course> {
let mut semester = Vec::new();
// Start date of the back-to-school week
let break_data = dates.get(&timetable.1 .0).unwrap();
let before_break = break_data.get(0).unwrap();
let datetimes = dates.get(&timetable.1 .0).unwrap();
let before_break = datetimes.get(0).unwrap();
let mut date = before_break.0;
let mut rep = before_break.1;
// For each weeks
@ -273,7 +272,7 @@ pub fn build(timetable: T, dates: D) -> Vec<models::Course> {
// From friday to monday
date += Duration::days(2);
}
let after_break = break_data.get(1).unwrap();
let after_break = datetimes.get(1).unwrap();
date = after_break.0;
rep = after_break.1;
}

15
src/utils.rs Normal file
View file

@ -0,0 +1,15 @@
/// Panic if an error happened
pub fn check_errors(html: &String, loc: &str) {
match html {
t if t.contains(&err_code(429)) => panic!(
"URL: {} • HTTP 429: Slow down - Rate limited (too many access attempts detected)",
loc
),
_ => (),
}
}
/// Create String error code
fn err_code(code: i32) -> String {
format!("HTTP Code : {}", code)
}