#!/usr/bin/python """ User script for pywikibot (https://gerrit.wikimedia.org/r/pywikibot/core.git), tested with version 6.6.1. Put it in directory scripts/userscripts. Create a sledrun JSON page from a sledrun wikitext page (including map). The following generators and filters are supported: ¶ms; """ import io import re from itertools import takewhile, dropwhile from typing import Optional import mwparserfromhell from mwparserfromhell.nodes.extras import Parameter import pywikibot from mwparserfromhell.nodes import Tag, Text, ExternalLink, Template, Wikilink, Heading from mwparserfromhell.wikicode import Wikicode from pywikibot import pagegenerators, Page from pywikibot.bot import ( AutomaticTWSummaryBot, ConfigParserBot, ExistingPageBot, NoRedirectPageBot, SingleSiteBot, ) from pywikibot.logging import warning 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_lonlat_from_str, \ opt_uint_from_str docuReplacements = {'¶ms;': pagegenerators.parameterHelp} 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 } def wikilink_to_json(value: Wikilink) -> dict: wl = {'title': str(value.title)} if value.text is not None: wl['text'] = str(value.text) 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, ExistingPageBot, NoRedirectPageBot, AutomaticTWSummaryBot, ): def treat_page(self) -> None: """Load the given page, do some changes, and save it.""" wikitext_content_model = 'wikitext' if self.current_page.content_model != wikitext_content_model: warning(f"The content model of {self.current_page.title()} is {self.current_page.content_model} " f"instead of {wikitext_content_model}.") return wikicode = mwparserfromhell.parse(self.current_page.text) wikilink_list = wikicode.filter_wikilinks() category_sledrun = 'Kategorie:Rodelbahn' if sum(1 for c in wikilink_list if c.title == category_sledrun) == 0: warning(f'The page {self.current_page.title()} does not have category {category_sledrun}.') return sledrun_json_page = Page(self.site, self.current_page.title() + '/Rodelbahn.json') if sledrun_json_page.exists(): warning(f"{sledrun_json_page.title()} already exists, skipping {self.current_page.title()}.") return map_json_page = Page(self.site, self.current_page.title() + '/Landkarte.json') if map_json_page.exists(): warning(f"{map_json_page.title()} already exists, skipping {self.current_page.title()}.") return map_json = None v = wikicode.filter_tags(matches='wrmap') if len(v) > 0: map_json = parse_wrmap(str(v[0])) sledrun_json = { "name": self.current_page.title(), "aliases": [], "entry_under_construction": sum(1 for c in wikilink_list if c.text == 'Kategorie:In Arbeit') > 0, } for v in wikicode.get_sections(levels=[2], matches='Allgemeines'): for w in v.ifilter_text(recursive=False): x = w.strip() if x: sledrun_json["description"] = str(x) break break rbb_list = wikicode.filter_templates(recursive=False, matches=lambda t: t.name.strip() == 'Rodelbahnbox') if len(rbb_list) == 1: rbb = rodelbahnbox_from_template(rbb_list[0]) v = rbb['Bild'] if v is not None: image_page = Page(self.site, v, ns=BuiltinNamespace.FILE) if not image_page.exists(): warning(f"{image_page.title()} does not exist.") sledrun_json['image'] = v v = rbb['Länge'] if v is not None: sledrun_json['length'] = v v = rbb['Schwierigkeit'] if v is not None: sledrun_json['difficulty'] = difficulty_german_to_str(v) v = rbb['Lawinen'] if v is not None: sledrun_json['avalanches'] = avalanches_german_to_str(v) v, w = rbb['Betreiber'] if v is not None: sledrun_json['has_operator'] = v if w is not None: sledrun_json['operator'] = w v = rbb['Aufstieg möglich'] if v is not None: sledrun_json['walkup_possible'] = v v, w = rbb['Aufstieg getrennt'] if v is not None: sledrun_json['walkup_separate'] = tristate_german_to_str(v) if w is not None: sledrun_json['walkup_comment'] = w # TODO v = rbb['Gehzeit'] if v is not None: sledrun_json['walkup_time'] = v def _walkup_support(): walkup_support_rbb = rbb['Aufstiegshilfe'] if walkup_support_rbb is not None: walkup_supports = [] for walkup_support_type, comment in walkup_support_rbb: walkup_support = {'type': walkup_support_type} if comment is not None: walkup_support['comment']: comment walkup_supports.append(walkup_support) sledrun_json['walkup_supports'] = walkup_supports _walkup_support() v, w = rbb['Beleuchtungsanlage'] if v is not None: sledrun_json['nightlight_possible'] = tristate_german_to_str(v) if w is not None: sledrun_json['nightlight_possible_comment'] = w v, w = rbb['Beleuchtungstage'] if v is not None: sledrun_json['nightlight_weekdays_count'] = v if w is not None: sledrun_json['nightlight_weekdays_comment'] = w def _sled_rental(): v = rbb['Rodelverleih'] if v is not None: sledrun_json['sled_rental_direct'] = v != [] w = [] for name, comment in v: x = {} name_code = mwparserfromhell.parse(name) wiki_link = next(name_code.ifilter_wikilinks(), None) if isinstance(wiki_link, Wikilink): x['wr_page'] = wikilink_to_json(wiki_link) else: x['name'] = name if comment is not None: x['comment'] = comment w.append(x) sledrun_json['sled_rental'] = w _sled_rental() def _cachet(): v = rbb['Gütesiegel'] if v is not None: sledrun_json['cachet'] = len(v) > 0 _cachet() v = rbb['In Übersichtskarte'] if v is not None: sledrun_json['show_in_overview'] = v v = rbb['Forumid'] if v is not None: sledrun_json['forum_id'] = v v = rbb['Position'] if v is not None: sledrun_json['position'] = lonlat_to_json(v) v = lonlat_ele_to_json(rbb['Position oben'], rbb['Höhe oben']) if v != {}: sledrun_json['top'] = v v = lonlat_ele_to_json(rbb['Position unten'], rbb['Höhe unten']) if v != {}: sledrun_json['bottom'] = v v = rbb['Telefonauskunft'] if v is not None: sledrun_json['info_phone'] = [{'phone': p, 'name': n} for p, n in v] v = rbb['Öffentliche Anreise'] if v is not None: sledrun_json['public_transport'] = public_transport_german_to_str(v) def _button_bar(): bb_iter = wikicode.ifilter_templates(recursive=False, matches=lambda t: t.name.strip() == 'Buttonleiste') bb = next(bb_iter, None) if bb is not None: video = bb.get('video', None) if isinstance(video, Parameter): sledrun_json['videos'] = [{'url': video.value}] _button_bar() 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 node in pt.nodes: if isinstance(node, Template): if node.name == 'Haltestelle': if ya is not None: public_transport_stops.append(ya) ya = {} z = node.get(1, None) if z is not None: ya['municipality'] = str(z) z = node.get(2, None) if z is not None: ya['name_local'] = str(z) za = str(node.get(3, '')).strip() zb = str(node.get(4, '')).strip() z = lonlat_ele_to_json(opt_lonlat_from_str(za), opt_uint_from_str(zb)) if len(z) > 0: ya['position'] = z 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(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 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') if not car_section_list: return v = car_section_list[0] description_nodes = dropwhile(lambda w: isinstance(w, Heading), v.nodes) description_nodes = takewhile(lambda w: not (isinstance(w, Tag) and w.wiki_markup == '*'), description_nodes) if description := str(Wikicode(list(description_nodes))).strip(): sledrun_json["car_description"] = description x = [] for w in v.ifilter_templates(matches='Parkplatz'): za = str(w.get(1, '')).strip() zb = str(w.get(2, '')).strip() z = lonlat_ele_to_json(opt_lonlat_from_str(za), opt_uint_from_str(zb)) if len(z) > 0: x.append({'position': z}) if len(x) > 0: sledrun_json['car_parking'] = x x = [] for w in io.StringIO(str(v)): match = re.match(r"\*\* von \'\'\'(.+)\'\'\'(.*): ([\d.,]+) km", w.rstrip()) if match: ya, yb, yc = match.groups() yc = float(yc.replace(',', '.')) x.append({ 'km': yc, 'route': (ya.strip() + ' ' + yb.strip()).strip(), }) if len(x) > 0: sledrun_json['car_distances'] = x _car() x = [] for v in wikicode.get_sections(levels=[2], matches='Allgemeines'): def _nightlight(value: str) -> Optional[str]: line_iter = io.StringIO(value) line = next(line_iter, None) while line is not None and not line.startswith("* '''Beleuchtung''':"): line = next(line_iter, None) if line is None: return None line = line.replace("* '''Beleuchtung''':", "").strip() if len(line) > 0: return line return None w = _nightlight(str(v)) if w is not None: sledrun_json['nightlight_description'] = w def _gastronomy(value: str): gastronomy = [] line_iter = io.StringIO(value) line = next(line_iter, None) while line is not None and line.rstrip() != "* '''Hütten''':": line = next(line_iter, None) if line is None: return gastronomy while line is not None: line = next(line_iter, None) if line is not None: if line.startswith('** '): g = {} wiki = mwparserfromhell.parse(line) wiki_link = next(wiki.ifilter_wikilinks(), None) if isinstance(wiki_link, Wikilink): g['wr_page'] = wikilink_to_json(wiki_link) ext_link = next(wiki.ifilter_external_links(), None) if isinstance(ext_link, ExternalLink): 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) if match: remaining = match.group(1) if len(remaining) > 0: g['note'] = remaining gastronomy.append(g) else: break return gastronomy w = _gastronomy(str(v)) if len(w) > 0: sledrun_json['gastronomy'] = w def _sled_rental_description(): line_iter = io.StringIO(str(v)) line = next(line_iter, None) match = None while line is not None and (match := re.match(r"\* '''Rodelverleih''':(.*)", line)) is None: line = next(line_iter, None) if match is None: return result = [match.group(1)] line = next(line_iter, None) while line is not None and re.match(r"\* ", line) is None: result.append(line) line = next(line_iter, None) sledrun_json['sled_rental_description'] = ''.join(result).strip() _sled_rental_description() i = iter(v.nodes) w = next(i, None) while w is not None: if isinstance(w, Tag) and str(w) == "'''Siehe auch'''": w = next(i, None) break w = next(i, None) while w is not None: if isinstance(w, ExternalLink): x.append(external_link_to_json(w)) elif isinstance(w, (Text, Tag)) and str(w).strip() in ['', '*', ':']: pass else: break w = next(i, None) if len(x) > 0: sledrun_json['see_also'] = x sledrun_json['allow_reports'] = True impressions = None sledrun_impressions_page = Page(self.site, self.current_page.title() + '/Impressionen') if sledrun_impressions_page.exists(): impressions = sledrun_impressions_page.title() text = create_sledrun_wiki(sledrun_json, map_json, impressions) summary = 'Rodelbahnbeschreibung nach Konvertierung nach und von JSON.' self.put_current(text, summary=summary) def main(*args: str) -> None: local_args = pywikibot.handle_args(args) gen_factory = pagegenerators.GeneratorFactory() gen_factory.handle_args(local_args) gen = gen_factory.getCombinedGenerator(preload=True) if gen: bot = SledrunWikiTextToJsonBot(generator=gen) bot.run() else: pywikibot.bot.suggest_help(missing_generator=True) if __name__ == '__main__': main()