]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blob - bots/sledrun_json_edit.py
Add script to remove unnecessary night light and sledrun rental descriptions.
[philipp/winterrodeln/wrpylib.git] / bots / sledrun_json_edit.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 docuReplacements = {'&params;': pagegenerators.parameterHelp}
30
31
32 class SledrunFromJsonBot(
33     SingleSiteBot,
34     ConfigParserBot,
35     ExistingPageBot,
36     NoRedirectPageBot,
37     AutomaticTWSummaryBot,
38 ):
39     def setup(self) -> None:
40         schema = Page(self.site, 'Winterrodeln:Datenschema/Rodelbahn/V1.json')
41         assert schema.content_model == 'json'
42         self.sledrun_schema = json.loads(schema.text)
43
44     def treat_page(self) -> None:
45         """Load the given page, do some changes, and save it."""
46         if self.current_page.content_model != 'wikitext':
47             warning(f"The content model of {self.current_page.title()} is {self.current_page.content_model} "
48                     f"instead of wikitext")
49             return
50
51         sledrun_json_page = Page(self.site, self.current_page.title() + '/Rodelbahn.json')
52         if not sledrun_json_page.exists():
53             warning(f"{sledrun_json_page} does not exist.")
54             return
55
56         if sledrun_json_page.content_model != 'json':
57             warning(f"The content model of {sledrun_json_page} is {sledrun_json_page.content_model} "
58                     f"instead of json")
59             return
60
61         sledrun_json = json.loads(sledrun_json_page.text)
62         sledrun_json_orig = json.loads(sledrun_json_page.text)
63         sledrun_json_orig_text = json.dumps(sledrun_json_orig, ensure_ascii=False, indent=4)
64
65         # *here*, sledrun_json can be processed
66
67         jsonschema.validate(instance=sledrun_json, schema=self.sledrun_schema)
68         sledrun_json_ordered = order_json_keys(sledrun_json, self.sledrun_schema)
69         assert sledrun_json_ordered == sledrun_json
70         if sledrun_json == sledrun_json_orig:
71             return
72         sledrun_json_text = json.dumps(sledrun_json_ordered, ensure_ascii=False, indent=4)
73         summary = 'JSON Daten aktualisiert.'
74         self.userPut(sledrun_json_page, sledrun_json_orig_text, sledrun_json_text, summary=summary, contentmodel='json')
75
76
77 def main(*args: str) -> None:
78     local_args = pywikibot.handle_args(args)
79     gen_factory = pagegenerators.GeneratorFactory()
80     gen_factory.handle_args(local_args)
81     gen = gen_factory.getCombinedGenerator(preload=True)
82     if gen:
83         bot = SledrunFromJsonBot(generator=gen)
84         bot.run()
85     else:
86         pywikibot.bot.suggest_help(missing_generator=True)
87
88
89 if __name__ == '__main__':
90     main()