]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Add public transport lines.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 12 Dec 2021 14:45:18 +0000 (15:45 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 12 Dec 2021 14:45:18 +0000 (15:45 +0100)
bots/sledrun_wikitext_to_json.py

index 400068ff95faad9715307a605717881648285d1e..e2a6c5393265fe6c3fba103d500fb2d82c1975ba 100644 (file)
@@ -43,6 +43,15 @@ def str_or_none(value: Any) -> Optional[str]:
     return None
 
 
+def template_to_json(value: Template) -> dict:
+    parameter = []
+    for p in value.params:
+        parameter.append({'value': str(p)})
+    return {
+        'name': str(value.name),
+        'parameter': parameter
+    }
+
 
 class SledrunWikiTextToJsonBot(
     SingleSiteBot,
@@ -183,29 +192,47 @@ class SledrunWikiTextToJsonBot(
                     x = str(Wikicode(v.nodes[:v.nodes.index(w)])).strip()
                     if x:
                         sledrun_json["public_transport_description"] = str(x)
-                x = []
-                y = None
+
+                public_transport_stops = []
+                public_transport_lines = []
+                ya = None
                 for w in v.nodes:
                     if isinstance(w, Template):
                         if w.name == 'Haltestelle':
-                            if y is not None:
-                                x.append(y)
-                            y = {}
+                            if ya is not None:
+                                public_transport_stops.append(ya)
+                            ya = {}
                             z = w.get(1, None)
                             if z is not None:
-                                y['municipality'] = str(z)
+                                ya['municipality'] = str(z)
                             z = w.get(2, None)
                             if z is not None:
-                                y['name_local'] = str(z)
+                                ya['name_local'] = str(z)
                             za = str_or_none(w.get(3, None))
                             zb = str_or_none(w.get(4, None))
                             z = lonlat_ele_to_json(opt_lonlat_from_str(za), opt_uint_from_str(zb))
                             if len(z) > 0:
-                                y['position'] = z
-                if y is not None:
-                    x.append(y)
-                if len(x) > 0:
-                    sledrun_json['public_transport_stops'] = x
+                                ya['position'] = z
+                        elif w.name in ["Fahrplan Abfahrtsmonitor VVT"]:
+                            ya['monitor_template'] = template_to_json(w)
+                        elif w.name in ["Fahrplan Hinfahrt VVT"]:
+                            ya['route_arrival_template'] = template_to_json(w)
+                        elif w.name in ["Fahrplan Rückfahrt VVT"]:
+                            ya['route_departure_template'] = template_to_json(w)
+                        elif w.name in ["Fahrplan Linie VVT"]:
+                            if ya is not None:
+                                public_transport_stops.append(ya)
+                                ya = None
+                            y = {
+                                'timetable_template': template_to_json(w),
+                            }
+                            public_transport_lines.append(y)
+                if ya is not None:
+                    public_transport_stops.append(ya)
+                if len(public_transport_stops) > 0:
+                    sledrun_json['public_transport_stops'] = public_transport_stops
+                if len(public_transport_lines) > 0:
+                    sledrun_json['public_transport_lines'] = public_transport_lines
                 break
 
             for v in wikicode.get_sections(levels=[2], matches='Anreise mit dem Auto'):