]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blob - bots/sledrun_from_json.py
Fix handling of empty coordinates.
[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 from pywikibot.logging import warning
25
26 from wrpylib.wrmwmarkup import create_sledrun_wiki
27
28
29 docuReplacements = {'&params;': pagegenerators.parameterHelp}
30
31
32 class SledrunFromJsonBot(
33     SingleSiteBot,
34     ConfigParserBot,
35     ExistingPageBot,
36     NoRedirectPageBot,
37     AutomaticTWSummaryBot,
38 ):
39     def treat_page(self) -> None:
40         """Load the given page, do some changes, and save it."""
41         sledrun_json_page = Page(self.site, self.current_page.title() + '/Rodelbahn.json')
42         if not sledrun_json_page.exists():
43             warning(f"{sledrun_json_page.title()} does not exist. Skipping.")
44             return
45         if sledrun_json_page.content_model != 'json':
46             warning(f"Content model of {sledrun_json_page.title()} is not 'json'.")
47             return
48         sledrun_json = json.loads(sledrun_json_page.text)
49         map_json_page = Page(self.site, self.current_page.title() + '/Landkarte.json')
50         if map_json_page.exists():
51             map_json = json.loads(map_json_page.text)
52         else:
53             map_json = None
54         impressions = None
55         sledrun_impressions_page = Page(self.site, self.current_page.title() + '/Impressionen')
56         if sledrun_impressions_page.exists():
57             impressions = sledrun_impressions_page.title()
58         text = create_sledrun_wiki(sledrun_json, map_json, impressions)
59         summary = 'Rodelbahnbeschreibung von aus JSON Daten aktualisiert.'
60         self.put_current(text, summary=summary)
61
62
63 def main(*args: str) -> None:
64     local_args = pywikibot.handle_args(args)
65     gen_factory = pagegenerators.GeneratorFactory()
66     gen_factory.handle_args(local_args)
67     gen = gen_factory.getCombinedGenerator(preload=True)
68     if gen:
69         bot = SledrunFromJsonBot(generator=gen)
70         bot.run()
71     else:
72         pywikibot.bot.suggest_help(missing_generator=True)
73
74
75 if __name__ == '__main__':
76     main()