summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/main.rs89
1 files changed, 38 insertions, 51 deletions
diff --git a/src/main.rs b/src/main.rs
index 7e1c85a..9bb2f9e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,8 +2,9 @@ use clap::{Parser, Subcommand};
use colored::*;
use serde::Deserialize;
-use traveltext::types::*;
-use traveltext::{iceportal::*, travelynx::*};
+use traveltext::onboard::{choose_api, OnBoardAPI};
+use traveltext::{traits::*, travelynx::*, types::*};
+
#[derive(Parser)]
struct Cli {
@@ -34,15 +35,21 @@ enum Command {
Destination {
to: String
},
+ Station {
+ station: String
+ },
Arewethereyet,
/// If iceportal.de is available, ask it which train we're in and
/// check in
- Autocheckin,
+ Autocheckin {
+ train: String
+ },
/// Undo the last checkin (if any).
Undo,
- /// Query iceportal.de (for testing)
- ICEPortal,
- Zugportal
+ /// Query a train's on-board API
+ Query {
+ train: String
+ }
}
#[derive(Deserialize)]
@@ -119,11 +126,9 @@ fn main() -> Result<(), ureq::Error> {
None => println!("{}: I have no idea", traveltext)
}
}
- Some(_to) => println!(
- "{}: {}",
- traveltext,
- "you're not checked in".red()
- )
+ Some(_to) => {
+ println!("{}: {}", traveltext, "you're not checked in".red())
+ }
}
}
Command::Checkin { from, to, train } => {
@@ -165,23 +170,25 @@ fn main() -> Result<(), ureq::Error> {
println!("{}: {}", traveltext, resp);
}
- Command::Autocheckin => {
- let iceportal: TripInfo = exiting_get_request(
- "https://iceportal.de/api1/rs/tripInfo/trip",
- cli.debug
- );
-
- let last_stop = iceportal.guess_last_station().unwrap();
- let train = iceportal.get_train_ref();
+ Command::Autocheckin { train } => {
+ let onboard = match choose_api(&train).request(cli.debug) {
+ Ok(resp) => resp,
+ Err(e) => exit_err(&e.to_string(), "failed to parse api response")
+ };
+ let last_stop = onboard.guess_last_station().unwrap();
+ let train = onboard.get_train_ref();
println!(
"{}: guessing you got onto {} {} in {}, checking in …",
- traveltext, train._type, train.no, last_stop
+ traveltext,
+ train._type,
+ train.no,
+ last_stop.to_fancy_string()
);
let resp: Response = exiting_post_request(
&format!("{}/api/v1/travel", cli.baseurl),
Action::CheckIn {
train,
- from_station: last_stop,
+ from_station: last_stop.name().to_owned(),
to_station: None,
comment: None,
token: format!("{}", config.token_travel)
@@ -192,51 +199,31 @@ fn main() -> Result<(), ureq::Error> {
// eprintln!("{:?}", resp);
println!("{}: {}", traveltext, resp);
}
- Command::ICEPortal => {
- match get_request::<TripInfo>(
- "https://iceportal.de/api1/rs/tripInfo/trip"
- ) {
+ Command::Query { train } => {
+ let api: &dyn OnBoardAPI = choose_api(&train);
+ match api.request(cli.debug) {
Ok(resp) => {
println!(
"{}: Currently in {}\n",
traveltext,
resp.get_train_ref().to_string().green()
);
- println!("guessing last stop was: {:?}\n", resp.guess_last_station());
- println!("Stops:\n{}", resp.trip())
- }
- Err(err) => {
- if cli.debug {
- eprintln!("{:?}", err);
- }
- println!("either this tool or the iceportal broke or you're not actually on an ICE\n\
- (get a response but couldn't parse it)");
- }
- }
- }
- Command::Zugportal => {
- match get_request::<traveltext::zugportal::Journey>(
- // "https://iceportal.de/api1/rs/tripInfo/trip"
- "https://zugportal.de/prd/zupo-travel-information/api/public/ri/journey"
- ) {
- Ok(resp) => {
println!(
- "{}: Currently in {}\n",
- traveltext,
- resp.get_train_ref().to_string().green()
+ "guessing last stop was: {:?}\n",
+ resp.guess_last_station().unwrap().name()
);
- // println!("guessing last stop was: {:?}\n", resp.guess_last_station());
- println!("Stops:\n{}", resp.trip())
+ // println!("Stops:\n{}", resp.stops())
}
Err(err) => {
- if cli.debug {
- eprintln!("{:?}", err);
- }
println!("either this tool or the zugportal broke or you're not actually on an ICE\n\
(get a response but couldn't parse it)");
}
}
}
+ Command::Station { station } => {
+ // let c = HafasClient::new(DbProfile, HyperRustlsRequester::new());
+ // println!("{:#?}", c.suggestions("München Hbf", None).await.unwrap());
+ }
}
Ok(())
}