Init repo
This commit is contained in:
133
src/main.rs
Normal file
133
src/main.rs
Normal file
@@ -0,0 +1,133 @@
|
||||
use axum::{
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Train {
|
||||
name: String,
|
||||
number: String,
|
||||
//line: String,
|
||||
r#type: String,
|
||||
admin: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Stop {
|
||||
name: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct DepartureInfo {
|
||||
time: String,
|
||||
platform: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Departure {
|
||||
departure: Option<DepartureInfo>,
|
||||
route: Vec<Stop>,
|
||||
train: Train,
|
||||
destination: String,
|
||||
initialStopPlace: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Departures {
|
||||
departures: Vec<Departure>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
||||
let mut args = std::env::args();
|
||||
let _name = args.next().unwrap();
|
||||
|
||||
let mut listen = String::from("[::]:3000");
|
||||
|
||||
loop {
|
||||
let arg = if let Some(arg) = args.next() {
|
||||
arg
|
||||
} else {
|
||||
break;
|
||||
};
|
||||
|
||||
match arg.as_str() {
|
||||
"--listen" => {
|
||||
listen = args.next().unwrap();
|
||||
}
|
||||
unknown => {
|
||||
println!("unknown option: {}", unknown);
|
||||
std::process::exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let app = Router::new()
|
||||
.route("/station/berlin-ostbahnhof/to/berlin-hbf", get(route_station));
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(listen).await.unwrap();
|
||||
println!("Server listening on: http://{}", listener.local_addr().unwrap());
|
||||
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
async fn route_station() -> Result<String, String> {
|
||||
let client = reqwest::Client::new();
|
||||
let stationdata_req = client.get(String::from("https://bahn.expert/api/iris/v2/abfahrten/8010255?lookahead=150&lookbehind=10"))
|
||||
.send().await
|
||||
.map_err(|_err| String::from("Station Data cannot be fetched"))?;
|
||||
|
||||
if stationdata_req.status() != reqwest::StatusCode::OK {
|
||||
return Err(String::from("Station Data returned an unexpected response"));
|
||||
}
|
||||
|
||||
let stationdata_body = stationdata_req.text().await
|
||||
.map_err(|_err| "Station Data cannot be read")?;
|
||||
|
||||
let stationdata: Departures = serde_json::from_str(stationdata_body.as_str())
|
||||
.map_err(|err| format!("{}", err))?;
|
||||
|
||||
let mut out = String::new();
|
||||
|
||||
|
||||
for departure in stationdata.departures {
|
||||
let departure_info = match departure.departure {
|
||||
Some(d) => d,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
// Skip trains, that end here
|
||||
if departure.destination.starts_with("Berlin Ostbahnhof") {
|
||||
continue;
|
||||
}
|
||||
let mut after_current_station = false;
|
||||
if departure.initialStopPlace == "8010255" {
|
||||
after_current_station = true;
|
||||
}
|
||||
|
||||
let mut stops_at_destination = false;
|
||||
for stop in &departure.route {
|
||||
if !after_current_station {
|
||||
if stop.name.starts_with("Berlin Ostbahnhof") {
|
||||
after_current_station = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if stop.name.starts_with("Berlin Hbf") {
|
||||
stops_at_destination = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if stops_at_destination {
|
||||
out.push_str(&format!("{}: {} ({} {}) [{}]-> {}\n", departure_info.time, &departure.train.name, &departure.train.r#type, &departure.train.number, departure_info.platform, &departure.destination));
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(out);
|
||||
|
||||
}
|
Reference in New Issue
Block a user