]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Add functionality to actually save map and sledrun JSON results.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 15 Mar 2022 21:23:50 +0000 (22:23 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 15 Mar 2022 21:23:50 +0000 (22:23 +0100)
bots/sledrun_wikitext_to_json.py

index feb2f285bb1841351526542810882dfe64196a4f..eec3329f468836d9e82042697d8e11e19d03b37a 100644 (file)
@@ -10,10 +10,12 @@ The following generators and filters are supported:
 &params;
 """
 import io
+import json
 import re
 from itertools import takewhile, dropwhile
 from typing import Optional
 
+import jsonschema
 import mwparserfromhell
 from mwparserfromhell.nodes.extras import Parameter
 
@@ -30,6 +32,7 @@ from pywikibot.bot import (
 )
 from pywikibot.logging import warning
 from pywikibot.site._namespace import BuiltinNamespace
+from wrpylib.json_tools import order_json_keys
 
 from wrpylib.wrmwmarkup import create_sledrun_wiki, lonlat_to_json, lonlat_ele_to_json, parse_wrmap
 from wrpylib.wrvalidators import rodelbahnbox_from_template, tristate_german_to_str, difficulty_german_to_str, \
@@ -71,6 +74,11 @@ class SledrunWikiTextToJsonBot(
     NoRedirectPageBot,
     AutomaticTWSummaryBot,
 ):
+    def setup(self) -> None:
+        schema = Page(self.site, 'Winterrodeln:Datenschema/Rodelbahn/V1.json')
+        assert schema.content_model == 'json'
+        self.sledrun_schema = json.loads(schema.text)
+
     def treat_page(self) -> None:
         """Load the given page, do some changes, and save it."""
         wikitext_content_model = 'wikitext'
@@ -87,14 +95,8 @@ class SledrunWikiTextToJsonBot(
             return
 
         sledrun_json_page = Page(self.site, self.current_page.title() + '/Rodelbahn.json')
-        if sledrun_json_page.exists():
-            warning(f"{sledrun_json_page.title()} already exists, skipping {self.current_page.title()}.")
-            return
 
         map_json_page = Page(self.site, self.current_page.title() + '/Landkarte.json')
-        if map_json_page.exists():
-            warning(f"{map_json_page.title()} already exists, skipping {self.current_page.title()}.")
-            return
 
         map_json = None
         v = wikicode.filter_tags(matches='wrmap')
@@ -433,8 +435,23 @@ class SledrunWikiTextToJsonBot(
             impressions = sledrun_impressions_page.title()
 
         text = create_sledrun_wiki(sledrun_json, map_json, impressions)
-        summary = 'Rodelbahnbeschreibung nach Konvertierung nach und von JSON.'
-        self.put_current(text, summary=summary)
+        pywikibot.output(text)
+        pywikibot.output('\03{lightpurple}---\03{default}')
+        pywikibot.showDiff(self.current_page.text, text)
+
+        jsonschema.validate(instance=sledrun_json, schema=self.sledrun_schema)
+        sledrun_json_ordered = order_json_keys(sledrun_json, self.sledrun_schema)
+        assert sledrun_json_ordered == sledrun_json
+        sledrun_json_text = json.dumps(sledrun_json_ordered, ensure_ascii=False, indent=4)
+        summary = 'Rodelbahnbeschreibung konvertiert von Wikitext nach JSON.'
+        pywikibot.output('\03{lightpurple}---\03{default}')
+        pywikibot.output(sledrun_json_text)
+        pywikibot.output('\03{lightpurple}---\03{default}')
+        self.userPut(sledrun_json_page, sledrun_json_page.text, sledrun_json_text, summary=summary)
+
+        map_json_text = json.dumps(map_json, ensure_ascii=False, indent=4)
+        summary = 'Landkarte konvertiert von Wikitext nach JSON.'
+        self.userPut(map_json_page, map_json_page.text, map_json_text, summary=summary)
 
 
 def main(*args: str) -> None: