]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blob - bots/sledrun_from_json.py
Move bot to bots directory.
[philipp/winterrodeln/wrpylib.git] / bots / sledrun_from_json.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 Replace a sledrun page with content generated from associated JSON subpages
7 (Rodelbahn and Landkarte).
8
9 The following generators and filters are supported:
10
11 &params;
12 """
13 import json
14
15 import pywikibot
16 from pywikibot import pagegenerators, Page
17 from pywikibot.bot import (
18     AutomaticTWSummaryBot,
19     ConfigParserBot,
20     ExistingPageBot,
21     NoRedirectPageBot,
22     SingleSiteBot,
23 )
24
25 from wrpylib.wrmwmarkup import create_sledrun_wiki
26
27
28 docuReplacements = {'&params;': pagegenerators.parameterHelp}
29
30
31 class SledrunFromJsonBot(
32     SingleSiteBot,
33     ConfigParserBot,
34     ExistingPageBot,
35     NoRedirectPageBot,
36     AutomaticTWSummaryBot,
37 ):
38     def treat_page(self) -> None:
39         """Load the given page, do some changes, and save it."""
40         sledrun_json_page = Page(self.site, self.current_page.title() + '/Rodelbahn.json')
41         if not sledrun_json_page.exists() or sledrun_json_page.content_model != 'json':
42             return
43         sledrun_json = json.loads(sledrun_json_page.text)
44         map_json_page = Page(self.site, self.current_page.title() + '/Landkarte.json')
45         if map_json_page.exists():
46             map_json = json.loads(map_json_page.text)
47         else:
48             map_json = None
49         text = create_sledrun_wiki(sledrun_json, map_json)
50         summary = 'Rodelbahnbeschreibung von aus JSON Daten aktualisiert.'
51         self.put_current(text, summary=summary)
52
53
54 def main(*args: str) -> None:
55     local_args = pywikibot.handle_args(args)
56     gen_factory = pagegenerators.GeneratorFactory()
57     gen_factory.handle_args(local_args)
58     gen = gen_factory.getCombinedGenerator(preload=True)
59     if gen:
60         bot = SledrunFromJsonBot(generator=gen)
61         bot.run()
62     else:
63         pywikibot.bot.suggest_help(missing_generator=True)
64
65
66 if __name__ == '__main__':
67     main()