summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 12c67a87dbda23afd6b15f83840bdb6424099ed5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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)
}