1 /* This file is part of Beifahrer.
3 * Copyright (C) 2010 Philipp Zabel
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.
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.
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/>.
23 public double latitude;
24 public double longitude;
30 public City (int _number, string _name) {
37 internal double bb_area () {
38 return (north - south) * (east - west);
42 public enum LiftFlags {
49 public class Lift : Object {
50 public string city_from;
51 public string city_to;
55 public LiftFlags flags;
59 public List<string> city_via;
65 public string description;
66 public string modified;
73 public class CallbackMessage : Soup.Message {
74 public SourceFunc callback;
76 public CallbackMessage (string url, SourceFunc? _callback) {
79 set_uri (new Soup.URI (url));
83 public class AdacMitfahrclub {
84 const string BASE_URI = "http://mitfahrclub.adac.de";
86 Soup.SessionAsync session;
88 List<City> city_list = null;
90 public AdacMitfahrclub () {
91 session = new Soup.SessionAsync ();
94 private void message_finished (Soup.Session session, Soup.Message message) {
95 Idle.add (((CallbackMessage) message).callback);
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);
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);
108 private void save_city_list () {
109 FileStream list_file = FileStream.open ("/home/user/.cache/beifahrer/city_list", "w");
110 if (list_file == null)
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);
119 list_file.printf ("%d\t%s\n", city.number, city.name);
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)
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)
136 int number = split_line[0].to_int ();
137 weak string name = split_line[1];
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 ();
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 ();
150 city_list.append ((owned) city);
152 line = list_file.read_line ();
158 public unowned List<City>? get_city_list () {
159 if (city_list != null)
162 if (load_city_list ())
168 public async unowned List<City>? download_city_list () {
169 var doc = yield get_html_document (BASE_URI);
171 print ("Error: parsing failed");
172 print ("%s\n", (string) message.response_body.data);
176 var form = search_tag_by_id (doc->children, "form", "search_national_form");
178 print ("Error: does not contain search_national_form");
179 print ("%s\n", (string) message.response_body.data);
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);
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"
197 var city = new City(number,
198 n->children->content);
199 city_list.append ((owned) city);
203 // TODO: get coordinates
210 private int get_city_number (string name) {
211 foreach (unowned City city in city_list) {
212 if (city.name == name)
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;
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));
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)) {
235 min_distance = distance;
243 public async List<Lift>? get_lift_list (string city_from, string city_to, Date date, int tolerance = 0) {
244 if (city_list == null)
247 int num_from = get_city_number (city_from);
249 stderr.printf ("Unknown city: %s\n", city_to);
253 int num_to = get_city_number (city_to);
255 stderr.printf ("Unknown city: %s\n", city_to);
259 string url = BASE_URI + "/mitfahrclub/%s/%s/b.html".printf (
264 url += "?type=b&city_from=%d&radius_from=0&city_to=%d&radius_to=0".printf (
269 url += "&date=date&day=%d&month=%d&year=%d&tolerance=%d&smoking=&avg_speed=&".printf (
276 var doc = yield get_html_document (url);
278 print ("Error: parsing failed");
279 print ("%s\n", (string) message.response_body.data);
283 var table = search_tag_by_class (doc->children, "table", "list p_15");
285 print ("Error: does not contain list p_15 table");
286 print ("%s\n", (string) message.response_body.data);
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);
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);
313 Lift parse_lift_row (Xml.Node* node) {
314 var lift = new Lift ();
316 for (var n = node; n != null; n = n->next) {
317 if (n->name == "td") {
318 var n2 = n->children;
320 if (n2->name == "a") {
321 var href = n2->get_prop ("href");
322 if (href != null && lift.href == null)
324 var n3 = n2->children;
326 if (n3->name == "text")
329 lift.city_from = n3->content;
332 lift.city_to = n3->content;
335 parse_date (n3->content, out lift.time);
338 parse_time (n3->content.strip (), out lift.time);
341 lift.places = n3->content.to_int ();
344 lift.price = n3->content;
347 print ("TEXT:%s\n", n3->content);
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);
374 public async bool update_lift_details (Lift lift) {
375 string url = BASE_URI + lift.href;
377 var doc = yield get_html_document (url);
379 print ("Error: parsing failed");
380 print ("%s\n", (string) message.response_body.data);
384 var table = search_tag_by_class (doc->children, "table", "lift");
386 print ("Error: does not contain lift table");
387 print ("%s\n", (string) message.response_body.data);
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")
399 string text = n2->children->content;
401 if (text != "Strecke & Infos" && text != " " && !text.has_prefix ("\xc2\xa0") &&
403 text != "Freie Pl\xc3\xa4tze" &&
407 text != "Telefon 2" &&
408 text != "E-Mail 1" &&
410 text != "Beschreibung")
417 // Skip text between td nodes
418 if (n2->name == "text")
421 if (n2 == null || n2->name != "td" || n2->children == null)
424 if (n2->children->name == "img") {
425 // FIXME: email image
426 // n2->children->get_prop ("src"))
430 if (n2->children->name == "div" && text == "Beschreibung") {
431 var n3 = n2->children->children;
432 lift.description = "";
434 if (n3->name == "text")
435 lift.description += n3->content.strip () + "\n";
439 } else if (n2->children->name != "text") {
443 var text1 = n2->children->content.strip ();
445 if (text == "Freie Pl\xc3\xa4tze")
446 lift.places = text1.to_int ();
447 else if (text == "Name")
449 else if (text == "Handy")
451 else if (text == "Telefon")
453 else if (text == "Telefon 2")
455 else if (text == "E-Mail 1")
457 else if (text != "Strecke & Infos" && text != " " &&
458 !text.has_prefix ("\xc2\xa0") && text != "Datum" &&
466 // Skip text between td nodes
467 if (n2->name == "text")
470 if (n2 == null || n2->name != "td" ||
471 n2->children == null)
474 if (n2->children->name == "span" &&
475 n2->children->get_prop ("class") == "icon_non_smoker") {
476 lift.flags |= LiftFlags.NON_SMOKER;
478 } else if (n2->children->name == "span" &&
479 n2->children->get_prop ("class") == "icon_smoker") {
480 lift.flags |= LiftFlags.SMOKER;
482 } else if (n2->children->name != "text")
485 var text2 = n2->children->content.strip ();
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")
501 else if (text1 == "ADAC-Mitglied" && text2 != "nein")
502 lift.flags |= LiftFlags.ADAC_MEMBER;
506 // The paragraph after the table contains the date of last modification
508 for (n = p->children; n != null; n = n->next) {
509 if (n->name != "text")
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"
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)
524 if (n->children != null) {
525 var found = search_tag_by_property (n->children, tag, prop, val);
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);
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);
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);
545 void parse_date (string date, out Time time) {
547 if (date.length == 11)
548 date = date.offset (3);
549 if (date.length != 8)
551 var res = date.scanf ("%02d.%02d.%02d", out time.day, out time.month, out year);
552 time.year = year + 2000;
555 void parse_time (string time, out Time result) {
556 var res = time.scanf ("%d.%02d Uhr", out result.hour, out result.minute);