]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blob - bots/update_sledrun_json_from_wikitext_gütesiegel.py
Temporary helper scripts that won't be needed for long...
[philipp/winterrodeln/wrpylib.git] / bots / update_sledrun_json_from_wikitext_gütesiegel.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 Update a sledrun JSON page from a detail in a sledrun wikitext page.
7
8 The following generators and filters are supported:
9
10 &params;
11 """
12 import io
13 import json
14 import re
15 from itertools import takewhile, dropwhile
16 from typing import Optional
17
18 import jsonschema
19 import mwparserfromhell
20 from mwparserfromhell.nodes.extras import Parameter
21
22 import pywikibot
23 from mwparserfromhell.nodes import Tag, Text, ExternalLink, Template, Wikilink, Heading
24 from mwparserfromhell.wikicode import Wikicode
25 from pywikibot import pagegenerators, Page
26 from pywikibot.bot import (
27     AutomaticTWSummaryBot,
28     ConfigParserBot,
29     ExistingPageBot,
30     NoRedirectPageBot,
31     SingleSiteBot,
32 )
33 from pywikibot.logging import warning
34 from pywikibot.site._namespace import BuiltinNamespace
35 from wrpylib.json_tools import order_json_keys
36
37 from wrpylib.wrmwmarkup import create_sledrun_wiki, lonlat_to_json, lonlat_ele_to_json, parse_wrmap
38 from wrpylib.wrvalidators import rodelbahnbox_from_template, tristate_german_to_str, difficulty_german_to_str, \
39     avalanches_german_to_str, public_transport_german_to_str, opt_lonlat_from_str, \
40     opt_uint_from_str
41 from wrpylib.lib_sledrun_wikitext_to_json import optional_set, get_sledrun_description
42
43 docuReplacements = {'&params;': pagegenerators.parameterHelp}
44
45
46 class UpdateSledrunJsonFromWikiText(
47     SingleSiteBot,
48     ConfigParserBot,
49     ExistingPageBot,
50     NoRedirectPageBot,
51     AutomaticTWSummaryBot,
52 ):
53     def setup(self) -> None:
54         schema = Page(self.site, 'Winterrodeln:Datenschema/Rodelbahn/V1.json')
55         assert schema.content_model == 'json'
56         self.sledrun_schema = json.loads(schema.text)
57
58     def treat_page(self) -> None:
59         """Load the given page, do some changes, and save it."""
60         wikitext_content_model = 'wikitext'
61         if self.current_page.content_model != wikitext_content_model:
62             warning(f"The content model of {self.current_page.title()} is {self.current_page.content_model} "
63                     f"instead of {wikitext_content_model}.")
64             return
65
66         wikicode = mwparserfromhell.parse(self.current_page.text)
67
68         sledrun_json_page = Page(self.site, self.current_page.title() + '/Rodelbahn.json')
69         if not sledrun_json_page.exists():
70             return
71         sledrun_json = json.loads(sledrun_json_page.text)
72         sledrun_json_orig_text = json.dumps(sledrun_json, ensure_ascii=False, indent=4)
73
74         def _tiroler_naturrodelbahn_guetesiegel(wikicode) -> Optional[dict]:
75             for gst in wikicode.filter_templates():
76                 if gst.name.strip() != 'Tiroler Naturrodelbahn Gütesiegel':
77                     continue
78                 gsj = {}
79                 keys = {
80                     'Anlagename': 'name',
81                     'Organisation': 'organization',
82                     'Erstverleihung': 'first_issued',
83                     'Verlängerung': 'valid_from',
84                     'Forum': 'forum_id',
85                     'Thread': 'thread_id',
86                 }
87                 numeric = ['first_issued', 'valid_from', 'forum_id', 'thread_id']
88                 for key, value in keys.items():
89                     if gst.has(key):
90                         v = gst.get(key).value.strip()
91                         if v != '':
92                             if value in numeric:
93                                 v = int(v)
94                             gsj[value] = v
95                 if len(gsj) > 0:
96                     return gsj
97
98         gsj = _tiroler_naturrodelbahn_guetesiegel(wikicode)
99
100         if gsj is None:
101             return
102
103         sledrun_json['tiroler_naturrodelbahn_gütesiegel'] = gsj
104
105         description_code = mwparserfromhell.parse(sledrun_json['description'])
106         for t in description_code.filter_templates():
107             # remove template from text
108             description_code.remove(t)
109             d = str(description_code)
110             d = re.sub(r'\n{2,}', r'\n\n', d)
111             sledrun_json['description'] = d
112
113         jsonschema.validate(instance=sledrun_json, schema=self.sledrun_schema)
114         sledrun_json_ordered = order_json_keys(sledrun_json, self.sledrun_schema)
115         assert sledrun_json_ordered == sledrun_json
116         sledrun_json_text = json.dumps(sledrun_json_ordered, ensure_ascii=False, indent=4)
117         summary = 'Gütesiegel im Rodelbahn JSON aktualisiert vom Wikitext.'
118         self.userPut(sledrun_json_page, sledrun_json_orig_text, sledrun_json_text, summary=summary, contentmodel='json')
119
120
121 def main(*args: str) -> None:
122     local_args = pywikibot.handle_args(args)
123     gen_factory = pagegenerators.GeneratorFactory()
124     gen_factory.handle_args(local_args)
125     gen = gen_factory.getCombinedGenerator(preload=True)
126     if gen:
127         bot = UpdateSledrunJsonFromWikiText(generator=gen)
128         bot.run()
129     else:
130         pywikibot.bot.suggest_help(missing_generator=True)
131
132
133 if __name__ == '__main__':
134     main()