]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blob - bots/sledrun_wikitext_to_json.py
686a196c5897de9a14cd39aa0c37654ab2e90c2c
[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 mwparserfromhell
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 from pywikibot.site._namespace import BuiltinNamespace
26
27 from wrpylib.wrmwmarkup import create_sledrun_wiki, lonlat_to_json, lonlat_ele_to_json
28 from wrpylib.wrvalidators import rodelbahnbox_from_template, tristate_german_to_str, difficulty_german_to_str, \
29     avalanches_german_to_str, public_transport_german_to_str
30
31 from pywikibot.site import Namespace
32
33 docuReplacements = {'&params;': pagegenerators.parameterHelp}
34
35
36 class SledrunWikiTextToJsonBot(
37     SingleSiteBot,
38     ConfigParserBot,
39     ExistingPageBot,
40     NoRedirectPageBot,
41     AutomaticTWSummaryBot,
42 ):
43     def treat_page(self) -> None:
44         """Load the given page, do some changes, and save it."""
45         wikitext_content_model = 'wikitext'
46         if self.current_page.content_model != wikitext_content_model:
47             warning(f"The content model of {self.current_page.title()} is {self.current_page.content_model} "
48                     f"instead of {wikitext_content_model}.")
49             return
50
51         wikicode = mwparserfromhell.parse(self.current_page.text)
52         wikilink_list = wikicode.filter_wikilinks()
53         category_sledrun = 'Kategorie:Rodelbahn'
54         if sum(1 for c in wikilink_list if c.title == category_sledrun) == 0:
55             warning(f'The page {self.current_page.title()} does not have category {category_sledrun}.')
56             return
57
58         sledrun_json_page = Page(self.site, self.current_page.title() + '/Rodelbahn.json')
59         if sledrun_json_page.exists():
60             warning(f"{sledrun_json_page.title()} already exists, skipping {self.current_page.title()}.")
61             return
62
63         map_json_page = Page(self.site, self.current_page.title() + '/Landkarte.json')
64         if map_json_page.exists():
65             warning(f"{map_json_page.title()} already exists, skipping {self.current_page.title()}.")
66             return
67
68         sledrun_json = {
69             "name": self.current_page.title(),
70             "aliases": [],
71             "entry_under_construction": sum(1 for c in wikilink_list if c.text == 'Kategorie:In Arbeit') > 0,
72             "description": "Holadrio!",
73         }
74
75         map_json = None
76
77         rbb_list = wikicode.filter_templates(recursive=False, matches=lambda t: t.name.strip() == 'Rodelbahnbox')
78         if len(rbb_list) == 1:
79             rbb = rodelbahnbox_from_template(rbb_list[0])
80             v = rbb['Bild']
81             if v is not None:
82                 image_page = Page(self.site, v, ns=BuiltinNamespace.FILE)
83                 if image_page.exists():
84                     warning(f"{image_page.title()} does not exist.")
85                 sledrun_json['image'] = v
86
87             v = rbb['Länge']
88             if v is not None:
89                 sledrun_json['length'] = v
90
91             v = rbb['Schwierigkeit']
92             if v is not None:
93                 sledrun_json['difficulty'] = difficulty_german_to_str(v)
94
95             v = rbb['Lawinen']
96             if v is not None:
97                 sledrun_json['avalanches'] = avalanches_german_to_str(v)
98
99             v, w = rbb['Betreiber']
100             if v is not None:
101                 sledrun_json['has_operator'] = v
102             if w is not None:
103                 sledrun_json['operator'] = w
104
105             v = rbb['Aufstieg möglich']
106             if v is not None:
107                 sledrun_json['walkup_possible'] = v
108
109             v, w = rbb['Aufstieg getrennt']
110             if v is not None:
111                 sledrun_json['walkup_separate'] = tristate_german_to_str(v)
112             if w is not None:
113                 sledrun_json['walkup_comment'] = w  # TODO
114
115             v = rbb['Gehzeit']
116             if v is not None:
117                 sledrun_json['walkup_time'] = v
118
119             v, w = rbb['Beleuchtungsanlage']
120             if v is not None:
121                 sledrun_json['nightlight_possible'] = tristate_german_to_str(v)
122             if w is not None:
123                 sledrun_json['nightlight_description'] = w
124
125             v = rbb['In Übersichtskarte']
126             if v is not None:
127                 sledrun_json['show_in_overview'] = v
128
129             v = rbb['Forumid']
130             if v is not None:
131                 sledrun_json['forum_id'] = v
132
133             v = rbb['Position']
134             if v is not None:
135                 sledrun_json['position'] = lonlat_to_json(v)
136
137             v = lonlat_ele_to_json(rbb['Position oben'], rbb['Höhe oben'])
138             if v != {}:
139                 sledrun_json['top'] = v
140
141             v = lonlat_ele_to_json(rbb['Position unten'], rbb['Höhe unten'])
142             if v != {}:
143                 sledrun_json['bottom'] = v
144
145             v = rbb['Öffentliche Anreise']
146             if v is not None:
147                 sledrun_json['public_transport'] = public_transport_german_to_str(v)
148
149         text = create_sledrun_wiki(sledrun_json, map_json)
150         summary = 'Rodelbahnbeschreibung nach Konvertierung nach und von JSON.'
151         self.put_current(text, summary=summary)
152
153
154 def main(*args: str) -> None:
155     local_args = pywikibot.handle_args(args)
156     gen_factory = pagegenerators.GeneratorFactory()
157     gen_factory.handle_args(local_args)
158     gen = gen_factory.getCombinedGenerator(preload=True)
159     if gen:
160         bot = SledrunWikiTextToJsonBot(generator=gen)
161         bot.run()
162     else:
163         pywikibot.bot.suggest_help(missing_generator=True)
164
165
166 if __name__ == '__main__':
167     main()