]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blob - scripts/update_links_almenrausch.py
Make use of new function format_json().
[philipp/winterrodeln/wrpylib.git] / scripts / update_links_almenrausch.py
1 #!/usr/bin/python
2 import argparse
3 import configparser
4 import csv
5 import json
6 import re
7 from itertools import islice
8 from typing import List
9
10 import jsonschema
11 from pywikiapi import Site  # https://github.com/nyurik/pywikiapi
12
13 from wrpylib.json_tools import order_json_keys, format_json
14
15
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'))
19
20
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'])
25
26
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()
31
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:
37         return
38
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 = format_json(sledrun_json_ordered)
43
44     site(
45         'edit',
46         pageid=sledrun_json_page['pageid'],
47         text=sledrun_json_str,
48         summary=f'Link von {title} bei almenrausch.at aktualisiert.',
49         minor=1,
50         bot=1,
51         baserevid=sledrun_json_page['revisions'][0]['revid'],
52         nocreate=1,
53         token=site.token(),
54     )
55
56
57 def update_almenrausch(inifile: List[str], csvfile: str):
58     config = configparser.ConfigParser()
59     config.read(inifile)
60
61     api_url = config.get('robot', 'wikiurl')
62     api_user = config.get('robot', 'botpassword_bot')
63     api_password = config.get('robot', 'botpassword_password')
64     site = Site(api_url)
65     site.login(api_user, api_password, True)
66
67     schema_page = query_page(site, 'Winterrodeln:Datenschema/Rodelbahn/V1.json')
68     schema = page_json(schema_page)
69
70     with open(csvfile, newline='') as fp:
71         csv_reader = csv.reader(fp)
72
73         for row in islice(csv_reader, 1, None):
74             title = row[0]
75             almenrausch_url = row[-1]
76             print(title, almenrausch_url)
77             update_sledrun(site, schema, title, almenrausch_url)
78
79
80 def main():
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)
86
87
88 if __name__ == '__main__':
89     main()