¶ms;
"""
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,
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 = {'¶ms;': pagegenerators.parameterHelp}
+def str_or_none(value: Any) -> Optional[str]:
+ if value is not None:
+ return str(value)
+ return None
+
+
+
class SledrunWikiTextToJsonBot(
SingleSiteBot,
ConfigParserBot,
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)
{%- 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]) }}