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 Edit JSON associated with sledruns.
8 The following generators and filters are supported:
16 from jsonschema import validate
17 from pywikibot import pagegenerators, Page, warning
18 from pywikibot.bot import (
19 AutomaticTWSummaryBot,
26 from wrpylib.json_tools import order_json_keys
27 from wrpylib.wrmwmarkup import create_sledrun_wiki
30 docuReplacements = {'¶ms;': pagegenerators.parameterHelp}
33 class SledrunFromJsonBot(
38 AutomaticTWSummaryBot,
40 def setup(self) -> None:
41 schema = Page(self.site, 'Winterrodeln:Datenschema/Rodelbahn/V1.json')
42 assert schema.content_model == 'json'
43 self.sledrun_schema = json.loads(schema.text)
45 def treat_page(self) -> None:
46 """Load the given page, do some changes, and save it."""
47 if self.current_page.content_model != 'wikitext':
48 warning(f"The content model of {self.current_page.title()} is {self.current_page.content_model} "
49 f"instead of wikitext")
52 sledrun_json_page = Page(self.site, self.current_page.title() + '/Rodelbahn.json')
53 if not sledrun_json_page.exists():
54 warning(f"{sledrun_json_page} does not exist.")
57 if sledrun_json_page.content_model != 'json':
58 warning(f"The content model of {sledrun_json_page} is {sledrun_json_page.content_model} "
62 sledrun_json = json.loads(sledrun_json_page.text)
63 sledrun_json_orig = json.loads(sledrun_json_page.text)
64 sledrun_json_orig_text = json.dumps(sledrun_json_orig, ensure_ascii=False, indent=4)
66 sled_rental_description = sledrun_json.get('sled_rental_description')
67 if sled_rental_description is not None and len(sled_rental_description) < 8 and (
68 "Nein" in sled_rental_description or "nein" in sled_rental_description
70 del sledrun_json['sled_rental_description']
71 sledrun_json['sled_rental_direct'] = False
73 nightlight_description = sledrun_json.get('nightlight_description')
74 if nightlight_description is not None and len(nightlight_description) < 8 and (
75 "Nein" in nightlight_description or "nein" in nightlight_description
77 del sledrun_json['nightlight_description']
78 sledrun_json['nightlight_possible'] = "Nein"
80 jsonschema.validate(instance=sledrun_json, schema=self.sledrun_schema)
81 sledrun_json_ordered = order_json_keys(sledrun_json, self.sledrun_schema)
82 assert sledrun_json_ordered == sledrun_json
83 if sledrun_json == sledrun_json_orig:
85 sledrun_json_text = json.dumps(sledrun_json_ordered, ensure_ascii=False, indent=4)
86 summary = 'JSON Daten aktualisiert.'
87 self.userPut(sledrun_json_page, sledrun_json_orig_text, sledrun_json_text, summary=summary, contentmodel='json')
90 def main(*args: str) -> None:
91 local_args = pywikibot.handle_args(args)
92 gen_factory = pagegenerators.GeneratorFactory()
93 gen_factory.handle_args(local_args)
94 gen = gen_factory.getCombinedGenerator(preload=True)
96 bot = SledrunFromJsonBot(generator=gen)
99 pywikibot.bot.suggest_help(missing_generator=True)
102 if __name__ == '__main__':