5 from itertools import islice
6 from typing import List
10 from wrpylib.json_tools import order_json_keys, format_json
11 from wrpylib.mwapi import WikiSite, page_json
14 def update_sledrun(site: WikiSite, title: str, almenrausch_url: str):
15 sledrun_json_page = site.query_page(f'{title}/Rodelbahn.json')
16 sledrun_json = page_json(sledrun_json_page)
17 sledrun_json_orig = sledrun_json.copy()
19 see_also = sledrun_json.pop('see_also', [])
20 see_also = [s for s in see_also if not re.match(r'https?://(www\.)?almenrausch\.at/', s['url'])]
21 see_also.append({'url': almenrausch_url, 'text': f'{title} bei almenrausch.at'})
22 sledrun_json['see_also'] = see_also
23 if sledrun_json == sledrun_json_orig:
26 jsonschema.validate(instance=sledrun_json, schema=site.sledrun_schema())
27 sledrun_json_ordered = order_json_keys(sledrun_json, site.sledrun_schema())
28 assert sledrun_json_ordered == sledrun_json
29 sledrun_json_str = format_json(sledrun_json_ordered)
33 pageid=sledrun_json_page['pageid'],
34 text=sledrun_json_str,
35 summary=f'Link von {title} bei almenrausch.at aktualisiert.',
38 baserevid=sledrun_json_page['revisions'][0]['revid'],
44 def update_almenrausch(ini_files: List[str], csv_file: str):
45 site = WikiSite(ini_files)
47 with open(csv_file, newline='') as fp:
48 csv_reader = csv.reader(fp)
50 for row in islice(csv_reader, 1, None):
52 almenrausch_url = row[-1]
53 print(title, almenrausch_url)
54 update_sledrun(site, title, almenrausch_url)
58 parser = argparse.ArgumentParser(description='Update almenrausch.at Link given a csv file.')
59 parser.add_argument('inifile', nargs='+', help='inifile.ini, see: https://www.winterrodeln.org/trac/wiki/ConfigIni')
60 parser.add_argument('csvfile', help='csv file containing sledrun links')
61 args = parser.parse_args()
62 update_almenrausch(args.inifile, args.csvfile)
65 if __name__ == '__main__':