+#!/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.
+
+Edit JSON associated with sledruns.
+
+The following generators and filters are supported:
+
+¶ms;
+"""
+import json
+
+import jsonschema
+import pywikibot
+from jsonschema import validate
+from pywikibot import pagegenerators, Page, warning
+from pywikibot.bot import (
+ AutomaticTWSummaryBot,
+ ConfigParserBot,
+ ExistingPageBot,
+ NoRedirectPageBot,
+ SingleSiteBot,
+)
+
+from wrpylib.json_tools import order_json_keys
+from wrpylib.wrmwmarkup import create_sledrun_wiki
+
+
+docuReplacements = {'¶ms;': pagegenerators.parameterHelp}
+
+
+class SledrunFromJsonBot(
+ SingleSiteBot,
+ ConfigParserBot,
+ ExistingPageBot,
+ NoRedirectPageBot,
+ AutomaticTWSummaryBot,
+):
+ def setup(self) -> None:
+ schema = Page(self.site, 'Winterrodeln:Datenschema/Rodelbahn/V1.json')
+ assert schema.content_model == 'json'
+ self.sledrun_schema = json.loads(schema.text)
+
+ def treat_page(self) -> None:
+ """Load the given page, do some changes, and save it."""
+ if self.current_page.content_model != 'wikitext':
+ warning(f"The content model of {self.current_page.title()} is {self.current_page.content_model} "
+ f"instead of wikitext")
+ return
+
+ sledrun_json_page = Page(self.site, self.current_page.title() + '/Rodelbahn.json')
+ if not sledrun_json_page.exists():
+ warning(f"{sledrun_json_page} does not exist.")
+ return
+
+ if sledrun_json_page.content_model != 'json':
+ warning(f"The content model of {sledrun_json_page} is {sledrun_json_page.content_model} "
+ f"instead of json")
+ return
+
+ sledrun_json = json.loads(sledrun_json_page.text)
+ sledrun_json_orig = json.loads(sledrun_json_page.text)
+ sledrun_json_orig_text = json.dumps(sledrun_json_orig, ensure_ascii=False, indent=4)
+
+ sled_rental_description = sledrun_json.get('sled_rental_description')
+ if sled_rental_description is not None and len(sled_rental_description) < 8 and (
+ "Nein" in sled_rental_description or "nein" in sled_rental_description
+ ):
+ del sledrun_json['sled_rental_description']
+ sledrun_json['sled_rental_direct'] = False
+
+ nightlight_description = sledrun_json.get('nightlight_description')
+ if nightlight_description is not None and len(nightlight_description) < 8 and (
+ "Nein" in nightlight_description or "nein" in nightlight_description
+ ):
+ del sledrun_json['nightlight_description']
+ sledrun_json['nightlight_possible'] = "Nein"
+
+ jsonschema.validate(instance=sledrun_json, schema=self.sledrun_schema)
+ sledrun_json_ordered = order_json_keys(sledrun_json, self.sledrun_schema)
+ assert sledrun_json_ordered == sledrun_json
+ if sledrun_json == sledrun_json_orig:
+ return
+ sledrun_json_text = json.dumps(sledrun_json_ordered, ensure_ascii=False, indent=4)
+ summary = 'JSON Daten aktualisiert.'
+ self.userPut(sledrun_json_page, sledrun_json_orig_text, sledrun_json_text, summary=summary, contentmodel='json')
+
+
+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 = SledrunFromJsonBot(generator=gen)
+ bot.run()
+ else:
+ pywikibot.bot.suggest_help(missing_generator=True)
+
+
+if __name__ == '__main__':
+ main()