Compare commits

..

4 Commits

3 changed files with 75 additions and 28 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/target /target
pdf pdf
csv csv
pdf.json

View File

@ -28,6 +28,16 @@ pub fn get_routes() -> Router {
} }
async fn get_pdf_links() -> Result<Vec<String>, reqwest::Error> { async fn get_pdf_links() -> Result<Vec<String>, reqwest::Error> {
if let Ok(metadata) = fs::metadata("pdf.json") {
let last_modified = metadata.modified().unwrap();
let now = std::time::SystemTime::now();
let diff = now.duration_since(last_modified).unwrap().as_secs();
if diff < 30 * 60 {
let data = fs::read_to_string("pdf.json").unwrap();
return Ok(serde_json::from_str(&data).unwrap());
}
}
let response = reqwest::get("https://cassandroferminervi.edu.it/orario-scolastico/") let response = reqwest::get("https://cassandroferminervi.edu.it/orario-scolastico/")
.await? .await?
.text() .text()
@ -41,6 +51,9 @@ async fn get_pdf_links() -> Result<Vec<String>, reqwest::Error> {
result.push(element.attr("href").unwrap().to_owned()); result.push(element.attr("href").unwrap().to_owned());
} }
let data = serde_json::to_string(&result).unwrap();
fs::write("pdf.json", data).unwrap();
Ok(result) Ok(result)
} }
@ -158,12 +171,7 @@ pub async fn get_class(Query(params): Query<FilterByClassQuery>) -> impl IntoRes
for record in rdr.records() { for record in rdr.records() {
let record = match record { let record = match record {
Ok(x) => x, Ok(x) => x,
Err(_) => { Err(_) => continue,
return (
StatusCode::UNPROCESSABLE_ENTITY,
Json(json!({ "errore": "Formato non supportato"})),
)
}
}; };
for (i, cell) in record.iter().enumerate() { for (i, cell) in record.iter().enumerate() {
if cell == params.classe { if cell == params.classe {
@ -202,6 +210,8 @@ pub fn get_header(record: &StringRecord) -> Header {
.position(|x| !x.is_empty()) .position(|x| !x.is_empty())
.unwrap_or(record.len()); .unwrap_or(record.len());
let field = field.replace("ì", "i").replace(" ", "").to_uppercase();
if field == "DOCENTE" { if field == "DOCENTE" {
docente = i as u8; docente = i as u8;
} else if field == "LUNEDI" { } else if field == "LUNEDI" {
@ -250,12 +260,7 @@ pub async fn get_teachers(Query(params): Query<GetTeachersQuery>) -> impl IntoRe
for record in rdr.records() { for record in rdr.records() {
let record = match record { let record = match record {
Ok(x) => x, Ok(x) => x,
Err(_) => { Err(_) => continue,
return (
StatusCode::UNPROCESSABLE_ENTITY,
Json(json!({ "errore": "Formato non supportato"})),
)
}
}; };
if let Some(teacher) = record.get(header.teacher as usize) { if let Some(teacher) = record.get(header.teacher as usize) {
@ -304,7 +309,10 @@ pub async fn get_classes(Query(params): Query<GetClassesQuery>) -> impl IntoResp
let mut classes: Vec<HashMap<char, HashSet<String>>> = vec![HashMap::new(); 5]; let mut classes: Vec<HashMap<char, HashSet<String>>> = vec![HashMap::new(); 5];
for record in rdr.records() { for record in rdr.records() {
let record = record.unwrap(); let record = match record {
Ok(x) => x,
Err(_) => continue,
};
for (i, cell) in record.iter().enumerate() { for (i, cell) in record.iter().enumerate() {
for (_, range) in header.weekdays.clone() { for (_, range) in header.weekdays.clone() {
if range.contains(&i) && is_valid_class(cell) { if range.contains(&i) && is_valid_class(cell) {

View File

@ -54,7 +54,7 @@ function load_classes(id) {
} }
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
const option = document.createElement("option"); const option = document.createElement("option");
option.value = i; option.value = i + 1;
option.text = i + 1; option.text = i + 1;
select.appendChild(option); select.appendChild(option);
} }
@ -71,6 +71,16 @@ function make_teacher_table(id, teacher) {
fetch(`/api/professore?id=${id}&professore=${teacher}`) fetch(`/api/professore?id=${id}&professore=${teacher}`)
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
for (let key in data) {
let i = data[key].length - 1;
while (i >= 0 && data[key][i] === "") {
data[key].pop();
i--;
}
}
const columns = Math.max(
...Object.values(data).map((vettore) => vettore.length),
);
let table = document.getElementById("table"); let table = document.getElementById("table");
let tbody = document.createElement("tbody"); let tbody = document.createElement("tbody");
let week = [ let week = [
@ -87,12 +97,21 @@ function make_teacher_table(id, teacher) {
th.scope = "row"; th.scope = "row";
th.textContent = weekday; th.textContent = weekday;
tr.appendChild(th); tr.appendChild(th);
data[weekday].forEach((teacher_class) => { if (data[weekday] != null) {
let td = document.createElement("td"); let inserted = 0;
td.textContent = teacher_class; data[weekday].forEach((teacher_class) => {
tr.appendChild(td); let td = document.createElement("td");
}); td.textContent = teacher_class;
tbody.appendChild(tr); tr.appendChild(td);
inserted++;
});
while (inserted < columns) {
let td = document.createElement("td");
tr.appendChild(td);
inserted++;
}
tbody.appendChild(tr);
}
}); });
table.appendChild(tbody); table.appendChild(tbody);
document.getElementById("table").removeAttribute("aria-busy"); document.getElementById("table").removeAttribute("aria-busy");
@ -107,6 +126,16 @@ function make_student_table(id, student_class) {
fetch(`/api/classe?id=${id}&classe=${student_class}`) fetch(`/api/classe?id=${id}&classe=${student_class}`)
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
for (let key in data) {
let i = data[key].length - 1;
while (i >= 0 && data[key][i] === "") {
data[key].pop();
i--;
}
}
const columns = Math.max(
...Object.values(data).map((vettore) => vettore.length),
);
let table = document.getElementById("table"); let table = document.getElementById("table");
let tbody = document.createElement("tbody"); let tbody = document.createElement("tbody");
let week = [ let week = [
@ -123,12 +152,21 @@ function make_student_table(id, student_class) {
th.scope = "row"; th.scope = "row";
th.textContent = weekday; th.textContent = weekday;
tr.appendChild(th); tr.appendChild(th);
data[weekday].forEach((student_class) => { if (data[weekday] != null) {
let td = document.createElement("td"); let inserted = 0;
td.textContent = student_class; data[weekday].forEach((student_class) => {
tr.appendChild(td); let td = document.createElement("td");
}); td.textContent = student_class;
tbody.appendChild(tr); tr.appendChild(td);
inserted++;
});
while (inserted < columns) {
let td = document.createElement("td");
tr.appendChild(td);
inserted++;
}
tbody.appendChild(tr);
}
}); });
table.appendChild(tbody); table.appendChild(tbody);
document.getElementById("table").removeAttribute("aria-busy"); document.getElementById("table").removeAttribute("aria-busy");
@ -168,7 +206,7 @@ function load_section() {
i--; i--;
} }
} }
Object.keys(data[year]).forEach((el) => { Object.keys(data[year - 1]).forEach((el) => {
const option = document.createElement("option"); const option = document.createElement("option");
option.value = el; option.value = el;
option.text = el; option.text = el;
@ -187,7 +225,7 @@ function load_major() {
i--; i--;
} }
} }
data[year][section].forEach((el) => { data[year - 1][section].forEach((el) => {
const option = document.createElement("option"); const option = document.createElement("option");
option.value = el; option.value = el;
option.text = el; option.text = el;