#!/usr/bin/python import argparse import csv import re from itertools import islice from typing import List import jsonschema from wrpylib.json_tools import order_json_keys, format_json from wrpylib.mwapi import WikiSite, page_json def update_sledrun(site: WikiSite, title: str, almenrausch_url: str): sledrun_json_page = site.query_page(f'{title}/Rodelbahn.json') sledrun_json = page_json(sledrun_json_page) sledrun_json_orig = sledrun_json.copy() see_also = sledrun_json.pop('see_also', []) see_also = [s for s in see_also if not re.match(r'https?://(www\.)?almenrausch\.at/', s['url'])] see_also.append({'url': almenrausch_url, 'text': f'{title} bei almenrausch.at'}) sledrun_json['see_also'] = see_also if sledrun_json == sledrun_json_orig: return jsonschema.validate(instance=sledrun_json, schema=site.sledrun_schema()) sledrun_json_ordered = order_json_keys(sledrun_json, site.sledrun_schema()) assert sledrun_json_ordered == sledrun_json sledrun_json_str = format_json(sledrun_json_ordered) site( 'edit', pageid=sledrun_json_page['pageid'], text=sledrun_json_str, summary=f'Link von {title} bei almenrausch.at aktualisiert.', minor=1, bot=1, baserevid=sledrun_json_page['revisions'][0]['revid'], nocreate=1, token=site.token(), ) def update_almenrausch(ini_files: List[str], csv_file: str): site = WikiSite(ini_files) with open(csv_file, newline='') as fp: csv_reader = csv.reader(fp) for row in islice(csv_reader, 1, None): title = row[0] almenrausch_url = row[-1] print(title, almenrausch_url) update_sledrun(site, title, almenrausch_url) def main(): parser = argparse.ArgumentParser(description='Update almenrausch.at Link given a csv file.') parser.add_argument('inifile', nargs='+', help='inifile.ini, see: https://www.winterrodeln.org/trac/wiki/ConfigIni') parser.add_argument('csvfile', help='csv file containing sledrun links') args = parser.parse_args() update_almenrausch(args.inifile, args.csvfile) if __name__ == '__main__': main()