Implement departures for all stations

This commit is contained in:
clerie 2024-07-27 21:10:46 +02:00
parent dab4c8f3c6
commit 22c963b037

View File

@ -63,23 +63,23 @@ async fn main() {
let stations = HashMap::from([ let stations = HashMap::from([
(String::from("berlin-hbf"), Station { (String::from("berlin-hbf"), Station {
name: String::from("Berlin Hbf"), name: String::from("Berlin Hbf"),
code: String::from(""), code: String::from("8011160"),
}), }),
(String::from("berlin-ostbahnhof"), Station { (String::from("berlin-ostbahnhof"), Station {
name: String::from("Berlin Ostbahnhof"), name: String::from("Berlin Ostbahnhof"),
code: String::from(""), code: String::from("8010255"),
}), }),
(String::from("berlin-spandau"), Station { (String::from("berlin-spandau"), Station {
name: String::from("Berlin Spandau"), name: String::from("Berlin-Spandau"),
code: String::from(""), code: String::from("8010404"),
}), }),
(String::from("berlin-suedkreuz"), Station { (String::from("berlin-suedkreuz"), Station {
name: String::from("Berlin Südkreuz"), name: String::from("Berlin Südkreuz"),
code: String::from(""), code: String::from("8011113"),
}), }),
(String::from("berlin-gesundbrunnen"), Station { (String::from("berlin-gesundbrunnen"), Station {
name: String::from("Berlin Gesundbrunnen"), name: String::from("Berlin Gesundbrunnen"),
code: String::from(""), code: String::from("8011102"),
}), }),
]); ]);
@ -114,7 +114,7 @@ async fn main() {
let app = Router::new() let app = Router::new()
.route("/", get(route_index)) .route("/", get(route_index))
.route("/station/:station", get(route_station_overview)) .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); .with_state(state);
let listener = tokio::net::TcpListener::bind(listen).await.unwrap(); let listener = tokio::net::TcpListener::bind(listen).await.unwrap();
@ -160,9 +160,31 @@ async fn route_station_overview(
return Ok(Html(out)); return Ok(Html(out));
} }
async fn route_station() -> Result<String, String> { async fn route_station_to_dest_station(
State(state): State<AppState>,
Path((station_id, dest_station_id)): Path<(String, String)>
) -> Result<String, String> {
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 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 .send().await
.map_err(|_err| String::from("Station Data cannot be fetched"))?; .map_err(|_err| String::from("Station Data cannot be fetched"))?;
@ -186,23 +208,23 @@ async fn route_station() -> Result<String, String> {
}; };
// Skip trains, that end here // Skip trains, that end here
if departure.destination.starts_with("Berlin Ostbahnhof") { if departure.destination.starts_with(&station_properties.name) {
continue; continue;
} }
let mut after_current_station = false; let mut after_current_station = false;
if departure.initialStopPlace == "8010255" { if departure.initialStopPlace == station_properties.code {
after_current_station = true; after_current_station = true;
} }
let mut stops_at_destination = false; let mut stops_at_destination = false;
for stop in &departure.route { for stop in &departure.route {
if !after_current_station { if !after_current_station {
if stop.name.starts_with("Berlin Ostbahnhof") { if stop.name.starts_with(&station_properties.name) {
after_current_station = true; after_current_station = true;
} }
} }
else { else {
if stop.name.starts_with("Berlin Hbf") { if stop.name.starts_with(&dest_station_properties.name) {
stops_at_destination = true; stops_at_destination = true;
break; break;
} }