7 from itertools import islice
8 from typing import List
11 from pywikiapi import Site # https://github.com/nyurik/pywikiapi
13 from wrpylib.json_tools import order_json_keys
16 def query_page(site: Site, title: str) -> dict:
17 rv_props = ['ids', 'contentmodel', 'content']
18 return next(site.query_pages(titles=title, prop='revisions', rvprop=rv_props, rvslots='main'))
21 def page_json(page: dict) -> dict:
22 slot = page['revisions'][0]['slots']['main']
23 assert slot['contentmodel'] == 'json'
24 return json.loads(slot['content'])
27 def update_sledrun(site: Site, schema: dict, title: str, almenrausch_url: str):
28 sledrun_json_page = query_page(site, f'{title}/Rodelbahn.json')
29 sledrun_json = page_json(sledrun_json_page)
30 sledrun_json_orig = sledrun_json.copy()
32 see_also = sledrun_json.pop('see_also', [])
33 see_also = [s for s in see_also if not re.match(r'https?://(www\.)?almenrausch\.at/', s['url'])]
34 see_also.append({'url': almenrausch_url, 'text': f'{title} bei almenrausch.at'})
35 sledrun_json['see_also'] = see_also
36 if sledrun_json == sledrun_json_orig:
39 jsonschema.validate(instance=sledrun_json, schema=schema)
40 sledrun_json_ordered = order_json_keys(sledrun_json, schema)
41 assert sledrun_json_ordered == sledrun_json
42 sledrun_json_str = json.dumps(sledrun_json_ordered, ensure_ascii=False, indent=4)
46 pageid=sledrun_json_page['pageid'],
47 text=sledrun_json_str,
48 summary=f'Link von {title} bei almenrausch.at aktualisiert.',
51 baserevid=sledrun_json_page['revisions'][0]['revid'],
57 def update_almenrausch(inifile: List[str], csvfile: str):
58 config = configparser.ConfigParser()
61 api_url = config.get('robot', 'wikiurl')
62 api_user = config.get('robot', 'botpassword_bot')
63 api_password = config.get('robot', 'botpassword_password')
65 site.login(api_user, api_password, True)
67 schema_page = query_page(site, 'Winterrodeln:Datenschema/Rodelbahn/V1.json')
68 schema = page_json(schema_page)
70 with open(csvfile, newline='') as fp:
71 csv_reader = csv.reader(fp)
73 for row in islice(csv_reader, 1, None):
75 almenrausch_url = row[-1]
76 print(title, almenrausch_url)
77 update_sledrun(site, schema, title, almenrausch_url)
81 parser = argparse.ArgumentParser(description='Update almenrausch.at Link given a csv file.')
82 parser.add_argument('inifile', nargs='+', help='inifile.ini, see: https://www.winterrodeln.org/trac/wiki/ConfigIni')
83 parser.add_argument('csvfile', help='csv file containing sledrun links')
84 args = parser.parse_args()
85 update_almenrausch(args.inifile, args.csvfile)
88 if __name__ == '__main__':