From: Philipp Spitzer Date: Tue, 26 Oct 2021 14:55:15 +0000 (+0200) Subject: Create bot to edit JSON data. X-Git-Url: https://git.toastfreeware.priv.at/philipp/winterrodeln/wrpylib.git/commitdiff_plain/a94a48893108d0f589e2c95c75cfbbe38bdf4172 Create bot to edit JSON data. --- diff --git a/bots/json_edit.py b/bots/json_edit.py new file mode 100644 index 0000000..bcd0eae --- /dev/null +++ b/bots/json_edit.py @@ -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: + +¶ms; +""" +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 = {'¶ms;': 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()