find size of days

This commit is contained in:
Mylloon 2023-09-18 20:52:18 +02:00
parent ff95bf09b5
commit 1dbfe12566
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -26,102 +26,45 @@ pub async fn timetable(
.await .await
.expect("Can't reach timetable website."); .expect("Can't reach timetable website.");
(vec![], (0, vec![]))
// Selectors // Selectors
/* let sel_table = Selector::parse("table").unwrap(); let sel_table = Selector::parse("table").unwrap();
let sel_thead = Selector::parse("thead").unwrap();
let sel_tr = Selector::parse("tr").unwrap(); let sel_tr = Selector::parse("tr").unwrap();
let sel_tbody = Selector::parse("tbody").unwrap();
let sel_th = Selector::parse("th").unwrap(); let sel_th = Selector::parse("th").unwrap();
let sel_td = Selector::parse("td").unwrap();
let sel_em = Selector::parse("em").unwrap();
let sel_small = Selector::parse("small").unwrap();
let sel_strong = Selector::parse("strong").unwrap();
// Find the timetable // Find the timetable
let raw_timetable = document.select(&sel_table).next().unwrap(); let raw_timetable = document.select(&sel_table).next().unwrap();
// Find the slots available for the timetable // Find days
let raw_schedules = raw_timetable.select(&sel_tr).next().unwrap(); let days_size: Vec<_> = raw_timetable
.select(&sel_thead)
// Find availables schedules .next()
let mut schedules = Vec::new(); .unwrap()
for time in raw_schedules.select(&sel_th) { .select(&sel_tr)
schedules.push(time.inner_html()); .next()
} .unwrap()
.select(&sel_th)
// Find the timetable values .next()
let raw_timetable_values = raw_timetable.select(&sel_tbody).next().unwrap(); .unwrap()
.next_siblings()
// For each days .flat_map(|i| {
let mut timetable = Vec::new(); let element = i.value().as_element().unwrap();
let span_regex = Regex::new(r"<span.*</span>").unwrap(); element
for day in raw_timetable_values.select(&sel_tr) { .attrs()
let mut courses_vec = Vec::new(); .filter_map(|f| {
let mut location_tracker = 0; if f.0.contains("colspan") {
for course in day.select(&sel_td) { Some(f.1)
if course.inner_html() == "" { } else {
courses_vec.push(None); None
location_tracker += 1; }
} else { })
courses_vec.push(Some(models::Course { .collect::<Vec<_>>()
name: match course.select(&sel_em).next() {
Some(value) => span_regex.replace(&value.inner_html(), " ").to_string(),
None => span_regex
.replace(course.inner_html().split("<br>").next().unwrap(), " ")
.to_string(),
},
professor: match course
.select(&sel_small)
.next()
.unwrap()
.inner_html()
.split("<br>")
.next()
{
Some(data) => {
if data.contains("</strong>") {
// This is the room, so there is no professor assigned
// to this courses yet
None
} else {
Some(data.to_string())
}
}
None => None,
},
room: capitalize(&mut match course.select(&sel_strong).next() {
Some(el) => el.inner_html().replace("<br>", ""),
// Error in the site, silently passing... (the room is probably at the professor member)
None => String::new(),
}),
start: location_tracker,
size: match course.value().attr("colspan") {
Some(i) => i.parse().unwrap(),
None => 1,
},
dtstart: None,
dtend: None,
}));
match &courses_vec[courses_vec.len() - 1] {
Some(course) => location_tracker += course.size,
None => location_tracker += 1,
}
}
}
timetable.push(models::Day {
name: day.select(&sel_th).next().unwrap().inner_html(),
courses: courses_vec,
}) })
} .collect();
if !check_consistency(&schedules, &timetable) { println!("{:#?}", days);
panic!("Error when building the timetable.");
}
(schedules, (semester as usize, timetable)) */ (vec![], (0, vec![]))
} }
/// Get timetable webpage /// Get timetable webpage