From 22c963b0374bf756a664b9a54bc1e12c989af4f2 Mon Sep 17 00:00:00 2001 From: clerie Date: Sat, 27 Jul 2024 21:10:46 +0200 Subject: [PATCH] Implement departures for all stations --- src/main.rs | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index aae6616..b771b44 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,23 +63,23 @@ async fn main() { let stations = HashMap::from([ (String::from("berlin-hbf"), Station { name: String::from("Berlin Hbf"), - code: String::from(""), + code: String::from("8011160"), }), (String::from("berlin-ostbahnhof"), Station { name: String::from("Berlin Ostbahnhof"), - code: String::from(""), + code: String::from("8010255"), }), (String::from("berlin-spandau"), Station { - name: String::from("Berlin Spandau"), - code: String::from(""), + name: String::from("Berlin-Spandau"), + code: String::from("8010404"), }), (String::from("berlin-suedkreuz"), Station { name: String::from("Berlin Südkreuz"), - code: String::from(""), + code: String::from("8011113"), }), (String::from("berlin-gesundbrunnen"), Station { name: String::from("Berlin Gesundbrunnen"), - code: String::from(""), + code: String::from("8011102"), }), ]); @@ -114,7 +114,7 @@ async fn main() { let app = Router::new() .route("/", get(route_index)) .route("/station/:station", get(route_station_overview)) - .route("/station/berlin-ostbahnhof/to/berlin-hbf", get(route_station)) + .route("/station/:station_id/to/:dest_station_id", get(route_station_to_dest_station)) .with_state(state); let listener = tokio::net::TcpListener::bind(listen).await.unwrap(); @@ -160,9 +160,31 @@ async fn route_station_overview( return Ok(Html(out)); } -async fn route_station() -> Result { +async fn route_station_to_dest_station( + State(state): State, + Path((station_id, dest_station_id)): Path<(String, String)> +) -> Result { + if !state.stations.contains_key(&station_id) { + return Err(String::from("")); + } + + if !state.stations.contains_key(&dest_station_id) { + return Err(String::from("")); + } + + let station_properties = match state.stations.get(&station_id) { + Some(v) => v, + None => return Err(String::from("")), + }; + + let dest_station_properties = match state.stations.get(&dest_station_id) { + Some(v) => v, + None => return Err(String::from("")), + }; + + let client = reqwest::Client::new(); - let stationdata_req = client.get(String::from("https://bahn.expert/api/iris/v2/abfahrten/8010255?lookahead=150&lookbehind=10")) + let stationdata_req = client.get(format!("https://bahn.expert/api/iris/v2/abfahrten/{}?lookahead=150&lookbehind=10", station_properties.code)) .send().await .map_err(|_err| String::from("Station Data cannot be fetched"))?; @@ -186,23 +208,23 @@ async fn route_station() -> Result { }; // Skip trains, that end here - if departure.destination.starts_with("Berlin Ostbahnhof") { + if departure.destination.starts_with(&station_properties.name) { continue; } let mut after_current_station = false; - if departure.initialStopPlace == "8010255" { + if departure.initialStopPlace == station_properties.code { 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") { + if stop.name.starts_with(&station_properties.name) { after_current_station = true; } } else { - if stop.name.starts_with("Berlin Hbf") { + if stop.name.starts_with(&dest_station_properties.name) { stops_at_destination = true; break; }