diff --git a/src/ics.rs b/src/ics.rs index 426976d..8287691 100644 --- a/src/ics.rs +++ b/src/ics.rs @@ -3,21 +3,7 @@ use ics::{ Event, ICalendar, }; -use chrono_tz::Europe::Paris; -use regex::Regex; - -type T = ( - // Schedules - Vec, - // Timetable per days with the semester as the key - (usize, Vec), -); -type D = std::collections::HashMap< - usize, - Vec<(chrono::DateTime, chrono::DateTime)>, ->; - -pub fn export(timetable: T, dates: D) -> ICalendar<'static> { +pub fn export(timetable: Vec, filename: &str) { let mut calendar = ICalendar::new("2.0", "cal8tor"); // Create event which contains the information regarding the course @@ -42,44 +28,5 @@ pub fn export(timetable: T, dates: D) -> ICalendar<'static> { // Add the course to the calendar calendar.add_event(event); - calendar.save_file("target/debug2.ics").unwrap(); - - let mut schedules = Vec::new(); - // h1 => heure de début | m1 => minute de début - // h2 => heure de fin | m2 => minute de fin - let re = - Regex::new(r"(?P

\d{1,2})h(?P\d{1,2})?.(?P

\d{1,2})h(?P\d{1,2})?").unwrap(); - for hour in timetable.0 { - let captures = re.captures(&hour).unwrap(); - - let h1 = match captures.name("h1") { - Some(h) => h.as_str().parse().unwrap(), - None => 0, - }; - let m1 = match captures.name("m1") { - Some(h) => h.as_str().parse().unwrap(), - None => 0, - }; - let h2 = match captures.name("h2") { - Some(h) => h.as_str().parse().unwrap(), - None => 0, - }; - let m2 = match captures.name("m2") { - Some(h) => h.as_str().parse().unwrap(), - None => 0, - }; - schedules.push(((h1, m1), (h2, m2))); - } - - let semester = (timetable.1).0; - let requested_timetable = (timetable.1).1; - - let mut requested_dates = Vec::new(); - for date in dates.get(&semester).unwrap() { - requested_dates.push((date.0.with_timezone(&Paris), date.1.with_timezone(&Paris))); - } - - println!("{:#?}", schedules); - - calendar + calendar.save_file(filename).unwrap(); } diff --git a/src/main.rs b/src/main.rs index 20b7070..73a82e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ -mod info; mod ics; +mod info; mod timetable; #[tokio::main] @@ -8,5 +8,7 @@ async fn main() { let info = info::info().await; - ics::export(timetable, info); + let builded_timetable = timetable::build(timetable, info); + + ics::export(builded_timetable, "target/debug2.ics"); } diff --git a/src/timetable.rs b/src/timetable.rs index 83917eb..0bd47f3 100644 --- a/src/timetable.rs +++ b/src/timetable.rs @@ -1,5 +1,8 @@ use scraper::{Html, Selector}; +use chrono_tz::Europe::Paris; +use regex::Regex; + pub mod models; /// Fetch the timetable for a class @@ -189,3 +192,56 @@ fn check_consistency(schedules: &Vec, timetable: &Vec) -> b checker } + +type T = ( + // Schedules + Vec, + // Timetable per days with the semester as the key + (usize, Vec), +); +type D = std::collections::HashMap< + usize, + Vec<(chrono::DateTime, chrono::DateTime)>, +>; + +/// Build the timetable +pub fn build(timetable: T, dates: D) -> Vec { + let mut schedules = Vec::new(); + // h1 => heure de début | m1 => minute de début + // h2 => heure de fin | m2 => minute de fin + let re = + Regex::new(r"(?P

\d{1,2})h(?P\d{1,2})?.(?P

\d{1,2})h(?P\d{1,2})?").unwrap(); + for hour in timetable.0 { + let captures = re.captures(&hour).unwrap(); + + let h1 = match captures.name("h1") { + Some(h) => h.as_str().parse().unwrap(), + None => 0, + }; + let m1 = match captures.name("m1") { + Some(h) => h.as_str().parse().unwrap(), + None => 0, + }; + let h2 = match captures.name("h2") { + Some(h) => h.as_str().parse().unwrap(), + None => 0, + }; + let m2 = match captures.name("m2") { + Some(h) => h.as_str().parse().unwrap(), + None => 0, + }; + schedules.push(((h1, m1), (h2, m2))); + } + + let semester = (timetable.1).0; + let requested_timetable = (timetable.1).1; + + let mut requested_dates = Vec::new(); + for date in dates.get(&semester).unwrap() { + requested_dates.push((date.0.with_timezone(&Paris), date.1.with_timezone(&Paris))); + } + + println!("{:#?}", schedules); + + vec!["cc".to_string()] +}