feat(api): add api endpoint for PDF link retrieval
This commit is contained in:
parent
dac624890a
commit
cc92e3360e
1113
Cargo.lock
generated
1113
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -5,4 +5,7 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
axum = "0.7.5"
|
||||
reqwest = "0.12.7"
|
||||
scraper = "0.20.0"
|
||||
serde_json = "1.0.127"
|
||||
tokio = { version = "1.40.0", features = ["full"] }
|
||||
|
36
src/api/mod.rs
Normal file
36
src/api/mod.rs
Normal file
@ -0,0 +1,36 @@
|
||||
use axum::{
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Json},
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use scraper::{Html, Selector};
|
||||
use serde_json::json;
|
||||
|
||||
pub fn get_routes() -> Router {
|
||||
Router::new().nest("/api", Router::new().route("/pdf", get(pdf)))
|
||||
}
|
||||
|
||||
async fn get_pdf_links() -> Result<Vec<String>, reqwest::Error> {
|
||||
let response = reqwest::get("https://cassandroferminervi.edu.it/orario-scolastico/")
|
||||
.await?
|
||||
.text()
|
||||
.await?;
|
||||
|
||||
let dom = Html::parse_document(&response);
|
||||
let selector = Selector::parse(".wp-block-list > li > a").unwrap();
|
||||
let mut result = Vec::new();
|
||||
|
||||
for element in dom.select(&selector) {
|
||||
result.push(element.attr("href").unwrap().to_owned());
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
pub async fn pdf() -> impl IntoResponse {
|
||||
match get_pdf_links().await {
|
||||
Ok(x) => (StatusCode::OK, Json(json!({ "links": x}))),
|
||||
Err(e) => (StatusCode::OK, Json(json!({ "error": e.to_string()}))),
|
||||
}
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
use std::net::SocketAddr;
|
||||
|
||||
mod api;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let app = axum::Router::new();
|
||||
let app = axum::Router::new().merge(api::get_routes());
|
||||
|
||||
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
|
||||
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();
|
||||
|
Loading…
Reference in New Issue
Block a user