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 pywikibot import pagegenerators, Page
17 from pywikibot.bot import (
18 AutomaticTWSummaryBot,
24 from pywikibot.logging import warning
25 from pywikibot.site._namespace import BuiltinNamespace
27 from wrpylib.wrmwmarkup import create_sledrun_wiki, lonlat_to_json, lonlat_ele_to_json, parse_wrmap
28 from wrpylib.wrvalidators import rodelbahnbox_from_template, tristate_german_to_str, difficulty_german_to_str, \
29 avalanches_german_to_str, public_transport_german_to_str, opt_str_opt_comment_enum_to_str
31 from pywikibot.site import Namespace
33 docuReplacements = {'¶ms;': pagegenerators.parameterHelp}
36 class SledrunWikiTextToJsonBot(
41 AutomaticTWSummaryBot,
43 def treat_page(self) -> None:
44 """Load the given page, do some changes, and save it."""
45 wikitext_content_model = 'wikitext'
46 if self.current_page.content_model != wikitext_content_model:
47 warning(f"The content model of {self.current_page.title()} is {self.current_page.content_model} "
48 f"instead of {wikitext_content_model}.")
51 wikicode = mwparserfromhell.parse(self.current_page.text)
52 wikilink_list = wikicode.filter_wikilinks()
53 category_sledrun = 'Kategorie:Rodelbahn'
54 if sum(1 for c in wikilink_list if c.title == category_sledrun) == 0:
55 warning(f'The page {self.current_page.title()} does not have category {category_sledrun}.')
58 sledrun_json_page = Page(self.site, self.current_page.title() + '/Rodelbahn.json')
59 if sledrun_json_page.exists():
60 warning(f"{sledrun_json_page.title()} already exists, skipping {self.current_page.title()}.")
63 map_json_page = Page(self.site, self.current_page.title() + '/Landkarte.json')
64 if map_json_page.exists():
65 warning(f"{map_json_page.title()} already exists, skipping {self.current_page.title()}.")
69 v = wikicode.filter_tags(matches='wrmap')
71 map_json = parse_wrmap(str(v[0]))
74 "name": self.current_page.title(),
76 "entry_under_construction": sum(1 for c in wikilink_list if c.text == 'Kategorie:In Arbeit') > 0,
77 "description": "Holadrio!",
81 rbb_list = wikicode.filter_templates(recursive=False, matches=lambda t: t.name.strip() == 'Rodelbahnbox')
82 if len(rbb_list) == 1:
83 rbb = rodelbahnbox_from_template(rbb_list[0])
86 image_page = Page(self.site, v, ns=BuiltinNamespace.FILE)
87 if image_page.exists():
88 warning(f"{image_page.title()} does not exist.")
89 sledrun_json['image'] = v
93 sledrun_json['length'] = v
95 v = rbb['Schwierigkeit']
97 sledrun_json['difficulty'] = difficulty_german_to_str(v)
101 sledrun_json['avalanches'] = avalanches_german_to_str(v)
103 v, w = rbb['Betreiber']
105 sledrun_json['has_operator'] = v
107 sledrun_json['operator'] = w
109 v = rbb['Aufstieg möglich']
111 sledrun_json['walkup_possible'] = v
113 v, w = rbb['Aufstieg getrennt']
115 sledrun_json['walkup_separate'] = tristate_german_to_str(v)
117 sledrun_json['walkup_comment'] = w # TODO
121 sledrun_json['walkup_time'] = v
123 v, w = rbb['Beleuchtungsanlage']
125 sledrun_json['nightlight_possible'] = tristate_german_to_str(v)
127 sledrun_json['nightlight_description'] = w
129 v = rbb['Rodelverleih']
131 sledrun_json['sled_rental_direct'] = v != []
132 sledrun_json['sled_rental_description'] = opt_str_opt_comment_enum_to_str(v)
134 v = rbb['In Übersichtskarte']
136 sledrun_json['show_in_overview'] = v
140 sledrun_json['forum_id'] = v
144 sledrun_json['position'] = lonlat_to_json(v)
146 v = lonlat_ele_to_json(rbb['Position oben'], rbb['Höhe oben'])
148 sledrun_json['top'] = v
150 v = lonlat_ele_to_json(rbb['Position unten'], rbb['Höhe unten'])
152 sledrun_json['bottom'] = v
154 v = rbb['Telefonauskunft']
156 sledrun_json['info_phone'] = [{'phone': p, 'name': n} for p, n in v]
158 v = rbb['Öffentliche Anreise']
160 sledrun_json['public_transport'] = public_transport_german_to_str(v)
162 text = create_sledrun_wiki(sledrun_json, map_json)
163 summary = 'Rodelbahnbeschreibung nach Konvertierung nach und von JSON.'
164 self.put_current(text, summary=summary)
167 def main(*args: str) -> None:
168 local_args = pywikibot.handle_args(args)
169 gen_factory = pagegenerators.GeneratorFactory()
170 gen_factory.handle_args(local_args)
171 gen = gen_factory.getCombinedGenerator(preload=True)
173 bot = SledrunWikiTextToJsonBot(generator=gen)
176 pywikibot.bot.suggest_help(missing_generator=True)
179 if __name__ == '__main__':