feat: update pdf api to return pdf names
This commit is contained in:
parent
c4b47c86d5
commit
a4ae52471a
@ -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") {
|
||||
let last_modified = metadata.modified().unwrap();
|
||||
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 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) {
|
||||
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<Vec<String>, 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<String, Error> {
|
||||
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());
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -44,10 +44,10 @@
|
||||
<div id="forms" hidden>
|
||||
<form id="form-pdf">
|
||||
<fieldset>
|
||||
<label id="links-label" aria-busy="true"
|
||||
<label id="pdf-label" aria-busy="true"
|
||||
>Quale orario vuoi usare?</label
|
||||
>
|
||||
<select id="links" onchange="select_pdf()" required>
|
||||
<select id="pdf" onchange="select_pdf()" required>
|
||||
<option selected disabled value="none">
|
||||
Seleziona un orario
|
||||
</option>
|
||||
|
Loading…
Reference in New Issue
Block a user