feat(api): implement 30-minute cache for PDF link requests

This commit is contained in:
Mariano Riefolo 2024-09-04 16:25:27 +02:00
parent 6a8dd1054c
commit 4524d84ab9
2 changed files with 14 additions and 0 deletions

1
.gitignore vendored
View File

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

View File

@ -28,6 +28,16 @@ pub fn get_routes() -> Router {
}
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/")
.await?
.text()
@ -41,6 +51,9 @@ async fn get_pdf_links() -> Result<Vec<String>, reqwest::Error> {
result.push(element.attr("href").unwrap().to_owned());
}
let data = serde_json::to_string(&result).unwrap();
fs::write("pdf.json", data).unwrap();
Ok(result)
}