Init repo
This commit is contained in:
commit
f75e09f49c
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
target
|
||||
result*
|
1411
Cargo.lock
generated
Normal file
1411
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
11
Cargo.toml
Normal file
11
Cargo.toml
Normal file
@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "nurausstieg"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
axum = "0.7.5"
|
||||
reqwest = { version = "0.12.5", features = ["json"] }
|
||||
serde = { version = "1.0.204", features = ["derive"] }
|
||||
serde_json = "1.0.120"
|
||||
tokio = { version = "1.38.0", features = ["rt-multi-thread"] }
|
27
flake.lock
Normal file
27
flake.lock
Normal file
@ -0,0 +1,27 @@
|
||||
{
|
||||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1721924956,
|
||||
"narHash": "sha256-Sb1jlyRO+N8jBXEX9Pg9Z1Qb8Bw9QyOgLDNMEpmjZ2M=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "5ad6a14c6bf098e98800b091668718c336effc95",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixos-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
37
flake.nix
Normal file
37
flake.nix
Normal file
@ -0,0 +1,37 @@
|
||||
{
|
||||
inputs = {
|
||||
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||
};
|
||||
outputs = { self, nixpkgs, ... }: {
|
||||
packages.x86_64-linux = let
|
||||
pkgs = import nixpkgs {
|
||||
system = "x86_64-linux";
|
||||
};
|
||||
in {
|
||||
nurausstieg = pkgs.rustPlatform.buildRustPackage rec {
|
||||
pname = "nurausstieg";
|
||||
version = "0.1.0";
|
||||
|
||||
src = ./.;
|
||||
|
||||
nativeBuildInputs = [
|
||||
pkgs.pkg-config
|
||||
];
|
||||
|
||||
buildInputs = [
|
||||
pkgs.openssl
|
||||
];
|
||||
|
||||
cargoLock.lockFile = ./Cargo.lock;
|
||||
|
||||
};
|
||||
default = self.packages.x86_64-linux.nurausstieg;
|
||||
};
|
||||
|
||||
hydraJobs = {
|
||||
inherit (self)
|
||||
packages;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
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);
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user