Compare commits

...

2 Commits

3 changed files with 26 additions and 19 deletions

View File

@ -27,7 +27,7 @@ pub fn get_routes() -> Router {
) )
} }
async fn get_pdf_links() -> Result<Vec<String>, reqwest::Error> { async fn get_pdf() -> Result<HashMap<String, Vec<String>>, reqwest::Error> {
if let Ok(metadata) = fs::metadata("pdf.json") { if let Ok(metadata) = fs::metadata("pdf.json") {
let last_modified = metadata.modified().unwrap(); let last_modified = metadata.modified().unwrap();
let now = std::time::SystemTime::now(); let now = std::time::SystemTime::now();
@ -45,10 +45,17 @@ async fn get_pdf_links() -> Result<Vec<String>, reqwest::Error> {
let dom = Html::parse_document(&response); let dom = Html::parse_document(&response);
let selector = Selector::parse("a:has(> strong)[href*=pdf]").unwrap(); let selector = Selector::parse("a:has(> strong)[href*=pdf]").unwrap();
let mut result = Vec::new(); let mut result: HashMap<String, Vec<String>> = HashMap::new();
for element in dom.select(&selector) { 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(); let data = serde_json::to_string(&result).unwrap();
@ -58,18 +65,18 @@ async fn get_pdf_links() -> Result<Vec<String>, reqwest::Error> {
} }
pub async fn pdf() -> impl IntoResponse { pub async fn pdf() -> impl IntoResponse {
match get_pdf_links().await { match get_pdf().await {
Ok(x) => (StatusCode::OK, Json(json!({ "links": x}))), Ok(x) => (StatusCode::OK, Json(json!(x["names"]))),
Err(e) => (StatusCode::OK, Json(json!({ "error": e.to_string()}))), Err(e) => (StatusCode::OK, Json(json!({ "error": e.to_string()}))),
} }
} }
async fn download_pdf(id: u8) -> Result<String, Error> { async fn download_pdf(id: u8) -> Result<String, Error> {
let links = get_pdf_links().await.unwrap(); let links = get_pdf().await.unwrap();
if id as usize >= links.len() { if id as usize >= links.len() {
return Err(failure::err_msg("Invalid ID")); 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(); let mut hasher = Sha1::new();
hasher.update(url.as_bytes()); hasher.update(url.as_bytes());
@ -211,7 +218,7 @@ 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(); let field = field.replace(" ", "").to_uppercase();
if field == "DOCENTE" { if field == "DOCENTE" {
docente = i as u8; docente = i as u8;

View File

@ -1,21 +1,21 @@
function load_links() { function load_pdfs() {
fetch("/api/pdf") fetch("/api/pdf")
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
let i = 0; let i = 0;
if (data.links === undefined || data.links.length === 0) { if (data === undefined || data.length === 0) {
show_error(null, "Nessun orario trovato"); show_error(null, "Nessun orario trovato");
return; return;
} }
data.links.forEach((el) => { data.forEach((el) => {
const select = document.getElementById("links"); const select = document.getElementById("pdf");
const option = document.createElement("option"); const option = document.createElement("option");
option.value = i; option.value = i;
option.text = el.split("/").pop().split(".pdf")[0]; option.text = el;
select.appendChild(option); select.appendChild(option);
i++; i++;
}); });
document.getElementById("links-label").removeAttribute("aria-busy"); document.getElementById("pdf-label").removeAttribute("aria-busy");
}) })
.catch((error) => { .catch((error) => {
show_error(error); show_error(error);
@ -144,7 +144,7 @@ function make_table(url) {
} }
function select_pdf() { function select_pdf() {
Cookies.set("id", document.getElementById("links").value); Cookies.set("id", document.getElementById("pdf").value);
reset_forms(); reset_forms();
document.getElementById("type").value = "none"; document.getElementById("type").value = "none";
document.getElementById("form-teacher").setAttribute("hidden", ""); document.getElementById("form-teacher").setAttribute("hidden", "");
@ -160,7 +160,7 @@ function reset_forms() {
} }
function select_type() { function select_type() {
let id = document.getElementById("links").value; let id = document.getElementById("pdf").value;
let value = document.getElementById("type").value; let value = document.getElementById("type").value;
reset_forms(); reset_forms();
if (value === "teacher") { if (value === "teacher") {
@ -253,5 +253,5 @@ if (Cookies.get("id") && Cookies.get("type")) {
} }
} else { } else {
document.getElementById("forms").removeAttribute("hidden"); document.getElementById("forms").removeAttribute("hidden");
load_links(); load_pdfs();
} }

View File

@ -44,10 +44,10 @@
<div id="forms" hidden> <div id="forms" hidden>
<form id="form-pdf"> <form id="form-pdf">
<fieldset> <fieldset>
<label id="links-label" aria-busy="true" <label id="pdf-label" aria-busy="true"
>Quale orario vuoi usare?</label >Quale orario vuoi usare?</label
> >
<select id="links" onchange="select_pdf()" required> <select id="pdf" onchange="select_pdf()" required>
<option selected disabled value="none"> <option selected disabled value="none">
Seleziona un orario Seleziona un orario
</option> </option>