Add functions to read bath names

This commit is contained in:
clerie 2024-07-14 22:39:41 +02:00
parent c3c5ff4ebe
commit 49e8db169c
3 changed files with 80 additions and 0 deletions

39
Cargo.lock generated
View File

@ -17,6 +17,15 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "async-trait" name = "async-trait"
version = "0.1.81" version = "0.1.81"
@ -121,6 +130,7 @@ name = "berlinerbaeder-exporter"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"axum", "axum",
"regex",
"reqwest", "reqwest",
"serde", "serde",
"serde_json", "serde_json",
@ -692,6 +702,35 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "regex"
version = "1.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
[[package]] [[package]]
name = "reqwest" name = "reqwest"
version = "0.12.5" version = "0.12.5"

View File

@ -5,6 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
axum = "0.7.5" axum = "0.7.5"
regex = "1.10.5"
reqwest = { version = "0.12.5", features = ["json"] } reqwest = { version = "0.12.5", features = ["json"] }
serde = { version = "1.0.204", features = ["derive"] } serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.120" serde_json = "1.0.120"

View File

@ -3,6 +3,8 @@ use axum::{
Router, Router,
}; };
use regex::Regex;
use serde::{ use serde::{
Deserialize, Deserialize,
Serialize, Serialize,
@ -27,6 +29,42 @@ struct TrafficDataItem {
thresholds: Vec<TrafficDataThresholds>, thresholds: Vec<TrafficDataThresholds>,
} }
struct BaederNames {
id: String,
name: String,
}
async fn get_baeder_names() -> Result<Vec<BaederNames>, String> {
let client = reqwest::Client::new();
let baedernames_req = client.get(String::from("https://www.berlinerbaeder.de/baeder/sommerbaeder-auslastung/"))
.send().await
.map_err(|_err| String::from("Cannot fetch baeder names"))?;
if baedernames_req.status() != reqwest::StatusCode::OK {
return Err(String::from("Unexpected response from baeder names"));
}
let baedernames_body = baedernames_req.text().await
.map_err(|_err| "Cannot read body of baedernames")?;
let r = Regex::new(r#"<tr data-bath="(.+)" data-name="(.+)" data-address="#)
.map_err(|_err| String::from("Cannot parse regex"))?;
let mut baedernames: Vec<BaederNames> = vec!{};
for c in r.captures_iter(baedernames_body.as_str()) {
baedernames.push(BaederNames {
id: c.get(1).unwrap().as_str().to_string(),
name: c.get(2).unwrap().as_str().to_string(),
})
}
return Ok(baedernames);
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@ -53,6 +91,8 @@ async fn main() {
} }
} }
let baedernames = get_baeder_names().await;
let app = Router::new() let app = Router::new()
.route("/", get(route_index)) .route("/", get(route_index))
.route("/metrics", get(route_metrics)); .route("/metrics", get(route_metrics));