]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blob - bots/sledrun_wikitext_to_json.py
Start to work on wikitext to JSON conversion.
[philipp/winterrodeln/wrpylib.git] / bots / sledrun_wikitext_to_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 Create a sledrun JSON page from a sledrun wikitext page (including map).
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 from pywikibot.logging import warning
24
25 from wrpylib.wrmwmarkup import create_sledrun_wiki
26
27
28 docuReplacements = {'&params;': pagegenerators.parameterHelp}
29
30
31 class SledrunWikiTextToJsonBot(
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         wikitext_content_model = 'wikitext'
41         if self.current_page.content_model != wikitext_content_model:
42             warning(f"The content model of {self.current_page.title()} is {self.current_page.content_model} "
43                     f"instead of {wikitext_content_model}.")
44             return
45
46         sledrun_json_page = Page(self.site, self.current_page.title() + '/Rodelbahn.json')
47         if sledrun_json_page.exists():
48             warning(f"{sledrun_json_page.title()} already exists, skipping {self.current_page.title()}.")
49             return
50
51         map_json_page = Page(self.site, self.current_page.title() + '/Landkarte.json')
52         if map_json_page.exists():
53             warning(f"{map_json_page.title()} already exists, skipping {self.current_page.title()}.")
54             return
55
56         sledrun_json = {
57             "name": self.current_page.title(),
58             "description": "Holadrio!",
59         }
60
61         map_json = None
62
63         text = create_sledrun_wiki(sledrun_json, map_json)
64         summary = 'Rodelbahnbeschreibung nach Konvertierung nach und von JSON.'
65         self.put_current(text, summary=summary)
66
67
68 def main(*args: str) -> None:
69     local_args = pywikibot.handle_args(args)
70     gen_factory = pagegenerators.GeneratorFactory()
71     gen_factory.handle_args(local_args)
72     gen = gen_factory.getCombinedGenerator(preload=True)
73     if gen:
74         bot = SledrunWikiTextToJsonBot(generator=gen)
75         bot.run()
76     else:
77         pywikibot.bot.suggest_help(missing_generator=True)
78
79
80 if __name__ == '__main__':
81     main()