]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Start to work on wikitext to JSON conversion.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 28 Nov 2021 10:45:26 +0000 (11:45 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Sun, 28 Nov 2021 10:45:26 +0000 (11:45 +0100)
bots/sledrun_wikitext_to_json.py [new file with mode: 0644]

diff --git a/bots/sledrun_wikitext_to_json.py b/bots/sledrun_wikitext_to_json.py
new file mode 100644 (file)
index 0000000..29c860b
--- /dev/null
@@ -0,0 +1,81 @@
+#!/usr/bin/python
+"""
+User script for pywikibot (https://gerrit.wikimedia.org/r/pywikibot/core.git), tested with version 6.6.1.
+Put it in directory scripts/userscripts.
+
+Create a sledrun JSON page from a sledrun wikitext page (including map).
+
+The following generators and filters are supported:
+
+&params;
+"""
+import json
+
+import pywikibot
+from pywikibot import pagegenerators, Page
+from pywikibot.bot import (
+    AutomaticTWSummaryBot,
+    ConfigParserBot,
+    ExistingPageBot,
+    NoRedirectPageBot,
+    SingleSiteBot,
+)
+from pywikibot.logging import warning
+
+from wrpylib.wrmwmarkup import create_sledrun_wiki
+
+
+docuReplacements = {'&params;': pagegenerators.parameterHelp}
+
+
+class SledrunWikiTextToJsonBot(
+    SingleSiteBot,
+    ConfigParserBot,
+    ExistingPageBot,
+    NoRedirectPageBot,
+    AutomaticTWSummaryBot,
+):
+    def treat_page(self) -> None:
+        """Load the given page, do some changes, and save it."""
+        wikitext_content_model = 'wikitext'
+        if self.current_page.content_model != wikitext_content_model:
+            warning(f"The content model of {self.current_page.title()} is {self.current_page.content_model} "
+                    f"instead of {wikitext_content_model}.")
+            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
+
+        sledrun_json = {
+            "name": self.current_page.title(),
+            "description": "Holadrio!",
+        }
+
+        map_json = None
+
+        text = create_sledrun_wiki(sledrun_json, map_json)
+        summary = 'Rodelbahnbeschreibung nach Konvertierung nach und von JSON.'
+        self.put_current(text, summary=summary)
+
+
+def main(*args: str) -> None:
+    local_args = pywikibot.handle_args(args)
+    gen_factory = pagegenerators.GeneratorFactory()
+    gen_factory.handle_args(local_args)
+    gen = gen_factory.getCombinedGenerator(preload=True)
+    if gen:
+        bot = SledrunWikiTextToJsonBot(generator=gen)
+        bot.run()
+    else:
+        pywikibot.bot.suggest_help(missing_generator=True)
+
+
+if __name__ == '__main__':
+    main()