Compare commits

..

10 Commits

10 changed files with 552 additions and 10 deletions

1
.gitignore vendored
View File

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

145
Cargo.lock generated
View File

@ -30,6 +30,50 @@ dependencies = [
"zerocopy",
]
[[package]]
name = "askama"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b79091df18a97caea757e28cd2d5fda49c6cd4bd01ddffd7ff01ace0c0ad2c28"
dependencies = [
"askama_derive",
"askama_escape",
"humansize",
"num-traits",
"percent-encoding",
]
[[package]]
name = "askama_derive"
version = "0.12.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19fe8d6cb13c4714962c072ea496f3392015f0989b1a2847bb4b2d9effd71d83"
dependencies = [
"askama_parser",
"basic-toml",
"mime",
"mime_guess",
"proc-macro2",
"quote",
"serde",
"syn 2.0.76",
]
[[package]]
name = "askama_escape"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341"
[[package]]
name = "askama_parser"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acb1161c6b64d1c3d83108213c2a2533a342ac225aabd0bda218278c2ddb00c0"
dependencies = [
"nom",
]
[[package]]
name = "async-trait"
version = "0.1.81"
@ -129,6 +173,15 @@ version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "basic-toml"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "823388e228f614e9558c6804262db37960ec8821856535f5c3f59913140558f8"
dependencies = [
"serde",
]
[[package]]
name = "bitflags"
version = "2.6.0"
@ -582,6 +635,12 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "http-range-header"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08a397c49fec283e3d6211adbe480be95aae5f304cfb923e9970e08956d5168a"
[[package]]
name = "httparse"
version = "1.9.4"
@ -594,6 +653,15 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
[[package]]
name = "humansize"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6cb51c9a029ddc91b07a787f1d86b53ccfa49b0e86688c946ebe8d3555685dd7"
dependencies = [
"libm",
]
[[package]]
name = "hyper"
version = "1.4.1"
@ -715,6 +783,12 @@ version = "0.2.158"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439"
[[package]]
name = "libm"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058"
[[package]]
name = "linux-raw-sys"
version = "0.4.14"
@ -775,6 +849,22 @@ version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]]
name = "mime_guess"
version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e"
dependencies = [
"mime",
"unicase",
]
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "miniz_oxide"
version = "0.7.4"
@ -819,6 +909,25 @@ version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086"
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "object"
version = "0.36.4"
@ -882,6 +991,7 @@ dependencies = [
name = "orario-scolastico-itet"
version = "0.1.0"
dependencies = [
"askama",
"axum",
"csv",
"failure",
@ -892,6 +1002,7 @@ dependencies = [
"serde_json",
"sha1",
"tokio",
"tower-http",
]
[[package]]
@ -1679,6 +1790,31 @@ dependencies = [
"tracing",
]
[[package]]
name = "tower-http"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5"
dependencies = [
"bitflags",
"bytes",
"futures-util",
"http",
"http-body",
"http-body-util",
"http-range-header",
"httpdate",
"mime",
"mime_guess",
"percent-encoding",
"pin-project-lite",
"tokio",
"tokio-util",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
name = "tower-layer"
version = "0.3.3"
@ -1723,6 +1859,15 @@ version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "unicase"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89"
dependencies = [
"version_check",
]
[[package]]
name = "unicode-bidi"
version = "0.3.15"

View File

@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"
[dependencies]
askama = "0.12.1"
axum = "0.7.5"
csv = "1.3.0"
failure = "0.1.8"
@ -14,3 +15,4 @@ serde = { version = "1.0.209", features = ["derive"] }
serde_json = "1.0.127"
sha1 = "0.10.6"
tokio = { version = "1.40.0", features = ["full"] }
tower-http = { version = "0.5.2", features = ["fs"] }

34
Dockerfile Normal file
View File

@ -0,0 +1,34 @@
FROM rust:1.80 AS builder
WORKDIR /code
COPY Cargo.toml Cargo.lock ./
RUN mkdir src && echo "fn main() {}" > src/main.rs
RUN cargo build --release
COPY src src
COPY templates templates
RUN cargo install --path .
FROM debian:bookworm-slim AS runtime
RUN apt-get update && \
apt-get install -y libssl-dev curl python3 python3-venv && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=builder /usr/local/cargo/bin/orario-scolastico-itet .
RUN python3 -m venv pyenv && \
bash -c "source pyenv/bin/activate"
RUN ./pyenv/bin/pip install pdfplumber
ENV PATH $PATH:/app/pyenv/bin
COPY static static
COPY pdf2csv.py .
EXPOSE 3000
CMD ["./orario-scolastico-itet"]

6
docker-compose.yaml Normal file
View File

@ -0,0 +1,6 @@
services:
web:
build: .
restart: unless-stopped
ports:
- "3000:3000"

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)
}
@ -96,7 +109,7 @@ pub struct Header {
weekdays: HashMap<String, Range<usize>>,
}
pub async fn get_class(Query(params): Query<FilterByTeacherQuery>) -> impl IntoResponse {
pub async fn get_teacher(Query(params): Query<FilterByTeacherQuery>) -> impl IntoResponse {
let filename = match download_pdf(params.id).await {
Ok(x) => x,
Err(_) => {
@ -138,7 +151,7 @@ pub struct FilterByClassQuery {
classe: String,
}
pub async fn get_teacher(Query(params): Query<FilterByClassQuery>) -> impl IntoResponse {
pub async fn get_class(Query(params): Query<FilterByClassQuery>) -> impl IntoResponse {
let filename = match download_pdf(params.id).await {
Ok(x) => x,
Err(_) => {
@ -202,6 +215,8 @@ pub fn get_header(record: &StringRecord) -> Header {
.position(|x| !x.is_empty())
.unwrap_or(record.len());
let field = field.replace("ì", "i").replace(" ", "").to_uppercase();
if field == "DOCENTE" {
docente = i as u8;
} else if field == "LUNEDI" {
@ -250,12 +265,7 @@ pub async fn get_teachers(Query(params): Query<GetTeachersQuery>) -> impl IntoRe
for record in rdr.records() {
let record = match record {
Ok(x) => x,
Err(_) => {
return (
StatusCode::UNPROCESSABLE_ENTITY,
Json(json!({ "errore": "Formato non supportato"})),
)
}
Err(_) => continue,
};
if let Some(teacher) = record.get(header.teacher as usize) {
@ -304,7 +314,10 @@ pub async fn get_classes(Query(params): Query<GetClassesQuery>) -> impl IntoResp
let mut classes: Vec<HashMap<char, HashSet<String>>> = vec![HashMap::new(); 5];
for record in rdr.records() {
let record = record.unwrap();
let record = match record {
Ok(x) => x,
Err(_) => continue,
};
for (i, cell) in record.iter().enumerate() {
for (_, range) in header.weekdays.clone() {
if range.contains(&i) && is_valid_class(cell) {

23
src/frontend/mod.rs Normal file
View File

@ -0,0 +1,23 @@
use askama::Template;
use axum::{
http::StatusCode,
response::{Html, IntoResponse},
routing::get,
};
use tower_http::services::ServeDir;
pub fn get_routes() -> axum::Router {
axum::Router::new()
.route("/", get(index))
.nest_service("/static", ServeDir::new("static"))
}
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {}
async fn index() -> impl IntoResponse {
let template = IndexTemplate {};
let html = template.render().unwrap();
(StatusCode::OK, Html(html).into_response())
}

View File

@ -1,10 +1,13 @@
use std::net::SocketAddr;
mod api;
mod frontend;
#[tokio::main]
async fn main() {
let app = axum::Router::new().merge(api::get_routes());
let app = axum::Router::new()
.merge(api::get_routes())
.merge(frontend::get_routes());
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
let listener = tokio::net::TcpListener::bind(&addr).await.unwrap();

234
static/index.js Normal file
View File

@ -0,0 +1,234 @@
function load_links() {
fetch("/api/pdf")
.then((response) => response.json())
.then((data) => {
let i = 0;
data.links.forEach((el) => {
const select = document.getElementById("links");
const option = document.createElement("option");
option.value = i;
option.text = el.split("/").pop().split(".pdf")[0];
select.appendChild(option);
i++;
});
document.getElementById("links-label").removeAttribute("aria-busy");
})
.catch((error) => {
alert("Errore nel caricamento dei dati, riprova più tardi.");
console.error("Error:", error);
});
}
function load_teachers(id) {
fetch(`/api/professori?id=${id}`)
.then((response) => response.json())
.then((data) => {
sessionStorage.setItem("teachers", JSON.stringify(data));
let i = 0;
const list = document.getElementById("teachers");
list.innerHTML = "";
data.forEach((el) => {
const option = document.createElement("option");
option.value = el;
list.appendChild(option);
i++;
});
document.getElementById("teacher-label").removeAttribute("aria-busy");
})
.catch((error) => {
alert("Errore nel caricamento dei dati, riprova più tardi.");
console.error("Error:", error);
});
}
function load_classes(id) {
fetch(`/api/classi?id=${id}`)
.then((response) => response.json())
.then((data) => {
const select = document.getElementById("year");
for (let i = 0; i < select.children.length; i++) {
if (!select.children[i].hasAttribute("disabled")) {
select.removeChild(select.children[i]);
i--;
}
}
for (let i = 0; i < data.length; i++) {
const option = document.createElement("option");
option.value = i;
option.text = i + 1;
select.appendChild(option);
}
sessionStorage.setItem("classes", JSON.stringify(data));
document.getElementById("class-label").removeAttribute("aria-busy");
})
.catch((error) => {
alert("Errore nel caricamento dei dati, riprova più tardi.");
console.error("Error:", error);
});
}
function make_teacher_table(id, teacher) {
fetch(`/api/professore?id=${id}&professore=${teacher}`)
.then((response) => response.json())
.then((data) => {
let table = document.getElementById("table");
let tbody = document.createElement("tbody");
let week = [
"Lunedì",
"Martedì",
"Mercoledì",
"Giovedì",
"Venerdì",
"Sabato",
];
week.forEach((weekday) => {
let tr = document.createElement("tr");
let th = document.createElement("th");
th.scope = "row";
th.textContent = weekday;
tr.appendChild(th);
data[weekday].forEach((teacher_class) => {
let td = document.createElement("td");
td.textContent = teacher_class;
tr.appendChild(td);
});
tbody.appendChild(tr);
});
table.appendChild(tbody);
document.getElementById("table").removeAttribute("aria-busy");
})
.catch((error) => {
alert("Errore nel caricamento dei dati, riprova più tardi.");
console.error("Error:", error);
});
}
function make_student_table(id, student_class) {
fetch(`/api/classe?id=${id}&classe=${student_class}`)
.then((response) => response.json())
.then((data) => {
let table = document.getElementById("table");
let tbody = document.createElement("tbody");
let week = [
"Lunedì",
"Martedì",
"Mercoledì",
"Giovedì",
"Venerdì",
"Sabato",
];
week.forEach((weekday) => {
let tr = document.createElement("tr");
let th = document.createElement("th");
th.scope = "row";
th.textContent = weekday;
tr.appendChild(th);
data[weekday].forEach((student_class) => {
let td = document.createElement("td");
td.textContent = student_class;
tr.appendChild(td);
});
tbody.appendChild(tr);
});
table.appendChild(tbody);
document.getElementById("table").removeAttribute("aria-busy");
})
.catch((error) => {
alert("Errore nel caricamento dei dati, riprova più tardi.");
console.error("Error:", error);
});
}
function select_pdf() {
Cookies.set("id", document.getElementById("links").value);
document.getElementById("form-type").removeAttribute("hidden");
}
function select_type() {
let id = document.getElementById("links").value;
let value = document.getElementById("type").value;
if (value === "teacher") {
load_teachers(id);
document.getElementById("form-teacher").removeAttribute("hidden");
document.getElementById("form-classes").setAttribute("hidden", "");
} else if (value === "student") {
load_classes(id);
document.getElementById("form-classes").removeAttribute("hidden");
document.getElementById("form-teacher").setAttribute("hidden", "");
}
}
function load_section() {
let data = JSON.parse(sessionStorage.getItem("classes"));
let year = document.getElementById("year").value;
const select = document.getElementById("section");
for (let i = 0; i < select.children.length; i++) {
if (!select.children[i].hasAttribute("disabled")) {
select.removeChild(select.children[i]);
i--;
}
}
Object.keys(data[year]).forEach((el) => {
const option = document.createElement("option");
option.value = el;
option.text = el;
select.appendChild(option);
});
}
function load_major() {
let data = JSON.parse(sessionStorage.getItem("classes"));
let year = document.getElementById("year").value;
let section = document.getElementById("section").value;
let select = document.getElementById("major");
for (let i = 0; i < select.children.length; i++) {
if (!select.children[i].hasAttribute("disabled")) {
select.removeChild(select.children[i]);
i--;
}
}
data[year][section].forEach((el) => {
const option = document.createElement("option");
option.value = el;
option.text = el;
select.appendChild(option);
});
}
function select_teacher() {
let teacher = document.getElementById("teacher").value;
let teachers = JSON.parse(sessionStorage.getItem("teachers"));
if (!teachers.includes(teacher)) {
return false;
}
Cookies.set("type", "teacher");
Cookies.set("teacher", teacher);
}
function select_class() {
Cookies.set("type", "student");
let student_class = document.getElementById("year").value;
student_class += document.getElementById("section").value;
student_class += document.getElementById("major").value;
Cookies.set("class", student_class);
}
function delete_cookies() {
Cookies.remove("id");
Cookies.remove("type");
Cookies.remove("teacher");
Cookies.remove("class");
}
if (Cookies.get("id") && Cookies.get("type")) {
if (Cookies.get("type") === "teacher" && Cookies.get("teacher")) {
make_teacher_table(Cookies.get("id"), Cookies.get("teacher"));
document.getElementById("dashboard").removeAttribute("hidden");
} else if (Cookies.get("type") === "student" && Cookies.get("class")) {
make_student_table(Cookies.get("id"), Cookies.get("class"));
document.getElementById("dashboard").removeAttribute("hidden");
}
} else {
document.getElementById("forms").removeAttribute("hidden");
load_links();
}

81
templates/index.html Normal file
View File

@ -0,0 +1,81 @@
<!doctype html>
<html lang="it">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Orario scolastico</title>
<link
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"
rel="stylesheet"
/>
<script src="https://cdn.jsdelivr.net/npm/js-cookie@3.0.5/dist/js.cookie.min.js"></script>
<script src="/static/index.js" defer></script>
</head>
<body>
<main class="container">
<div id="forms" hidden>
<form id="form-pdf">
<fieldset>
<label id="links-label" aria-busy="true"
>Quale orario vuoi usare?</label
>
<select id="links" onchange="select_pdf()" required>
<option selected disabled value="none">
Seleziona un orario
</option>
</select>
</fieldset>
</form>
<form id="form-type" hidden>
<formfield>
<label>Sei un professore o uno studente?</label>
<select id="type" onchange="select_type()">
<option selected disabled value="none">
Seleziona una categoria
</option>
<option value="teacher">Professore</option>
<option value="student">Studente</option>
</select>
</formfield>
</form>
<form id="form-teacher" onsubmit="return select_teacher()" hidden>
<formfield>
<label id="teacher-label" aria-busy="true"
>Seleziona il tuo cognome</label
>
<input id="teacher" type="text" list="teachers" />
<datalist id="teachers"></datalist>
</formfield>
<input type="submit" value="Continua" />
</form>
<form id="form-classes" hidden>
<formfield>
<label id="class-label" aria-busy="true"
>Seleziona la tua classe</label
>
<select id="year" onchange="load_section()">
<option selected disabled value="none">Seleziona l'anno</option>
</select>
<select id="section" onchange="load_major()">
<option selected disabled value="none">
Seleziona la sezione
</option>
</select>
<select id="major" onchange="select_class()">
<option selected disabled value="none">
Seleziona l'indirizzo
</option>
</select>
<input type="submit" value="Continua" />
</formfield>
</form>
</div>
<div id="dashboard" hidden>
<table id="table" class="striped" aria-busy="true"></table>
<form>
<input type="submit" onclick="delete_cookies()" value="Reimposta" />
</form>
</div>
</main>
</body>
</html>