Archived
1
0
Fork 0
forked from Anri/cal8tor

move related to building the final timetable to the timetable file

This commit is contained in:
Mylloon 2022-08-16 10:41:42 +02:00
parent 75e9b57862
commit df16ca03ba
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 62 additions and 57 deletions

View file

@ -3,21 +3,7 @@ use ics::{
Event, ICalendar,
};
use chrono_tz::Europe::Paris;
use regex::Regex;
type T = (
// Schedules
Vec<String>,
// Timetable per days with the semester as the key
(usize, Vec<crate::timetable::models::Day>),
);
type D = std::collections::HashMap<
usize,
Vec<(chrono::DateTime<chrono::Utc>, chrono::DateTime<chrono::Utc>)>,
>;
pub fn export(timetable: T, dates: D) -> ICalendar<'static> {
pub fn export(timetable: Vec<String>, 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<h1>\d{1,2})h(?P<m1>\d{1,2})?.(?P<h2>\d{1,2})h(?P<m2>\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();
}

View file

@ -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");
}

View file

@ -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<String>, timetable: &Vec<models::Day>) -> b
checker
}
type T = (
// Schedules
Vec<String>,
// Timetable per days with the semester as the key
(usize, Vec<crate::timetable::models::Day>),
);
type D = std::collections::HashMap<
usize,
Vec<(chrono::DateTime<chrono::Utc>, chrono::DateTime<chrono::Utc>)>,
>;
/// Build the timetable
pub fn build(timetable: T, dates: D) -> Vec<String> {
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<h1>\d{1,2})h(?P<m1>\d{1,2})?.(?P<h2>\d{1,2})h(?P<m2>\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()]
}