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,
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')
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)
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: