From a4ae52471a423e35f089b500dab35335ec714f61 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Wed, 11 Sep 2024 19:45:03 +0200 Subject: [PATCH] feat: update pdf api to return pdf names --- src/api/mod.rs | 21 ++++++++++++++------- static/index.js | 18 +++++++++--------- templates/index.html | 4 ++-- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/api/mod.rs b/src/api/mod.rs index 6b24749..fcfa580 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -27,7 +27,7 @@ pub fn get_routes() -> Router { ) } -async fn get_pdf_links() -> Result, reqwest::Error> { +async fn get_pdf() -> Result>, reqwest::Error> { if let Ok(metadata) = fs::metadata("pdf.json") { let last_modified = metadata.modified().unwrap(); let now = std::time::SystemTime::now(); @@ -45,10 +45,17 @@ async fn get_pdf_links() -> Result, reqwest::Error> { let dom = Html::parse_document(&response); let selector = Selector::parse("a:has(> strong)[href*=pdf]").unwrap(); - let mut result = Vec::new(); + let mut result: HashMap> = HashMap::new(); for element in dom.select(&selector) { - result.push(element.attr("href").unwrap().to_owned()); + result + .entry(String::from("links")) + .or_default() + .push(element.attr("href").unwrap().to_owned()); + result + .entry(String::from("names")) + .or_default() + .push(element.text().collect()); } let data = serde_json::to_string(&result).unwrap(); @@ -58,18 +65,18 @@ async fn get_pdf_links() -> Result, reqwest::Error> { } pub async fn pdf() -> impl IntoResponse { - match get_pdf_links().await { - Ok(x) => (StatusCode::OK, Json(json!({ "links": x}))), + match get_pdf().await { + Ok(x) => (StatusCode::OK, Json(json!(x["names"]))), Err(e) => (StatusCode::OK, Json(json!({ "error": e.to_string()}))), } } async fn download_pdf(id: u8) -> Result { - let links = get_pdf_links().await.unwrap(); + let links = get_pdf().await.unwrap(); if id as usize >= links.len() { return Err(failure::err_msg("Invalid ID")); } - let url = &links[id as usize]; + let url = &links["links"][id as usize]; let mut hasher = Sha1::new(); hasher.update(url.as_bytes()); diff --git a/static/index.js b/static/index.js index 82e8aa5..10a1377 100644 --- a/static/index.js +++ b/static/index.js @@ -1,21 +1,21 @@ -function load_links() { +function load_pdfs() { fetch("/api/pdf") .then((response) => response.json()) .then((data) => { let i = 0; - if (data.links === undefined || data.links.length === 0) { + if (data === undefined || data.length === 0) { show_error(null, "Nessun orario trovato"); return; } - data.links.forEach((el) => { - const select = document.getElementById("links"); + data.forEach((el) => { + const select = document.getElementById("pdf"); const option = document.createElement("option"); option.value = i; - option.text = el.split("/").pop().split(".pdf")[0]; + option.text = el; select.appendChild(option); i++; }); - document.getElementById("links-label").removeAttribute("aria-busy"); + document.getElementById("pdf-label").removeAttribute("aria-busy"); }) .catch((error) => { show_error(error); @@ -144,7 +144,7 @@ function make_table(url) { } function select_pdf() { - Cookies.set("id", document.getElementById("links").value); + Cookies.set("id", document.getElementById("pdf").value); reset_forms(); document.getElementById("type").value = "none"; document.getElementById("form-teacher").setAttribute("hidden", ""); @@ -160,7 +160,7 @@ function reset_forms() { } function select_type() { - let id = document.getElementById("links").value; + let id = document.getElementById("pdf").value; let value = document.getElementById("type").value; reset_forms(); if (value === "teacher") { @@ -253,5 +253,5 @@ if (Cookies.get("id") && Cookies.get("type")) { } } else { document.getElementById("forms").removeAttribute("hidden"); - load_links(); + load_pdfs(); } diff --git a/templates/index.html b/templates/index.html index 217f9b2..9aaa6a8 100644 --- a/templates/index.html +++ b/templates/index.html @@ -44,10 +44,10 @@