summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorstuebinm2022-01-25 23:07:23 +0100
committerstuebinm2022-01-25 23:07:23 +0100
commit130d8907335c93e48017c13e3202816d552683c9 (patch)
treed94809523a3e47d3eadf94d19cd26b22cdb3a0ca /src/main.rs
hacky proof of concept
Diffstat (limited to '')
-rw-r--r--src/main.rs145
1 files changed, 145 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 0000000..12c67a8
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,145 @@
+use clap::{Parser, Subcommand};
+
+use colored::*;
+
+use traveltext::types::*;
+use traveltext::{travelynx::*, iceportal::*};
+
+
+#[allow(non_upper_case_globals)]
+const token: &str = "1387-d942ee22-1d34-4dc2-89b6-5e7ef229fb5e";
+#[allow(non_upper_case_globals)]
+const baseurl: &str = "https://travelynx.de";
+
+
+
+#[derive(Parser)]
+struct Cli {
+ #[clap(subcommand)]
+ command: Command,
+}
+
+#[derive(Subcommand)]
+enum Command {
+ /// Get current travelynx status
+ Status,
+ /// Check in to a train using travelynx
+ Checkin {
+ from: String,
+ to: String,
+ // TODO: make this optional and guess which train if not given
+ #[clap(flatten)]
+ train: TrainRef
+ },
+ /// If iceportal.de is available, ask it which train we're in and
+ /// check in
+ Autocheckin,
+ /// Undo the last checkin (if any).
+ Undo,
+ /// Query iceportal.de (for testing)
+ ICEPortal
+}
+
+
+
+fn main() -> Result<(), ureq::Error> {
+ let cli = Cli::parse();
+
+ let traveltext = format!(
+ "{}{}el{}{}",
+ "tr".cyan(),
+ "av".bright_magenta(),
+ "te".bright_magenta(),
+ "xt".cyan()
+ );
+
+ match cli.command {
+ Command::Status => {
+ let body: Status = ureq::get(&format!("{}/api/v1/status/{}", baseurl, token))
+ .call()
+ // TODO: this prints the token!
+ .unwrap_or_else(|err| exit_err(&err.to_string()))
+ .into_json()
+ .unwrap_or_else(|err| exit_err(&err.to_string()));
+
+ println!("{}: {}", traveltext, body);
+ }
+ Command::Checkin {from, to, train} => {
+ let request = Action::CheckIn {
+ train,
+ from_station: from,
+ to_station: Some(to),
+ comment: None,
+ token: format!("{}", token)
+ };
+
+ // println!("{}", serde_json::to_string(&request).unwrap());
+
+ let resp: Response = ureq::post(&format!("{}/api/v1/travel", baseurl))
+ .send_json(request)
+ .unwrap_or_else(|err| exit_err(&err.to_string()))
+ .into_json()
+ .unwrap_or_else(|err| exit_err(&err.to_string()));
+
+ // eprintln!("{:?}", resp);
+ println!("{}: {}", traveltext, resp);
+ },
+ Command::Undo => {
+ let resp: Response = ureq::post(&format!("{}/api/v1/travel", baseurl))
+ .send_json(Action::Undo {token: token.to_owned()})
+ .unwrap_or_else(|err| exit_err(&err.to_string()))
+ .into_json()
+ .unwrap_or_else(|err| exit_err(&err.to_string()));
+
+ println!("{}: {}", traveltext, resp);
+ },
+ Command::Autocheckin => {
+ let iceportal: TripInfo = ureq::get("https://iceportal.de/api1/rs/tripInfo/trip")
+ .call()?
+ .into_json()
+ .unwrap_or_else(|err| exit_err(&err.to_string()));
+ let last_stop = iceportal.guess_last_station().unwrap();
+ let train = iceportal.get_train_ref();
+ println!(
+ "{}: guessing you got onto {} {} in {}, checking in …",
+ traveltext,
+ train._type,
+ train.no,
+ last_stop
+ );
+ let request = Action::CheckIn {
+ train,
+ from_station: last_stop,
+ to_station: None,
+ comment: None,
+ token: format!("{}", token)
+ };
+
+ // println!("{}", serde_json::to_string(&request).unwrap());
+
+ let resp: Response = ureq::post(&format!("{}/api/v1/travel", baseurl))
+ .send_json(request)
+ .unwrap_or_else(|err| exit_err(&err.to_string()))
+ .into_json()
+ .unwrap_or_else(|err| exit_err(&err.to_string()));
+
+ // eprintln!("{:?}", resp);
+ println!("{}: {}", traveltext, resp);
+
+ },
+ Command::ICEPortal => {
+ let resp: TripInfo = ureq::get("https://iceportal.de/api1/rs/tripInfo/trip")
+ .call()?
+ .into_json()
+ .unwrap_or_else(|err| exit_err(&err.to_string()));
+ println!("{:?}", resp);
+ println!("guessing last stop was: {:?}", resp.guess_last_station());
+ }
+ }
+ Ok(())
+}
+
+fn exit_err(msg: &str) -> ! {
+ eprintln!("{}", msg);
+ std::process::exit(1)
+}