From: Philipp Zabel Date: Fri, 25 Jun 2010 16:50:28 +0000 (+0200) Subject: Adac Mitfahrclub: add my offers parser X-Git-Url: http://git.maemo.org/git/?p=beifahrer;a=commitdiff_plain;h=d49043818b7df16a83ea0ce1c1d7344e68f2b70c Adac Mitfahrclub: add my offers parser --- diff --git a/src/adac-mitfahrclub.vala b/src/adac-mitfahrclub.vala index bf5f25d..2fca248 100644 --- a/src/adac-mitfahrclub.vala +++ b/src/adac-mitfahrclub.vala @@ -43,7 +43,8 @@ public enum LiftFlags { SMOKER = 1, NON_SMOKER = 2, ADAC_MEMBER = 4, - WOMEN_ONLY = 8 + WOMEN_ONLY = 8, + ACTIVE = 16, } public class Lift : Object { @@ -812,6 +813,136 @@ public class AdacMitfahrclub { return my_info; } + public string get_my_offers_url () { + return HTTP_BASE_URI + "/lifts/mysinglelifts"; + } + + public async List? get_my_offers () { + var doc = yield get_html_document (get_my_offers_url ()); + if (doc == null) { + stderr.printf ("Error: parsing failed\n"); + return null; + } + + var table = search_tag_by_class (doc->children, "table", "list"); + if (table == null) { + stderr.printf ("Error: does not contain user table\n"); + return null; + } + + var list = new List (); + for (var n = table->children; n != null; n = n->next) { + if (n->name == "tr") { + var lift = parse_offer_row (n); + if (lift != null) // Skip the title row + list.append ((owned) lift); + } + } + + if (table->next != null && table->next->name == "div") { + var text = get_child_text_content (table->next); + if (text != null) { + print ("\"%s\"\n", text); + if (text == "Sie haben derzeit keine einmaligen Fahrten eingetragen") { + print ("NO ENTRIES\n"); + } + } + } + + return list; + } + + Lift? parse_offer_row (Xml.Node *tr) { + var lift = new Lift (); + + // checkbox + var td = get_next_td (tr->children); + if (td == null) + return null; + + // action + td = get_next_td (td->next); + if (td == null) + return null; + // FIXME: get uri + + // type + td = get_next_td (td->next); + if (td == null) + return null; + var text = get_child_text_content (td); + if (text == null) + return null; + // FIXME == + if (text != "Mitfahrer") + return null; + + // point of departure + td = get_next_td (td->next); + if (td == null) + return null; + text = get_child_text_content (td); + if (text == null) + return null; + lift.city_from = text; + + // point of arrival + td = get_next_td (td->next); + if (td == null) + return null; + text = get_child_text_content (td); + if (text == null) + return null; + lift.city_to = text; + + // date + td = get_next_td (td->next); + if (td == null) + return null; + text = get_child_text_content (td); + if (text == null) + return null; + parse_date (text, out lift.time); + + // time + td = get_next_td (td->next); + if (td == null) + return null; + text = get_child_text_content (td); + if (text == null) + return null; + parse_time (text, out lift.time); + + // active? + td = get_next_td (td->next); + if (td == null) + return null; + var a = td->children; + if (a == null || a->name != "a") + return null; + text = a->get_prop ("class"); + if (text == "status icon icon_ajax_active") + lift.flags |= LiftFlags.ACTIVE; + + return lift; + } + + Xml.Node* get_next_td (Xml.Node *n) { + while (n != null) { + if (n->name == "td") + return n; + n = n->next; + } + return null; + } + + unowned string get_child_text_content (Xml.Node *n) { + if (n->children != null && n->children->name == "text") + return n->children->content; + else + return null; + } + Xml.Node* search_tag_by_property (Xml.Node* node, string tag, string prop, string val) requires (node != null) { for (var n = node; n != null; n = n->next) { if (n->name == tag && n->get_prop (prop) == val) @@ -839,9 +970,11 @@ public class AdacMitfahrclub { void parse_date (string date, out Time time) { int year; - if (date.length == 11) + if (date.length == 12) // "Mo, 01.02.03" + date = date.offset (4); + else if (date.length == 11) // "Mo 01.02.03" date = date.offset (3); - if (date.length != 8) + if (date.length != 8) // "01.02.03" return; var res = date.scanf ("%02d.%02d.%02d", out time.day, out time.month, out year); time.year = year + 2000;