]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Parse car description, public transport description and bus stop.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 12 Dec 2021 14:06:59 +0000 (15:06 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 12 Dec 2021 14:07:07 +0000 (15:07 +0100)
bots/sledrun_wikitext_to_json.py
wrpylib/templates/sledrun_wiki.txt

index 4bc22162fc900fc155d46e315142b5da6a6af985..400068ff95faad9715307a605717881648285d1e 100644 (file)
@@ -10,10 +10,12 @@ The following generators and filters are supported:
 &params;
 """
 import json
+from typing import Any, Optional
 
 import mwparserfromhell
 import pywikibot
-from mwparserfromhell.nodes import Tag, Text, ExternalLink
+from mwparserfromhell.nodes import Tag, Text, ExternalLink, Template
+from mwparserfromhell.wikicode import Wikicode
 from pywikibot import pagegenerators, Page
 from pywikibot.bot import (
     AutomaticTWSummaryBot,
@@ -27,13 +29,21 @@ from pywikibot.site._namespace import BuiltinNamespace
 
 from wrpylib.wrmwmarkup import create_sledrun_wiki, lonlat_to_json, lonlat_ele_to_json, parse_wrmap
 from wrpylib.wrvalidators import rodelbahnbox_from_template, tristate_german_to_str, difficulty_german_to_str, \
-    avalanches_german_to_str, public_transport_german_to_str, opt_str_opt_comment_enum_to_str
+    avalanches_german_to_str, public_transport_german_to_str, opt_str_opt_comment_enum_to_str, opt_lonlat_from_str, \
+    opt_uint_from_str
 
 from pywikibot.site import Namespace
 
 docuReplacements = {'&params;': pagegenerators.parameterHelp}
 
 
+def str_or_none(value: Any) -> Optional[str]:
+    if value is not None:
+        return str(value)
+    return None
+
+
+
 class SledrunWikiTextToJsonBot(
     SingleSiteBot,
     ConfigParserBot,
@@ -166,6 +176,46 @@ class SledrunWikiTextToJsonBot(
             if v is not None:
                 sledrun_json['public_transport'] = public_transport_german_to_str(v)
 
+            for v in wikicode.get_sections(levels=[2], matches='Anreise mit öffentlichen Verkehrsmitteln',
+                                           include_headings=False):
+                w = next((w for w in v.nodes if isinstance(w, Tag) and w.wiki_markup == '*'), None)
+                if w is not None:
+                    x = str(Wikicode(v.nodes[:v.nodes.index(w)])).strip()
+                    if x:
+                        sledrun_json["public_transport_description"] = str(x)
+                x = []
+                y = None
+                for w in v.nodes:
+                    if isinstance(w, Template):
+                        if w.name == 'Haltestelle':
+                            if y is not None:
+                                x.append(y)
+                            y = {}
+                            z = w.get(1, None)
+                            if z is not None:
+                                y['municipality'] = str(z)
+                            z = w.get(2, None)
+                            if z is not None:
+                                y['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
+                break
+
+            for v in wikicode.get_sections(levels=[2], matches='Anreise mit dem Auto'):
+                for w in v.ifilter_text(recursive=False):
+                    x = w.strip()
+                    if x:
+                        sledrun_json["car_description"] = str(x)
+                        break
+                break
+
             x = []
             for v in wikicode.get_sections(levels=[2], matches='Allgemeines'):
                 i = iter(v.nodes)
index ddcf0426ee930cca84039f52736890236b8c90cf..f0ee1d1bf8a3a2bfa1e2eb135080011b3780be36 100644 (file)
@@ -65,7 +65,7 @@
 {%- endif %}
 
 {% for pt_stop in public_transport_stops -%}
-* {{ h.list_template('Haltestelle', [pt_stop.municipality, pt_stop.name_local, h.json_pos_ele_position(pt_stop.get('position', '')), h.json_pos_ele_elevation(pt_stop.get('position', ''))]) }}
+* {{ h.list_template('Haltestelle', [pt_stop.municipality, pt_stop.name_local, h.json_pos_ele_position(pt_stop.get('position', {})), h.json_pos_ele_elevation(pt_stop.get('position', {}))]) }}
 {% for template_name in ['monitor_template', 'route_arrival_template', 'route_departure_template'] -%}
 {% if template_name in pt_stop -%}
 ** {{ h.json_template(pt_stop[template_name]) }}