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
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
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 "name": self.current_page.title(),
71 "entry_under_construction": sum(1 for c in wikilink_list if c.text == 'Kategorie:In Arbeit') > 0,
72 "description": "Holadrio!",
77 rbb_list = wikicode.filter_templates(recursive=False, matches=lambda t: t.name.strip() == 'Rodelbahnbox')
78 if len(rbb_list) == 1:
79 rbb = rodelbahnbox_from_template(rbb_list[0])
82 image_page = Page(self.site, v, ns=BuiltinNamespace.FILE)
83 if image_page.exists():
84 warning(f"{image_page.title()} does not exist.")
85 sledrun_json['image'] = v
89 sledrun_json['length'] = v
91 v = rbb['Schwierigkeit']
93 sledrun_json['difficulty'] = difficulty_german_to_str(v)
97 sledrun_json['avalanches'] = avalanches_german_to_str(v)
99 v, w = rbb['Betreiber']
101 sledrun_json['has_operator'] = v
103 sledrun_json['operator'] = w
105 v = rbb['Aufstieg möglich']
107 sledrun_json['walkup_possible'] = v
109 v, w = rbb['Aufstieg getrennt']
111 sledrun_json['walkup_separate'] = tristate_german_to_str(v)
113 sledrun_json['walkup_comment'] = w # TODO
117 sledrun_json['walkup_time'] = v
119 v, w = rbb['Beleuchtungsanlage']
121 sledrun_json['nightlight_possible'] = tristate_german_to_str(v)
123 sledrun_json['nightlight_description'] = w
125 v = rbb['In Übersichtskarte']
127 sledrun_json['show_in_overview'] = v
131 sledrun_json['forum_id'] = v
135 sledrun_json['position'] = lonlat_to_json(v)
137 v = lonlat_ele_to_json(rbb['Position oben'], rbb['Höhe oben'])
139 sledrun_json['top'] = v
141 v = lonlat_ele_to_json(rbb['Position unten'], rbb['Höhe unten'])
143 sledrun_json['bottom'] = v
145 v = rbb['Öffentliche Anreise']
147 sledrun_json['public_transport'] = public_transport_german_to_str(v)
149 text = create_sledrun_wiki(sledrun_json, map_json)
150 summary = 'Rodelbahnbeschreibung nach Konvertierung nach und von JSON.'
151 self.put_current(text, summary=summary)
154 def main(*args: str) -> None:
155 local_args = pywikibot.handle_args(args)
156 gen_factory = pagegenerators.GeneratorFactory()
157 gen_factory.handle_args(local_args)
158 gen = gen_factory.getCombinedGenerator(preload=True)
160 bot = SledrunWikiTextToJsonBot(generator=gen)
163 pywikibot.bot.suggest_help(missing_generator=True)
166 if __name__ == '__main__':