]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Work on parsing wikicode (currently Rodelbahnbox).
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 28 Nov 2021 22:14:30 +0000 (23:14 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 28 Nov 2021 22:14:30 +0000 (23:14 +0100)
bots/sledrun_wikitext_to_json.py

index 29c860bf9a560e2a0018bab91d10b9c05e48246a..686a196c5897de9a14cd39aa0c37654ab2e90c2c 100644 (file)
@@ -11,6 +11,7 @@ The following generators and filters are supported:
 """
 import json
 
+import mwparserfromhell
 import pywikibot
 from pywikibot import pagegenerators, Page
 from pywikibot.bot import (
@@ -21,9 +22,13 @@ from pywikibot.bot import (
     SingleSiteBot,
 )
 from pywikibot.logging import warning
+from pywikibot.site._namespace import BuiltinNamespace
 
-from wrpylib.wrmwmarkup import create_sledrun_wiki
+from wrpylib.wrmwmarkup import create_sledrun_wiki, lonlat_to_json, lonlat_ele_to_json
+from wrpylib.wrvalidators import rodelbahnbox_from_template, tristate_german_to_str, difficulty_german_to_str, \
+    avalanches_german_to_str, public_transport_german_to_str
 
+from pywikibot.site import Namespace
 
 docuReplacements = {'&params;': pagegenerators.parameterHelp}
 
@@ -43,6 +48,13 @@ class SledrunWikiTextToJsonBot(
                     f"instead of {wikitext_content_model}.")
             return
 
+        wikicode = mwparserfromhell.parse(self.current_page.text)
+        wikilink_list = wikicode.filter_wikilinks()
+        category_sledrun = 'Kategorie:Rodelbahn'
+        if sum(1 for c in wikilink_list if c.title == category_sledrun) == 0:
+            warning(f'The page {self.current_page.title()} does not have category {category_sledrun}.')
+            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()}.")
@@ -55,11 +67,85 @@ class SledrunWikiTextToJsonBot(
 
         sledrun_json = {
             "name": self.current_page.title(),
+            "aliases": [],
+            "entry_under_construction": sum(1 for c in wikilink_list if c.text == 'Kategorie:In Arbeit') > 0,
             "description": "Holadrio!",
         }
 
         map_json = None
 
+        rbb_list = wikicode.filter_templates(recursive=False, matches=lambda t: t.name.strip() == 'Rodelbahnbox')
+        if len(rbb_list) == 1:
+            rbb = rodelbahnbox_from_template(rbb_list[0])
+            v = rbb['Bild']
+            if v is not None:
+                image_page = Page(self.site, v, ns=BuiltinNamespace.FILE)
+                if image_page.exists():
+                    warning(f"{image_page.title()} does not exist.")
+                sledrun_json['image'] = v
+
+            v = rbb['Länge']
+            if v is not None:
+                sledrun_json['length'] = v
+
+            v = rbb['Schwierigkeit']
+            if v is not None:
+                sledrun_json['difficulty'] = difficulty_german_to_str(v)
+
+            v = rbb['Lawinen']
+            if v is not None:
+                sledrun_json['avalanches'] = avalanches_german_to_str(v)
+
+            v, w = rbb['Betreiber']
+            if v is not None:
+                sledrun_json['has_operator'] = v
+            if w is not None:
+                sledrun_json['operator'] = w
+
+            v = rbb['Aufstieg möglich']
+            if v is not None:
+                sledrun_json['walkup_possible'] = v
+
+            v, w = rbb['Aufstieg getrennt']
+            if v is not None:
+                sledrun_json['walkup_separate'] = tristate_german_to_str(v)
+            if w is not None:
+                sledrun_json['walkup_comment'] = w  # TODO
+
+            v = rbb['Gehzeit']
+            if v is not None:
+                sledrun_json['walkup_time'] = v
+
+            v, w = rbb['Beleuchtungsanlage']
+            if v is not None:
+                sledrun_json['nightlight_possible'] = tristate_german_to_str(v)
+            if w is not None:
+                sledrun_json['nightlight_description'] = w
+
+            v = rbb['In Übersichtskarte']
+            if v is not None:
+                sledrun_json['show_in_overview'] = v
+
+            v = rbb['Forumid']
+            if v is not None:
+                sledrun_json['forum_id'] = v
+
+            v = rbb['Position']
+            if v is not None:
+                sledrun_json['position'] = lonlat_to_json(v)
+
+            v = lonlat_ele_to_json(rbb['Position oben'], rbb['Höhe oben'])
+            if v != {}:
+                sledrun_json['top'] = v
+
+            v = lonlat_ele_to_json(rbb['Position unten'], rbb['Höhe unten'])
+            if v != {}:
+                sledrun_json['bottom'] = v
+
+            v = rbb['Öffentliche Anreise']
+            if v is not None:
+                sledrun_json['public_transport'] = public_transport_german_to_str(v)
+
         text = create_sledrun_wiki(sledrun_json, map_json)
         summary = 'Rodelbahnbeschreibung nach Konvertierung nach und von JSON.'
         self.put_current(text, summary=summary)