]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Parse external public transport links.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 23 Jan 2022 21:14:53 +0000 (22:14 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 23 Jan 2022 21:14:53 +0000 (22:14 +0100)
bots/sledrun_wikitext_to_json.py
wrpylib/templates/sledrun_wiki.txt

index 11070486fe7bf6a80171b2f14c7b9ff5a8361a5f..428316fdf983f4e69fdd61ab58c0eeb0ccd67895 100644 (file)
@@ -66,6 +66,13 @@ def wikilink_to_json(value: Wikilink) -> dict:
     return wl
 
 
+def external_link_to_json(value: ExternalLink) -> dict:
+    link = {'url': str(value.url)}
+    if value.title is not None:
+        link['text'] = str(value.title)
+    return link
+
+
 class SledrunWikiTextToJsonBot(
     SingleSiteBot,
     ConfigParserBot,
@@ -239,55 +246,64 @@ class SledrunWikiTextToJsonBot(
                     sledrun_json['videos'] = [{'url': video.value}]
         _button_bar()
 
-        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)
+        def _public_transport():
+            pt_sections = wikicode.get_sections(levels=[2], matches='Anreise mit öffentlichen Verkehrsmitteln',
+                                                include_headings=False)
+            if len(pt_sections) < 1:
+                return
+            pt = pt_sections[0]
+            node = next((node for node in pt.nodes if isinstance(node, Tag) and node.wiki_markup == '*'), None)
+            if node is not None:
+                description = str(Wikicode(pt.nodes[:pt.nodes.index(node)])).strip()
+                if description:
+                    sledrun_json["public_transport_description"] = str(description)
 
             public_transport_stops = []
             public_transport_lines = []
+            public_transport_links = []
             ya = None
-            for w in v.nodes:
-                if isinstance(w, Template):
-                    if w.name == 'Haltestelle':
+            for node in pt.nodes:
+                if isinstance(node, Template):
+                    if node.name == 'Haltestelle':
                         if ya is not None:
                             public_transport_stops.append(ya)
                         ya = {}
-                        z = w.get(1, None)
+                        z = node.get(1, None)
                         if z is not None:
                             ya['municipality'] = str(z)
-                        z = w.get(2, None)
+                        z = node.get(2, None)
                         if z is not None:
                             ya['name_local'] = str(z)
-                        za = str_or_none(w.get(3, None))
-                        zb = str_or_none(w.get(4, None))
+                        za = str_or_none(node.get(3, None))
+                        zb = str_or_none(node.get(4, None))
                         z = lonlat_ele_to_json(opt_lonlat_from_str(za), opt_uint_from_str(zb))
                         if len(z) > 0:
                             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"]:
+                    elif node.name in ["Fahrplan Abfahrtsmonitor VVT"]:
+                        ya['monitor_template'] = template_to_json(node)
+                    elif node.name in ["Fahrplan Hinfahrt VVT"]:
+                        ya['route_arrival_template'] = template_to_json(node)
+                    elif node.name in ["Fahrplan Rückfahrt VVT"]:
+                        ya['route_departure_template'] = template_to_json(node)
+                    elif node.name in ["Fahrplan Linie VVT"]:
                         if ya is not None:
                             public_transport_stops.append(ya)
                             ya = None
                         y = {
-                            'timetable_template': template_to_json(w),
+                            'timetable_template': template_to_json(node),
                         }
                         public_transport_lines.append(y)
+                elif isinstance(node, ExternalLink):
+                    public_transport_links.append(external_link_to_json(node))
             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
+            if len(public_transport_links) > 0:
+                sledrun_json['public_transport_links'] = public_transport_links
+        _public_transport()
 
         def _car():
             car_section_list = wikicode.get_sections(levels=[2], matches='Anreise mit dem Auto')
@@ -346,11 +362,7 @@ class SledrunWikiTextToJsonBot(
                                 g['wr_page'] = wikilink_to_json(wiki_link)
                             ext_link = next(wiki.ifilter_external_links(), None)
                             if isinstance(ext_link, ExternalLink):
-                                el = {
-                                    'url': str(ext_link.url),
-                                    'text': str(ext_link.title)
-                                }
-                                g['weblink'] = el
+                                g['weblink'] = external_link_to_json(ext_link)
                             remaining = str(Wikicode(n for n in wiki.nodes
                                                      if isinstance(n, (Text, Tag)) and str(n).strip() != '*')).strip()
                             match = re.match(r'\((.+)\)', remaining)
@@ -391,10 +403,7 @@ class SledrunWikiTextToJsonBot(
                 w = next(i, None)
             while w is not None:
                 if isinstance(w, ExternalLink):
-                    link = {'url': w.url}
-                    if w.title is not None:
-                        link['text'] = w.title
-                    x.append(link)
+                    x.append(external_link_to_json(w))
                 elif isinstance(w, (Text, Tag)) and str(w).strip() in ['', '*', ':']:
                     pass
                 else:
index adc71be1e409081f7aecacbf9ff752aa355b5c55..d7563515187fb6831a6572698e1d99a8b4cc1225 100644 (file)
 ** {% if pt_line.timetable_template is defined %}{{ h.json_template(pt_line.timetable_template) }}
    {%- else %}{{ pt_line.name }}{% endif %}
 {%- endfor %}
-
+{% for link in public_transport_links -%}
+{% if loop.first -%}
+* '''Fahrplan''':
+{%- endif %}
+** {{ weblink(link) }}
+{%- endfor %}
 
 == Anreise mit dem Auto ==
 {% if car_description -%}