Switch from libsoup to libcurl, add login and my information support
[beifahrer] / src / beifahrer-cli.vala
1 /* This file is part of Beifahrer.
2  *
3  * Copyright (C) 2010 Philipp Zabel
4  *
5  * Beifahrer is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Beifahrer is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Beifahrer. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 AdacMitfahrclub adac;
20 MainLoop loop;
21
22 async void get_lifts (string city_from, string city_to, Date date) {
23         print ("Lifts from %s to %s on %d.%d.%d\n", city_from, city_to,
24                date.get_day (), date.get_month (), date.get_year ());
25
26         var lift_list = yield adac.get_lift_list (city_from, 0, city_to, 0, date);
27         foreach (Lift lift in lift_list) {
28                 string datetime = "%02d.%02d.%02d".printf (lift.time.day, lift.time.month, lift.time.year);
29                 if (lift.time.hour >= 0)
30                         datetime += ", %d:%02d".printf (lift.time.hour, lift.time.minute);
31                 print ("%s\t%s\t%s\t%d\t%s\t", lift.city_from, lift.city_to, datetime, lift.places, lift.price);
32                 if (LiftFlags.SMOKER in lift.flags)
33                         print ("smoker ");
34                 else if (LiftFlags.NON_SMOKER in lift.flags)
35                         print ("non_smoker ");
36                 if (LiftFlags.ADAC_MEMBER in lift.flags)
37                         print ("adac ");
38                 if  (LiftFlags.WOMEN_ONLY in lift.flags)
39                         print ("women ");
40                 print ("\n");
41                 print ("%s\n", lift.href);
42         }
43
44         loop.quit ();
45 }
46
47 async void get_details (string href) {
48         var lift = new Lift ();
49         lift.href = href;
50         yield adac.update_lift_details (lift);
51
52         string datetime = "%02d.%02d.%02d".printf (lift.time.day, lift.time.month, lift.time.year);
53         if (lift.time.hour >= 0)
54                 datetime += ", %d:%02d".printf (lift.time.hour, lift.time.minute);
55         print ("%s\t%s\t%s\t%d\t%s\t", lift.city_from, lift.city_to, datetime, lift.places, lift.price);
56         if (LiftFlags.SMOKER in lift.flags)
57                 print ("smoker ");
58         else if (LiftFlags.NON_SMOKER in lift.flags)
59                 print ("non_smoker ");
60         if (LiftFlags.ADAC_MEMBER in lift.flags)
61                 print ("adac ");
62         if  (LiftFlags.WOMEN_ONLY in lift.flags)
63                 print ("women ");
64                print ("\n");
65         foreach (string via in lift.city_via) {
66                 print ("\tvia %s\n", via);
67         }
68
69         print ("Driver: %s (%s)\n", lift.name, lift.phone);
70         print ("Description:\n%s\n", lift.description);
71
72         loop.quit ();
73 }
74
75 void usage () {
76         print ("usage: beifahrer-cli <city_from> <city_to> [date]\n");
77         print ("or:    beifahrer-cli details <lift_url>\n");
78         print ("or:    beifahrer-cli login <username> <password>\n");
79         print ("or:    beifahrer-cli login <cookie>\n");
80 }
81
82 static int main (string[] args) {
83         loop = new MainLoop (null);
84
85         if (args.length < 3) {
86                 usage ();
87                 return 1;
88         }
89
90         Curl.global_init (Curl.GLOBAL_DEFAULT);
91
92         adac = new AdacMitfahrclub ();
93
94         if (args[1] == "login") {
95                 if (args.length > 3) {
96                         adac.login (args[2], args[3]);
97                 } else {
98                         adac.set_cookie (args[2]);
99                         adac.login (null, null);
100                 }
101                 loop.run ();
102                 return 0;
103         }
104
105         if (args[1] == "details") {
106                 get_details.begin (args[2]);
107
108                 loop.run ();
109                 return 0;
110         }
111
112         string city_from = args[1];
113         string city_to = args[2];
114
115         var date = Date ();
116         if (args.length > 3) {
117                 date.set_parse (args[3]);
118         } else {
119                 var now = TimeVal ();
120                 date.set_time_val (now);
121         }
122
123         get_lifts.begin (city_from, city_to, date);
124
125         loop.run ();
126
127         Curl.global_cleanup ();
128
129         return 0;
130 }