]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blob - scripts/update_links_almenrausch.py
VAO is missing important streets in Switzerland.
[philipp/winterrodeln/wrpylib.git] / scripts / update_links_almenrausch.py
1 #!/usr/bin/python
2 import argparse
3 import csv
4 import re
5 from itertools import islice
6 from typing import List
7
8 import jsonschema
9
10 from wrpylib.json_tools import order_json_keys, format_json
11 from wrpylib.mwapi import WikiSite, page_json
12
13
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()
18
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:
24         return
25
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)
30
31     site(
32         'edit',
33         pageid=sledrun_json_page['pageid'],
34         text=sledrun_json_str,
35         summary=f'Link von {title} bei almenrausch.at aktualisiert.',
36         minor=1,
37         bot=1,
38         baserevid=sledrun_json_page['revisions'][0]['revid'],
39         nocreate=1,
40         token=site.token(),
41     )
42
43
44 def update_almenrausch(ini_files: List[str], csv_file: str):
45     site = WikiSite(ini_files)
46
47     with open(csv_file, newline='') as fp:
48         csv_reader = csv.reader(fp)
49
50         for row in islice(csv_reader, 1, None):
51             title = row[0]
52             almenrausch_url = row[-1]
53             print(title, almenrausch_url)
54             update_sledrun(site, title, almenrausch_url)
55
56
57 def main():
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)
63
64
65 if __name__ == '__main__':
66     main()