feat(api): add api endpoint for PDF link retrieval

This commit is contained in:
Mariano Riefolo 2024-08-30 17:34:43 +02:00
parent dac624890a
commit cc92e3360e
4 changed files with 1152 additions and 4 deletions

1113
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -5,4 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
axum = "0.7.5" axum = "0.7.5"
reqwest = "0.12.7"
scraper = "0.20.0"
serde_json = "1.0.127"
tokio = { version = "1.40.0", features = ["full"] } tokio = { version = "1.40.0", features = ["full"] }

36
src/api/mod.rs Normal file
View 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()}))),
}
}

View File

@ -1,8 +1,10 @@
use std::net::SocketAddr; use std::net::SocketAddr;
mod api;
#[tokio::main] #[tokio::main]
async fn 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 addr = SocketAddr::from(([0, 0, 0, 0], 3000));
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap(); let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();