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) }