Don't ignore the departure and arrival radius settings
[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 static int main (string[] args) {
76         loop = new MainLoop (null);
77
78         if (args.length < 3) {
79                 print ("usage: beifahrer-cli <city_from> <city_to> [date]\n");
80                 print ("or:    beifahrer-cli details <lift_url>\n");
81                 return 1;
82         }
83
84         adac = new AdacMitfahrclub ();
85
86         if (args[1] == "details") {
87                 get_details.begin (args[2]);
88
89                 loop.run ();
90                 return 0;
91         }
92
93         string city_from = args[1];
94         string city_to = args[2];
95
96         var date = Date ();
97         if (args.length > 3) {
98                 date.set_parse (args[3]);
99         } else {
100                 var now = TimeVal ();
101                 date.set_time_val (now);
102         }
103
104         get_lifts.begin (city_from, city_to, date);
105
106         loop.run ();
107         return 0;
108 }