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;
87 List<City> city_list = null;
89 public AdacMitfahrclub () {
90 session = new Soup.SessionAsync ();
93 private void message_finished (Soup.Session session, Soup.Message message) {
94 Idle.add (((CallbackMessage) message).callback);
97 private async Html.Doc* get_html_document (string url) {
98 var message = new CallbackMessage (url, get_html_document.callback);
99 session.queue_message (message, message_finished);
103 return Html.Doc.read_memory ((char[]) message.response_body.data, (int) message.response_body.length,
104 url, null, Html.ParserOption.NOERROR | Html.ParserOption.NOWARNING);
107 private void save_city_list () {
108 FileStream list_file = FileStream.open ("/home/user/.cache/beifahrer/city_list", "w");
109 if (list_file == null)
112 foreach (unowned City city in city_list) {
113 if (city.north != 0.0 || city.south != 0.0 || city.east != 0.0 || city.west != 0.0)
114 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);
115 else if (city.latitude != 0.0 || city.longitude != 0.0)
116 list_file.printf ("%d\t%s\t%f\t%f\n", city.number, city.name, city.latitude, city.longitude);
118 list_file.printf ("%d\t%s\n", city.number, city.name);
122 private bool load_city_list () {
123 FileStream list_file = FileStream.open ("/home/user/.cache/beifahrer/city_list", "r");
124 if (list_file == null)
125 list_file = FileStream.open ("/usr/share/beifahrer/city_list", "r");
126 if (list_file == null)
129 city_list = new List<City> ();
130 string line = list_file.read_line ();
131 while (line != null) {
132 var split_line = line.split ("\t");
133 if (split_line.length < 2)
135 int number = split_line[0].to_int ();
136 weak string name = split_line[1];
138 var city = new City (number, name);
139 if (split_line.length >= 4) {
140 city.latitude = split_line[2].to_double ();
141 city.longitude = split_line[3].to_double ();
143 if (split_line.length >= 8) {
144 city.north = split_line[4].to_double ();
145 city.south = split_line[5].to_double ();
146 city.east = split_line[6].to_double ();
147 city.west = split_line[7].to_double ();
149 city_list.append ((owned) city);
151 line = list_file.read_line ();
157 public unowned List<City>? get_city_list () {
158 if (city_list != null)
161 if (load_city_list ())
167 public async unowned List<City>? download_city_list () {
168 var doc = yield get_html_document (BASE_URI);
170 stderr.printf ("Error: parsing failed\n");
174 var form = search_tag_by_id (doc->children, "form", "search_national_form");
176 stderr.printf ("Error: does not contain search_national_form\n");
180 var select = search_tag_by_name (form->children, "select", "city_from");
181 if (select == null) {
182 stderr.printf ("Error: does not contain city_from\n");
186 city_list = new List<City> ();
187 for (var n = select->children; n != null; n = n->next) {
188 if (n->name == "option" && n->children != null && n->children->name == "text") {
189 int number = n->get_prop ("value").to_int ();
190 // Skip 0 "Alle St.dte"
193 var city = new City(number,
194 n->children->content);
195 city_list.append ((owned) city);
199 // TODO: get coordinates
206 private int get_city_number (string name) {
207 foreach (unowned City city in city_list) {
208 if (city.name == name)
214 public unowned City find_nearest_city (double latitude, double longitude) {
215 unowned City result = null;
216 double min_distance = 0.0;
217 bool in_result = false;
219 foreach (unowned City city in city_list) {
220 double lat = latitude - city.latitude;
221 double lng = longitude - city.longitude;
222 double distance = lat * lat + lng * lng;
223 bool in_city = ((city.south <= latitude <= city.north) &&
224 (city.west <= longitude <= city.east));
226 if ((result == null) ||
227 (in_city && !in_result) ||
228 (in_city && in_result && distance / city.bb_area () < min_distance / result.bb_area ()) ||
229 (!in_city && !in_result && distance < min_distance)) {
231 min_distance = distance;
239 public async List<Lift>? get_lift_list (string city_from, string city_to, Date date, int tolerance = 0) {
240 if (city_list == null)
243 int num_from = get_city_number (city_from);
245 stderr.printf ("Unknown city: %s\n", city_to);
249 int num_to = get_city_number (city_to);
251 stderr.printf ("Unknown city: %s\n", city_to);
255 string url = BASE_URI + "/mitfahrclub/%s/%s/b.html".printf (
260 url += "?type=b&city_from=%d&radius_from=0&city_to=%d&radius_to=0".printf (
265 url += "&date=date&day=%d&month=%d&year=%d&tolerance=%d&smoking=&avg_speed=&".printf (
272 var doc = yield get_html_document (url);
274 stderr.printf ("Error: parsing failed\n");
278 var table = search_tag_by_class (doc->children, "table", "list p_15");
280 stderr.printf ("Error: does not contain list p_15 table\n");
284 var list = new List<Lift> ();
285 for (var n = table->children; n != null; n = n->next) {
286 if (n->name == "tr") {
287 var lift = parse_lift_row (n->children);
288 if (lift.city_from != null) // Skip the title row
289 list.append ((owned) lift);
294 var div = table->next;
295 if (div != null && div->get_prop ("class") == "error-message") {
296 if (div->children == null || div->children->content == null ||
297 !div->children->content.has_prefix ("Es sind leider noch keine Einträge vorhanden.")) {
298 stderr.printf ("Got an unknown error message!\n");
299 if (div->children != null && div->children->content != null)
300 stderr.printf ("\"%s\"\n", div->children->content);
307 Lift parse_lift_row (Xml.Node* node) {
308 var lift = new Lift ();
310 for (var n = node; n != null; n = n->next) {
311 if (n->name == "td") {
312 var n2 = n->children;
314 if (n2->name == "a") {
315 var href = n2->get_prop ("href");
316 if (href != null && lift.href == null)
318 var n3 = n2->children;
320 if (n3->name == "text")
323 lift.city_from = n3->content;
326 lift.city_to = n3->content;
329 parse_date (n3->content, out lift.time);
332 parse_time (n3->content.strip (), out lift.time);
335 lift.places = n3->content.to_int ();
338 lift.price = n3->content;
341 print ("TEXT:%s\n", n3->content);
344 if (n3->name == "span") {
345 string class = n3->get_prop ("class");
346 if (class == "icon_smoker")
347 lift.flags |= LiftFlags.SMOKER;
348 else if (class == "icon_non_smoker")
349 lift.flags |= LiftFlags.NON_SMOKER;
350 else if (class == "icon_adac")
351 lift.flags |= LiftFlags.ADAC_MEMBER;
352 else if (class == "icon_women")
353 lift.flags |= LiftFlags.WOMEN_ONLY;
354 else if (class != null)
355 print ("SPAN %s\n", class);
368 public async bool update_lift_details (Lift lift) {
369 string url = BASE_URI + lift.href;
371 var doc = yield get_html_document (url);
373 stderr.printf ("Error: parsing failed\n");
377 var table = search_tag_by_class (doc->children, "table", "lift");
379 stderr.printf ("Error: does not contain lift table\n");
384 for (n = table->children; n != null; n = n->next) {
385 if (n->name == "tr") {
386 var n2 = n->children;
387 if (n2 == null || n2->name != "td" ||
388 n2->children == null || n2->children->name != "text")
391 string text = n2->children->content;
393 if (text != "Strecke & Infos" && text != " " && !text.has_prefix ("\xc2\xa0") &&
395 text != "Freie Pl\xc3\xa4tze" &&
399 text != "Telefon 2" &&
400 text != "E-Mail 1" &&
402 text != "Beschreibung")
409 // Skip text between td nodes
410 if (n2->name == "text")
413 if (n2 == null || n2->name != "td" || n2->children == null)
416 if (n2->children->name == "img") {
417 // FIXME: email image
418 // n2->children->get_prop ("src"))
422 if (n2->children->name == "div" && text == "Beschreibung") {
423 var n3 = n2->children->children;
424 lift.description = "";
426 if (n3->name == "text")
427 lift.description += n3->content.strip () + "\n";
431 } else if (n2->children->name != "text") {
435 var text1 = n2->children->content.strip ();
437 if (text == "Freie Pl\xc3\xa4tze")
438 lift.places = text1.to_int ();
439 else if (text == "Name")
441 else if (text == "Handy")
443 else if (text == "Telefon")
445 else if (text == "Telefon 2")
447 else if (text == "E-Mail 1")
449 else if (text != "Strecke & Infos" && text != " " &&
450 !text.has_prefix ("\xc2\xa0") && text != "Datum" &&
458 // Skip text between td nodes
459 if (n2->name == "text")
462 if (n2 == null || n2->name != "td" ||
463 n2->children == null)
466 if (n2->children->name == "span" &&
467 n2->children->get_prop ("class") == "icon_non_smoker") {
468 lift.flags |= LiftFlags.NON_SMOKER;
470 } else if (n2->children->name == "span" &&
471 n2->children->get_prop ("class") == "icon_smoker") {
472 lift.flags |= LiftFlags.SMOKER;
474 } else if (n2->children->name != "text")
477 var text2 = n2->children->content.strip ();
480 lift.city_from = text2;
481 else if (text1.has_prefix ("\xc3\xbc"))
482 lift.city_via.append (text2);
483 else if (text1 == "nach")
484 lift.city_to = text2;
485 else if (text1 == "Datum")
486 parse_date (text2, out lift.time);
487 else if (text1 == "Uhrzeit")
488 parse_time (text2, out lift.time);
489 else if (text1 == "Raucher")
490 print ("Raucher: %s\n", text2);
491 else if (text1 == "Fahrpreis")
493 else if (text1 == "ADAC-Mitglied" && text2 != "nein")
494 lift.flags |= LiftFlags.ADAC_MEMBER;
498 // The paragraph after the table contains the date of last modification
500 for (n = p->children; n != null; n = n->next) {
501 if (n->name != "text")
504 var s = n->content.strip ();
505 if (s.has_prefix ("Letztmalig aktualisiert am "))
506 lift.modified = s.offset (27).dup (); // "Do 15.04.2010 20:32"
512 Xml.Node* search_tag_by_property (Xml.Node* node, string tag, string prop, string val) requires (node != null) {
513 for (var n = node; n != null; n = n->next) {
514 if (n->name == tag && n->get_prop (prop) == val)
516 if (n->children != null) {
517 var found = search_tag_by_property (n->children, tag, prop, val);
525 Xml.Node* search_tag_by_id (Xml.Node* node, string tag, string id) requires (node != null) {
526 return search_tag_by_property (node, tag, "id", id);
529 Xml.Node* search_tag_by_name (Xml.Node* node, string tag, string name) requires (node != null) {
530 return search_tag_by_property (node, tag, "name", name);
533 Xml.Node* search_tag_by_class (Xml.Node* node, string tag, string @class) requires (node != null) {
534 return search_tag_by_property (node, tag, "class", @class);
537 void parse_date (string date, out Time time) {
539 if (date.length == 11)
540 date = date.offset (3);
541 if (date.length != 8)
543 var res = date.scanf ("%02d.%02d.%02d", out time.day, out time.month, out year);
544 time.year = year + 2000;
547 void parse_time (string time, out Time result) {
548 var res = time.scanf ("%d.%02d Uhr", out result.hour, out result.minute);