3 User script for pywikibot (https://gerrit.wikimedia.org/r/pywikibot/core.git), tested with version 6.6.1.
4 Put it in directory scripts/userscripts.
6 Create a sledrun JSON page from a sledrun wikitext page (including map).
8 The following generators and filters are supported:
14 import mwparserfromhell
16 from mwparserfromhell.nodes import Tag, Text, ExternalLink
17 from pywikibot import pagegenerators, Page
18 from pywikibot.bot import (
19 AutomaticTWSummaryBot,
25 from pywikibot.logging import warning
26 from pywikibot.site._namespace import BuiltinNamespace
28 from wrpylib.wrmwmarkup import create_sledrun_wiki, lonlat_to_json, lonlat_ele_to_json, parse_wrmap
29 from wrpylib.wrvalidators import rodelbahnbox_from_template, tristate_german_to_str, difficulty_german_to_str, \
30 avalanches_german_to_str, public_transport_german_to_str, opt_str_opt_comment_enum_to_str
32 from pywikibot.site import Namespace
34 docuReplacements = {'¶ms;': pagegenerators.parameterHelp}
37 class SledrunWikiTextToJsonBot(
42 AutomaticTWSummaryBot,
44 def treat_page(self) -> None:
45 """Load the given page, do some changes, and save it."""
46 wikitext_content_model = 'wikitext'
47 if self.current_page.content_model != wikitext_content_model:
48 warning(f"The content model of {self.current_page.title()} is {self.current_page.content_model} "
49 f"instead of {wikitext_content_model}.")
52 wikicode = mwparserfromhell.parse(self.current_page.text)
53 wikilink_list = wikicode.filter_wikilinks()
54 category_sledrun = 'Kategorie:Rodelbahn'
55 if sum(1 for c in wikilink_list if c.title == category_sledrun) == 0:
56 warning(f'The page {self.current_page.title()} does not have category {category_sledrun}.')
59 sledrun_json_page = Page(self.site, self.current_page.title() + '/Rodelbahn.json')
60 if sledrun_json_page.exists():
61 warning(f"{sledrun_json_page.title()} already exists, skipping {self.current_page.title()}.")
64 map_json_page = Page(self.site, self.current_page.title() + '/Landkarte.json')
65 if map_json_page.exists():
66 warning(f"{map_json_page.title()} already exists, skipping {self.current_page.title()}.")
70 v = wikicode.filter_tags(matches='wrmap')
72 map_json = parse_wrmap(str(v[0]))
75 "name": self.current_page.title(),
77 "entry_under_construction": sum(1 for c in wikilink_list if c.text == 'Kategorie:In Arbeit') > 0,
80 for v in wikicode.get_sections(levels=[2], matches='Allgemeines'):
81 for w in v.ifilter_text(recursive=False):
84 sledrun_json["description"] = str(x)
88 rbb_list = wikicode.filter_templates(recursive=False, matches=lambda t: t.name.strip() == 'Rodelbahnbox')
89 if len(rbb_list) == 1:
90 rbb = rodelbahnbox_from_template(rbb_list[0])
93 image_page = Page(self.site, v, ns=BuiltinNamespace.FILE)
94 if not image_page.exists():
95 warning(f"{image_page.title()} does not exist.")
96 sledrun_json['image'] = v
100 sledrun_json['length'] = v
102 v = rbb['Schwierigkeit']
104 sledrun_json['difficulty'] = difficulty_german_to_str(v)
108 sledrun_json['avalanches'] = avalanches_german_to_str(v)
110 v, w = rbb['Betreiber']
112 sledrun_json['has_operator'] = v
114 sledrun_json['operator'] = w
116 v = rbb['Aufstieg möglich']
118 sledrun_json['walkup_possible'] = v
120 v, w = rbb['Aufstieg getrennt']
122 sledrun_json['walkup_separate'] = tristate_german_to_str(v)
124 sledrun_json['walkup_comment'] = w # TODO
128 sledrun_json['walkup_time'] = v
130 v, w = rbb['Beleuchtungsanlage']
132 sledrun_json['nightlight_possible'] = tristate_german_to_str(v)
134 sledrun_json['nightlight_description'] = w
136 v = rbb['Rodelverleih']
138 sledrun_json['sled_rental_direct'] = v != []
139 sledrun_json['sled_rental_description'] = opt_str_opt_comment_enum_to_str(v)
141 v = rbb['In Übersichtskarte']
143 sledrun_json['show_in_overview'] = v
147 sledrun_json['forum_id'] = v
151 sledrun_json['position'] = lonlat_to_json(v)
153 v = lonlat_ele_to_json(rbb['Position oben'], rbb['Höhe oben'])
155 sledrun_json['top'] = v
157 v = lonlat_ele_to_json(rbb['Position unten'], rbb['Höhe unten'])
159 sledrun_json['bottom'] = v
161 v = rbb['Telefonauskunft']
163 sledrun_json['info_phone'] = [{'phone': p, 'name': n} for p, n in v]
165 v = rbb['Öffentliche Anreise']
167 sledrun_json['public_transport'] = public_transport_german_to_str(v)
170 for v in wikicode.get_sections(levels=[2], matches='Allgemeines'):
174 if isinstance(w, Tag) and str(w) == "'''Siehe auch'''":
179 if isinstance(w, ExternalLink):
180 link = {'url': w.url}
181 if w.title is not None:
182 link['text'] = w.title
184 elif isinstance(w, (Text, Tag)) and str(w).strip() in ['', '*', ':']:
190 sledrun_json['see_also'] = x
192 sledrun_json['allow_reports'] = True
194 text = create_sledrun_wiki(sledrun_json, map_json)
195 summary = 'Rodelbahnbeschreibung nach Konvertierung nach und von JSON.'
196 self.put_current(text, summary=summary)
199 def main(*args: str) -> None:
200 local_args = pywikibot.handle_args(args)
201 gen_factory = pagegenerators.GeneratorFactory()
202 gen_factory.handle_args(local_args)
203 gen = gen_factory.getCombinedGenerator(preload=True)
205 bot = SledrunWikiTextToJsonBot(generator=gen)
208 pywikibot.bot.suggest_help(missing_generator=True)
211 if __name__ == '__main__':