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;
56 public LiftFlags flags;
60 public List<string> city_via;
66 public string description;
67 public string modified;
70 public class CallbackMessage : Soup.Message {
71 public SourceFunc callback;
73 public CallbackMessage (string url, SourceFunc? _callback) {
76 set_uri (new Soup.URI (url));
80 public class AdacMitfahrclub {
81 const string BASE_URI = "http://mitfahrclub.adac.de";
83 Soup.SessionAsync session;
85 List<City> city_list = null;
87 public AdacMitfahrclub () {
88 session = new Soup.SessionAsync ();
91 private void message_finished (Soup.Session session, Soup.Message message) {
92 Idle.add (((CallbackMessage) message).callback);
95 private async Html.Doc* get_html_document (string url) {
96 message = new CallbackMessage (url, get_html_document.callback);
97 session.queue_message (message, message_finished);
101 return Html.Doc.read_memory ((char[]) message.response_body.data, (int) message.response_body.length,
102 url, null, Html.ParserOption.NOERROR | Html.ParserOption.NOWARNING);
105 private void save_city_list () {
106 FileStream list_file = FileStream.open ("/home/user/.cache/beifahrer/city_list", "w");
107 if (list_file == null)
110 foreach (unowned City city in city_list) {
111 if (city.north != 0.0 || city.south != 0.0 || city.east != 0.0 || city.west != 0.0)
112 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);
113 else if (city.latitude != 0.0 || city.longitude != 0.0)
114 list_file.printf ("%d\t%s\t%f\t%f\n", city.number, city.name, city.latitude, city.longitude);
116 list_file.printf ("%d\t%s\n", city.number, city.name);
120 private bool load_city_list () {
121 FileStream list_file = FileStream.open ("/home/user/.cache/beifahrer/city_list", "r");
122 if (list_file == null)
123 list_file = FileStream.open ("/usr/share/beifahrer/city_list", "r");
124 if (list_file == null)
127 city_list = new List<City> ();
128 string line = list_file.read_line ();
129 while (line != null) {
130 var split_line = line.split ("\t");
131 if (split_line.length < 2)
133 int number = split_line[0].to_int ();
134 weak string name = split_line[1];
136 var city = new City (number, name);
137 if (split_line.length >= 4) {
138 city.latitude = split_line[2].to_double ();
139 city.longitude = split_line[3].to_double ();
141 if (split_line.length >= 8) {
142 city.north = split_line[4].to_double ();
143 city.south = split_line[5].to_double ();
144 city.east = split_line[6].to_double ();
145 city.west = split_line[7].to_double ();
147 city_list.append ((owned) city);
149 line = list_file.read_line ();
155 public unowned List<City>? get_city_list () {
156 if (city_list != null)
159 if (load_city_list ())
165 public async unowned List<City>? download_city_list () {
166 var doc = yield get_html_document (BASE_URI);
168 print ("Error: parsing failed");
169 print ("%s\n", (string) message.response_body.data);
173 var form = search_tag_by_id (doc->children, "form", "search_national_form");
175 print ("Error: does not contain search_national_form");
176 print ("%s\n", (string) message.response_body.data);
180 var select = search_tag_by_name (form->children, "select", "city_from");
181 if (select == null) {
182 print ("Error: does not contain city_from");
183 print ("%s\n", (string) message.response_body.data);
187 city_list = new List<City> ();
188 for (var n = select->children; n != null; n = n->next) {
189 if (n->name == "option" && n->children != null && n->children->name == "text") {
190 int number = n->get_prop ("value").to_int ();
191 // Skip 0 "Alle St.dte"
194 var city = new City(number,
195 n->children->content);
196 city_list.append ((owned) city);
200 // TODO: get coordinates
207 private int get_city_number (string name) {
208 foreach (unowned City city in city_list) {
209 if (city.name == name)
215 public unowned City find_nearest_city (double latitude, double longitude) {
216 unowned City result = null;
217 double min_distance = 0.0;
218 bool in_result = false;
220 foreach (unowned City city in city_list) {
221 double lat = latitude - city.latitude;
222 double lng = longitude - city.longitude;
223 double distance = lat * lat + lng * lng;
224 bool in_city = ((city.south <= latitude <= city.north) &&
225 (city.west <= longitude <= city.east));
227 if ((result == null) ||
228 (in_city && !in_result) ||
229 (in_city && in_result && distance / city.bb_area () < min_distance / result.bb_area ()) ||
230 (!in_city && !in_result && distance < min_distance)) {
232 min_distance = distance;
240 public async List<Lift>? get_lift_list (string city_from, string city_to, Date date, int tolerance = 0) {
241 if (city_list == null)
244 int num_from = get_city_number (city_from);
246 stderr.printf ("Unknown city: %s\n", city_to);
250 int num_to = get_city_number (city_to);
252 stderr.printf ("Unknown city: %s\n", city_to);
256 string url = BASE_URI + "/mitfahrclub/%s/%s/b.html".printf (
261 url += "?type=b&city_from=%d&radius_from=0&city_to=%d&radius_to=0".printf (
266 url += "&date=date&day=%d&month=%d&year=%d&tolerance=%d&smoking=&avg_speed=&".printf (
273 var doc = yield get_html_document (url);
275 print ("Error: parsing failed");
276 print ("%s\n", (string) message.response_body.data);
280 var table = search_tag_by_class (doc->children, "table", "list p_15");
282 print ("Error: does not contain list p_15 table");
283 print ("%s\n", (string) message.response_body.data);
287 var list = new List<Lift> ();
288 for (var n = table->children; n != null; n = n->next) {
289 if (n->name == "tr") {
290 var lift = parse_lift_row (n->children);
291 if (lift.city_from != null) // Skip the title row
292 list.append ((owned) lift);
297 var div = table->next;
298 if (div != null && div->get_prop ("class") == "error-message") {
299 if (div->children == null || div->children->content == null ||
300 !div->children->content.has_prefix ("Es sind leider noch keine Einträge vorhanden.")) {
301 stderr.printf ("Got an unknown error message!\n");
302 if (div->children != null && div->children->content != null)
303 stderr.printf ("\"%s\"\n", div->children->content);
310 Lift parse_lift_row (Xml.Node* node) {
311 var lift = new Lift ();
313 for (var n = node; n != null; n = n->next) {
314 if (n->name == "td") {
315 var n2 = n->children;
317 if (n2->name == "a") {
318 var href = n2->get_prop ("href");
319 if (href != null && lift.href == null)
321 var n3 = n2->children;
323 if (n3->name == "text")
326 lift.city_from = n3->content;
329 lift.city_to = n3->content;
332 lift.date = n3->content;
335 lift.time = n3->content;
338 lift.places = n3->content.to_int ();
341 lift.price = n3->content;
344 print ("TEXT:%s\n", n3->content);
347 if (n3->name == "span") {
348 string class = n3->get_prop ("class");
349 if (class == "icon_smoker")
350 lift.flags |= LiftFlags.SMOKER;
351 else if (class == "icon_non_smoker")
352 lift.flags |= LiftFlags.NON_SMOKER;
353 else if (class == "icon_adac")
354 lift.flags |= LiftFlags.ADAC_MEMBER;
355 else if (class == "icon_women")
356 lift.flags |= LiftFlags.WOMEN_ONLY;
357 else if (class != null)
358 print ("SPAN %s\n", class);
371 public async bool update_lift_details (Lift lift) {
372 string url = BASE_URI + lift.href;
374 var doc = yield get_html_document (url);
376 print ("Error: parsing failed");
377 print ("%s\n", (string) message.response_body.data);
381 var table = search_tag_by_class (doc->children, "table", "lift");
383 print ("Error: does not contain lift table");
384 print ("%s\n", (string) message.response_body.data);
389 for (n = table->children; n != null; n = n->next) {
390 if (n->name == "tr") {
391 var n2 = n->children;
392 if (n2 == null || n2->name != "td" ||
393 n2->children == null || n2->children->name != "text")
396 string text = n2->children->content;
398 if (text != "Strecke & Infos" && text != " " && !text.has_prefix ("\xc2\xa0") &&
400 text != "Freie Pl\xc3\xa4tze" &&
404 text != "Telefon 2" &&
405 text != "E-Mail 1" &&
407 text != "Beschreibung")
414 // Skip text between td nodes
415 if (n2->name == "text")
418 if (n2 == null || n2->name != "td" || n2->children == null)
421 if (n2->children->name == "img") {
422 // FIXME: email image
423 // n2->children->get_prop ("src"))
427 if (n2->children->name == "div" && text == "Beschreibung") {
428 var n3 = n2->children->children;
429 lift.description = "";
431 if (n3->name == "text")
432 lift.description += n3->content.strip () + "\n";
436 } else if (n2->children->name != "text") {
440 var text1 = n2->children->content.strip ();
444 else if (text == "Freie Pl\xc3\xa4tze")
445 lift.places = text1.to_int ();
446 else if (text == "Name")
448 else if (text == "Handy")
450 else if (text == "Telefon")
452 else if (text == "Telefon 2")
454 else if (text == "E-Mail 1")
456 else if (text != "Strecke & Infos" && text != " " &&
457 !text.has_prefix ("\xc2\xa0") && text != "Datum" &&
465 // Skip text between td nodes
466 if (n2->name == "text")
469 if (n2 == null || n2->name != "td" ||
470 n2->children == null)
473 if (n2->children->name == "span" &&
474 n2->children->get_prop ("class") == "icon_non_smoker") {
475 lift.flags |= LiftFlags.NON_SMOKER;
477 } else if (n2->children->name == "span" &&
478 n2->children->get_prop ("class") == "icon_smoker") {
479 lift.flags |= LiftFlags.SMOKER;
481 } else if (n2->children->name != "text")
484 var text2 = n2->children->content.strip ();
487 lift.city_from = text2;
488 else if (text1.has_prefix ("\xc3\xbc"))
489 lift.city_via.append (text2);
490 else if (text1 == "nach")
491 lift.city_to = text2;
492 else if (text1 == "Datum")
494 else if (text1 == "Uhrzeit")
496 else if (text1 == "Raucher")
497 print ("Raucher: %s\n", text2);
498 else if (text1 == "Fahrpreis")
500 else if (text1 == "ADAC-Mitglied" && text2 != "nein")
501 lift.flags |= LiftFlags.ADAC_MEMBER;
505 // The paragraph after the table contains the date of last modification
507 for (n = p->children; n != null; n = n->next) {
508 if (n->name != "text")
511 var s = n->content.strip ();
512 if (s.has_prefix ("Letztmalig aktualisiert am "))
513 lift.modified = s.offset (27).dup (); // "Do 15.04.2010 20:32"
519 Xml.Node* search_tag_by_property (Xml.Node* node, string tag, string prop, string val) requires (node != null) {
520 for (var n = node; n != null; n = n->next) {
521 if (n->name == tag && n->get_prop (prop) == val)
523 if (n->children != null) {
524 var found = search_tag_by_property (n->children, tag, prop, val);
532 Xml.Node* search_tag_by_id (Xml.Node* node, string tag, string id) requires (node != null) {
533 return search_tag_by_property (node, tag, "id", id);
536 Xml.Node* search_tag_by_name (Xml.Node* node, string tag, string name) requires (node != null) {
537 return search_tag_by_property (node, tag, "name", name);
540 Xml.Node* search_tag_by_class (Xml.Node* node, string tag, string @class) requires (node != null) {
541 return search_tag_by_property (node, tag, "class", @class);