]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blob - bots/sledrun_json_edit_nightlight_and_sled_rental.py
Add script to remove unnecessary night light and sledrun rental descriptions.
[philipp/winterrodeln/wrpylib.git] / bots / sledrun_json_edit_nightlight_and_sled_rental.py
1 #!/usr/bin/python
2 """
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.
5
6 Edit JSON associated with sledruns.
7
8 The following generators and filters are supported:
9
10 &params;
11 """
12 import json
13
14 import jsonschema
15 import pywikibot
16 from jsonschema import validate
17 from pywikibot import pagegenerators, Page, warning
18 from pywikibot.bot import (
19     AutomaticTWSummaryBot,
20     ConfigParserBot,
21     ExistingPageBot,
22     NoRedirectPageBot,
23     SingleSiteBot,
24 )
25
26 from wrpylib.json_tools import order_json_keys
27 from wrpylib.wrmwmarkup import create_sledrun_wiki
28
29
30 docuReplacements = {'&params;': pagegenerators.parameterHelp}
31
32
33 class SledrunFromJsonBot(
34     SingleSiteBot,
35     ConfigParserBot,
36     ExistingPageBot,
37     NoRedirectPageBot,
38     AutomaticTWSummaryBot,
39 ):
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)
44
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")
50             return
51
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.")
55             return
56
57         if sledrun_json_page.content_model != 'json':
58             warning(f"The content model of {sledrun_json_page} is {sledrun_json_page.content_model} "
59                     f"instead of json")
60             return
61
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)
65
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
69         ):
70             del sledrun_json['sled_rental_description']
71             sledrun_json['sled_rental_direct'] = False
72
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
76         ):
77             del sledrun_json['nightlight_description']
78             sledrun_json['nightlight_possible'] = "Nein"
79
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:
84             return
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')
88
89
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)
95     if gen:
96         bot = SledrunFromJsonBot(generator=gen)
97         bot.run()
98     else:
99         pywikibot.bot.suggest_help(missing_generator=True)
100
101
102 if __name__ == '__main__':
103     main()