]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blob - bots/json_edit.py
Create bot to edit JSON data.
[philipp/winterrodeln/wrpylib.git] / bots / 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 pywikibot
15 from pywikibot import pagegenerators, Page
16 from pywikibot.bot import (
17     AutomaticTWSummaryBot,
18     ConfigParserBot,
19     ExistingPageBot,
20     NoRedirectPageBot,
21     SingleSiteBot,
22 )
23
24 from wrpylib.wrmwmarkup import create_sledrun_wiki
25
26
27 docuReplacements = {'&params;': pagegenerators.parameterHelp}
28
29
30 class SledrunFromJsonBot(
31     SingleSiteBot,
32     ConfigParserBot,
33     ExistingPageBot,
34     NoRedirectPageBot,
35     AutomaticTWSummaryBot,
36 ):
37     def treat_page(self) -> None:
38         """Load the given page, do some changes, and save it."""
39         if self.current_page.content_model != 'json':
40             return
41         content_json = json.loads(self.current_page.text)
42         processed_json = content_json
43         text = json.dumps(processed_json, ensure_ascii=False, indent=4)
44         summary = 'JSON Daten aktualisiert.'
45         self.put_current(text, summary=summary)
46
47
48 def main(*args: str) -> None:
49     local_args = pywikibot.handle_args(args)
50     gen_factory = pagegenerators.GeneratorFactory()
51     gen_factory.handle_args(local_args)
52     gen = gen_factory.getCombinedGenerator(preload=True)
53     if gen:
54         bot = SledrunFromJsonBot(generator=gen)
55         bot.run()
56     else:
57         pywikibot.bot.suggest_help(missing_generator=True)
58
59
60 if __name__ == '__main__':
61     main()