]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Create bot to edit JSON data.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 26 Oct 2021 14:55:15 +0000 (16:55 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 26 Oct 2021 14:55:15 +0000 (16:55 +0200)
bots/json_edit.py [new file with mode: 0644]

diff --git a/bots/json_edit.py b/bots/json_edit.py
new file mode 100644 (file)
index 0000000..bcd0eae
--- /dev/null
@@ -0,0 +1,61 @@
+#!/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.
+
+Edit JSON associated with sledruns.
+
+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 wrpylib.wrmwmarkup import create_sledrun_wiki
+
+
+docuReplacements = {'&params;': pagegenerators.parameterHelp}
+
+
+class SledrunFromJsonBot(
+    SingleSiteBot,
+    ConfigParserBot,
+    ExistingPageBot,
+    NoRedirectPageBot,
+    AutomaticTWSummaryBot,
+):
+    def treat_page(self) -> None:
+        """Load the given page, do some changes, and save it."""
+        if self.current_page.content_model != 'json':
+            return
+        content_json = json.loads(self.current_page.text)
+        processed_json = content_json
+        text = json.dumps(processed_json, ensure_ascii=False, indent=4)
+        summary = 'JSON Daten aktualisiert.'
+        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 = SledrunFromJsonBot(generator=gen)
+        bot.run()
+    else:
+        pywikibot.bot.suggest_help(missing_generator=True)
+
+
+if __name__ == '__main__':
+    main()