From 63f5b6dc34c945c91ee0744826670ca1acec7012 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 16 Aug 2022 15:48:13 +0200 Subject: [PATCH] add http error detection --- src/info.rs | 9 +++++---- src/main.rs | 4 ++++ src/timetable.rs | 19 +++++++++---------- src/utils.rs | 15 +++++++++++++++ 4 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 src/utils.rs diff --git a/src/info.rs b/src/info.rs index 2ff91c0..37df4a5 100644 --- a/src/info.rs +++ b/src/info.rs @@ -59,12 +59,13 @@ pub async fn info() -> HashMap, i64)>> { /// Get info webpage async fn get_webpage() -> Result> { - /* 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 diff --git a/src/main.rs b/src/main.rs index f643ae0..96435ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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"); diff --git a/src/timetable.rs b/src/timetable.rs index eb5d98a..036d9da 100644 --- a/src/timetable.rs +++ b/src/timetable.rs @@ -111,7 +111,7 @@ async fn get_webpage( semester: i8, letter: Option, ) -> Result> { - /* 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 { 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 { // 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; } diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..44dec89 --- /dev/null +++ b/src/utils.rs @@ -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) +}