Turn lift date and time strings into struct Time
[beifahrer] / src / adac-mitfahrclub.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 [Compact]
20 public class City {
21         public int number;
22         public string name;
23         public double latitude;
24         public double longitude;
25         public double north;
26         public double south;
27         public double east;
28         public double west;
29
30         public City (int _number, string _name) {
31                 number = _number;
32                 name = _name;
33                 latitude = 0.0;
34                 longitude = 0.0;
35         }
36
37         internal double bb_area () {
38                 return (north - south) * (east - west);
39         }
40 }
41
42 public enum LiftFlags {
43         SMOKER = 1,
44         NON_SMOKER = 2,
45         ADAC_MEMBER = 4,
46         WOMEN_ONLY = 8
47 }
48
49 public class Lift : Object {
50         public string city_from;
51         public string city_to;
52         public Time time;
53         public int places;
54         public string price;
55         public LiftFlags flags;
56
57         public string href;
58
59         public List<string> city_via;
60         public string name;
61         public string cell;
62         public string phone;
63         public string phone2;
64         public string email;
65         public string description;
66         public string modified;
67
68         public Lift () {
69                 time.hour = -1;
70         }
71 }
72
73 public class CallbackMessage : Soup.Message {
74         public SourceFunc callback;
75
76         public CallbackMessage (string url, SourceFunc? _callback) {
77                 method = "GET";
78                 callback = _callback;
79                 set_uri (new Soup.URI (url));
80         }
81 }
82
83 public class AdacMitfahrclub {
84         const string BASE_URI = "http://mitfahrclub.adac.de";
85
86         Soup.SessionAsync session;
87         Soup.Message message;
88         List<City> city_list = null;
89
90         public AdacMitfahrclub () {
91                 session = new Soup.SessionAsync ();
92         }
93
94         private void message_finished (Soup.Session session, Soup.Message message) {
95                 Idle.add (((CallbackMessage) message).callback);
96         }
97
98         private async Html.Doc* get_html_document (string url) {
99                 message = new CallbackMessage (url, get_html_document.callback);
100                 session.queue_message (message, message_finished);
101
102                 yield;
103
104                 return Html.Doc.read_memory ((char[]) message.response_body.data, (int) message.response_body.length,
105                                              url, null, Html.ParserOption.NOERROR | Html.ParserOption.NOWARNING);
106         }
107
108         private void save_city_list () {
109                 FileStream list_file = FileStream.open ("/home/user/.cache/beifahrer/city_list", "w");
110                 if (list_file == null)
111                         return;
112
113                 foreach (unowned City city in city_list) {
114                         if (city.north != 0.0 || city.south != 0.0 || city.east != 0.0 || city.west != 0.0)
115                                 list_file.printf ("%d\t%s\t%f\t%f\t%f\t%f\t%f\t%f\n", city.number, city.name, city.latitude, city.longitude, city.north, city.south, city.east, city.west);
116                         else if (city.latitude != 0.0 || city.longitude != 0.0)
117                                 list_file.printf ("%d\t%s\t%f\t%f\n", city.number, city.name, city.latitude, city.longitude);
118                         else
119                                 list_file.printf ("%d\t%s\n", city.number, city.name);
120                 }
121         }
122
123         private bool load_city_list () {
124                 FileStream list_file = FileStream.open ("/home/user/.cache/beifahrer/city_list", "r");
125                 if (list_file == null)
126                         list_file = FileStream.open ("/usr/share/beifahrer/city_list", "r");
127                 if (list_file == null)
128                         return false;
129
130                 city_list = new List<City> ();
131                 string line = list_file.read_line ();
132                 while (line != null) {
133                         var split_line = line.split ("\t");
134                         if (split_line.length < 2)
135                                 continue;
136                         int number = split_line[0].to_int ();
137                         weak string name = split_line[1];
138
139                         var city = new City (number, name);
140                         if (split_line.length >= 4) {
141                                 city.latitude = split_line[2].to_double ();
142                                 city.longitude = split_line[3].to_double ();
143                         }
144                         if (split_line.length >= 8) {
145                                 city.north = split_line[4].to_double ();
146                                 city.south = split_line[5].to_double ();
147                                 city.east = split_line[6].to_double ();
148                                 city.west = split_line[7].to_double ();
149                         }
150                         city_list.append ((owned) city);
151
152                         line = list_file.read_line ();
153                 }
154
155                 return true;
156         }
157
158         public unowned List<City>? get_city_list () {
159                 if (city_list != null)
160                         return city_list;
161
162                 if (load_city_list ())
163                         return city_list;
164
165                 return null;
166         }
167
168         public async unowned List<City>? download_city_list () {
169                 var doc = yield get_html_document (BASE_URI);
170                 if (doc == null) {
171                         print ("Error: parsing failed");
172                         print ("%s\n", (string) message.response_body.data);
173                         return null;
174                 }
175
176                 var form = search_tag_by_id (doc->children, "form", "search_national_form");
177                 if (form == null) {
178                         print ("Error: does not contain search_national_form");
179                         print ("%s\n", (string) message.response_body.data);
180                         return null;
181                 }
182
183                 var select = search_tag_by_name (form->children, "select", "city_from");
184                 if (select == null) {
185                         print ("Error: does not contain city_from");
186                         print ("%s\n", (string) message.response_body.data);
187                         return null;
188                 }
189
190                 city_list = new List<City> ();
191                 for (var n = select->children; n != null; n = n->next) {
192                         if (n->name == "option" && n->children != null && n->children->name == "text") {
193                                 int number = n->get_prop ("value").to_int ();
194                                 // Skip 0 "Alle St.dte"
195                                 if (number == 0)
196                                         continue;
197                                 var city = new City(number,
198                                                     n->children->content);
199                                 city_list.append ((owned) city);
200                         }
201                 }
202
203                 // TODO: get coordinates
204
205                 save_city_list ();
206
207                 return city_list;
208         }
209
210         private int get_city_number (string name) {
211                 foreach (unowned City city in city_list) {
212                         if (city.name == name)
213                                 return city.number;
214                 }
215                 return 0;
216         }
217
218         public unowned City find_nearest_city (double latitude, double longitude) {
219                 unowned City result = null;
220                 double min_distance = 0.0;
221                 bool in_result = false;
222
223                 foreach (unowned City city in city_list) {
224                         double lat = latitude - city.latitude;
225                         double lng = longitude - city.longitude;
226                         double distance = lat * lat + lng * lng;
227                         bool in_city = ((city.south <= latitude <= city.north) &&
228                                         (city.west <= longitude <= city.east));
229
230                         if ((result == null) ||
231                             (in_city && !in_result) ||
232                             (in_city && in_result && distance / city.bb_area () < min_distance / result.bb_area ()) ||
233                             (!in_city && !in_result && distance < min_distance)) {
234                                 result = city;
235                                 min_distance = distance;
236                                 in_result = in_city;
237                         }
238                 }
239
240                 return result;
241         }
242
243         public async List<Lift>? get_lift_list (string city_from, string city_to, Date date, int tolerance = 0) {
244                 if (city_list == null)
245                         get_city_list ();
246
247                 int num_from = get_city_number (city_from);
248                 if (num_from == 0) {
249                         stderr.printf ("Unknown city: %s\n", city_to);
250                         return null;
251                 }
252
253                 int num_to = get_city_number (city_to);
254                 if (num_to == 0) {
255                         stderr.printf ("Unknown city: %s\n", city_to);
256                         return null;
257                 }
258
259                 string url = BASE_URI + "/mitfahrclub/%s/%s/b.html".printf (
260                         city_from,
261                         city_to
262                 );
263
264                 url += "?type=b&city_from=%d&radius_from=0&city_to=%d&radius_to=0".printf (
265                         num_from,
266                         num_to
267                 );
268
269                 url += "&date=date&day=%d&month=%d&year=%d&tolerance=%d&smoking=&avg_speed=&".printf (
270                         date.get_day (),
271                         date.get_month (),
272                         date.get_year (),
273                         tolerance
274                 );
275
276                 var doc = yield get_html_document (url);
277                 if (doc == null) {
278                         print ("Error: parsing failed");
279                         print ("%s\n", (string) message.response_body.data);
280                         return null;
281                 }
282
283                 var table = search_tag_by_class (doc->children, "table", "list p_15");
284                 if (table == null) {
285                         print ("Error: does not contain list p_15 table");
286                         print ("%s\n", (string) message.response_body.data);
287                         return null;
288                 }
289
290                 var list = new List<Lift> ();
291                 for (var n = table->children; n != null; n = n->next) {
292                         if (n->name == "tr") {
293                                 var lift = parse_lift_row (n->children);
294                                 if (lift.city_from != null) // Skip the title row
295                                         list.append ((owned) lift);
296                         }
297                 }
298
299                 // Error message?
300                 var div = table->next;
301                 if (div != null && div->get_prop ("class") == "error-message") {
302                         if (div->children == null || div->children->content == null ||
303                             !div->children->content.has_prefix ("Es sind leider noch keine Einträge vorhanden.")) {
304                                 stderr.printf ("Got an unknown error message!\n");
305                                 if (div->children != null && div->children->content != null)
306                                         stderr.printf ("\"%s\"\n", div->children->content);
307                         }
308                 }
309
310                 return list;
311         }
312
313         Lift parse_lift_row (Xml.Node* node) {
314                 var lift = new Lift ();
315                 int i = 0;
316                 for (var n = node; n != null; n = n->next) {
317                         if (n->name == "td") {
318                                 var n2 = n->children;
319                                 if (n2 != null) {
320                                         if (n2->name == "a") {
321                                                 var href = n2->get_prop ("href");
322                                                 if (href != null && lift.href == null)
323                                                         lift.href = href;
324                                                 var n3 = n2->children;
325                                                 while (n3 != null) {
326                                                         if (n3->name == "text")
327                                                                 switch (i) {
328                                                                 case 0:
329                                                                         lift.city_from = n3->content;
330                                                                         break;
331                                                                 case 1:
332                                                                         lift.city_to = n3->content;
333                                                                         break;
334                                                                 case 2:
335                                                                         parse_date (n3->content, out lift.time);
336                                                                         break;
337                                                                 case 3:
338                                                                         parse_time (n3->content.strip (), out lift.time);
339                                                                         break;
340                                                                 case 4:
341                                                                         lift.places = n3->content.to_int ();
342                                                                         break;
343                                                                 case 5:
344                                                                         lift.price = n3->content;
345                                                                         break;
346                                                                 default:
347                                                                         print ("TEXT:%s\n", n3->content);
348                                                                         break;
349                                                                 }
350                                                         if (n3->name == "span") {
351                                                                 string class = n3->get_prop ("class");
352                                                                 if (class == "icon_smoker")
353                                                                         lift.flags |= LiftFlags.SMOKER;
354                                                                 else if (class == "icon_non_smoker")
355                                                                         lift.flags |= LiftFlags.NON_SMOKER;
356                                                                 else if (class == "icon_adac")
357                                                                         lift.flags |= LiftFlags.ADAC_MEMBER;
358                                                                 else if (class == "icon_women")
359                                                                         lift.flags |= LiftFlags.WOMEN_ONLY;
360                                                                 else if (class != null)
361                                                                         print ("SPAN %s\n", class);
362                                                         }
363                                                         n3 = n3->next;
364                                                 }
365                                         }
366                                 }
367                                 i++;
368                         }
369                 }
370
371                 return lift;
372         }
373
374         public async bool update_lift_details (Lift lift) {
375                 string url = BASE_URI + lift.href;
376
377                 var doc = yield get_html_document (url);
378                 if (doc == null) {
379                         print ("Error: parsing failed");
380                         print ("%s\n", (string) message.response_body.data);
381                         return false;
382                 }
383
384                 var table = search_tag_by_class (doc->children, "table", "lift");
385                 if (table == null) {
386                         print ("Error: does not contain lift table");
387                         print ("%s\n", (string) message.response_body.data);
388                         return false;
389                 }
390
391                 Xml.Node* n;
392                 for (n = table->children; n != null; n = n->next) {
393                         if (n->name == "tr") {
394                                 var n2 = n->children;
395                                 if (n2 == null || n2->name != "td" ||
396                                     n2->children == null || n2->children->name != "text")
397                                         continue;
398
399                                 string text = n2->children->content;
400
401                                 if (text != "Strecke & Infos" && text != "&nbsp;" && !text.has_prefix ("\xc2\xa0") &&
402                                     text != "Datum" &&
403                                     text != "Freie Pl\xc3\xa4tze" &&
404                                     text != "Name" &&
405                                     text != "Handy" &&
406                                     text != "Telefon" &&
407                                     text != "Telefon 2" &&
408                                     text != "E-Mail 1" &&
409                                     text != "Details" &&
410                                     text != "Beschreibung")
411                                         continue;
412
413                                 n2 = n2->next;
414                                 if (n2 == null)
415                                         continue;
416
417                                 // Skip text between td nodes
418                                 if (n2->name == "text")
419                                         n2 = n2->next;
420
421                                 if (n2 == null || n2->name != "td" || n2->children == null)
422                                         continue;
423
424                                 if (n2->children->name == "img") {
425                                         // FIXME: email image
426                                         // n2->children->get_prop ("src"))
427                                         continue;
428                                 }
429
430                                 if (n2->children->name == "div" && text == "Beschreibung") {
431                                         var n3 = n2->children->children;
432                                         lift.description = "";
433                                         while (n3 != null) {
434                                                 if (n3->name == "text")
435                                                         lift.description += n3->content.strip () + "\n";
436                                                 n3 = n3->next;
437                                         }
438                                         continue;
439                                 } else if (n2->children->name != "text") {
440                                         continue;
441                                 }
442
443                                 var text1 = n2->children->content.strip ();
444
445                                 if (text == "Freie Pl\xc3\xa4tze")
446                                         lift.places = text1.to_int ();
447                                 else if (text == "Name")
448                                         lift.name = text1;
449                                 else if (text == "Handy")
450                                         lift.cell = text1;
451                                 else if (text == "Telefon")
452                                         lift.phone = text1;
453                                 else if (text == "Telefon 2")
454                                         lift.phone2 = text1;
455                                 else if (text == "E-Mail 1")
456                                         lift.email = text1;
457                                 else if (text != "Strecke & Infos" && text != "&nbsp;" &&
458                                     !text.has_prefix ("\xc2\xa0") && text != "Datum" &&
459                                     text != "Details")
460                                         continue;
461
462                                 n2 = n2->next;
463                                 if (n2 == null)
464                                         continue;
465
466                                 // Skip text between td nodes
467                                 if (n2->name == "text")
468                                         n2 = n2->next;
469
470                                 if (n2 == null || n2->name != "td" ||
471                                     n2->children == null)
472                                         continue;
473
474                                 if (n2->children->name == "span" &&
475                                     n2->children->get_prop ("class") == "icon_non_smoker") {
476                                         lift.flags |= LiftFlags.NON_SMOKER;
477                                         continue;
478                                 } else if (n2->children->name == "span" &&
479                                     n2->children->get_prop ("class") == "icon_smoker") {
480                                         lift.flags |= LiftFlags.SMOKER;
481                                         continue;
482                                 } else if (n2->children->name != "text")
483                                         continue;
484
485                                 var text2 = n2->children->content.strip ();
486
487                                 if (text1 == "von")
488                                         lift.city_from = text2;
489                                 else if (text1.has_prefix ("\xc3\xbc"))
490                                         lift.city_via.append (text2);
491                                 else if (text1 == "nach")
492                                         lift.city_to = text2;
493                                 else if (text1 == "Datum")
494                                         parse_date (text2, out lift.time);
495                                 else if (text1 == "Uhrzeit")
496                                         parse_time (text2, out lift.time);
497                                 else if (text1 == "Raucher")
498                                         print ("Raucher: %s\n", text2);
499                                 else if (text1 == "Fahrpreis")
500                                         lift.price = text2;
501                                 else if (text1 == "ADAC-Mitglied" && text2 != "nein")
502                                         lift.flags |= LiftFlags.ADAC_MEMBER;
503                         }
504                 }
505
506                 // The paragraph after the table contains the date of last modification
507                 var p = table->next;
508                 for (n = p->children; n != null; n = n->next) {
509                         if (n->name != "text")
510                                 continue;
511
512                         var s = n->content.strip ();
513                         if (s.has_prefix ("Letztmalig aktualisiert am "))
514                                 lift.modified = s.offset (27).dup (); // "Do 15.04.2010 20:32"
515                 }
516
517                 return true;
518         }
519
520         Xml.Node* search_tag_by_property (Xml.Node* node, string tag, string prop, string val) requires (node != null) {
521                 for (var n = node; n != null; n = n->next) {
522                         if (n->name == tag && n->get_prop (prop) == val)
523                                 return n;
524                         if (n->children != null) {
525                                 var found = search_tag_by_property (n->children, tag, prop, val);
526                                 if (found != null)
527                                         return found;
528                         }
529                 }
530                 return null;
531         }
532
533         Xml.Node* search_tag_by_id (Xml.Node* node, string tag, string id) requires (node != null) {
534                 return search_tag_by_property (node, tag, "id", id);
535         }
536
537         Xml.Node* search_tag_by_name (Xml.Node* node, string tag, string name) requires (node != null) {
538                 return search_tag_by_property (node, tag, "name", name);
539         }
540
541         Xml.Node* search_tag_by_class (Xml.Node* node, string tag, string @class) requires (node != null) {
542                 return search_tag_by_property (node, tag, "class", @class);
543         }
544
545         void parse_date (string date, out Time time) {
546                 int year;
547                 if (date.length == 11)
548                         date = date.offset (3);
549                 if (date.length != 8)
550                         return;
551                 var res = date.scanf ("%02d.%02d.%02d", out time.day, out time.month, out year);
552                 time.year = year + 2000;
553         }
554
555         void parse_time (string time, out Time result) {
556                 var res = time.scanf ("%d.%02d Uhr", out result.hour, out result.minute);
557         }
558 }