feat(api): implement 30-minute cache for PDF link requests
This commit is contained in:
parent
6a8dd1054c
commit
4524d84ab9
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
|||||||
/target
|
/target
|
||||||
pdf
|
pdf
|
||||||
csv
|
csv
|
||||||
|
pdf.json
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user