1 """Helper functions for using the MediaWiki API."""
4 from functools import lru_cache
5 from typing import List
7 from pywikiapi import Site
10 def page_json(page: dict) -> dict:
11 slot = page['revisions'][0]['slots']['main']
12 assert slot['contentmodel'] == 'json'
13 return json.loads(slot['content'])
17 def __init__(self, ini_files: List[str]):
18 config = configparser.ConfigParser()
19 config.read(ini_files)
21 api_url = config.get('robot', 'wikiurl')
22 api_user = config.get('robot', 'botpassword_bot')
23 api_password = config.get('robot', 'botpassword_password')
24 super().__init__(api_url)
25 if api_url.startswith('http:'):
27 self.login(api_user, api_password, True)
29 def query_page(self, title: str) -> dict:
30 rv_props = ['ids', 'contentmodel', 'content']
31 return next(self.query_pages(titles=title, prop='revisions', rvprop=rv_props, rvslots='main'))
34 def sledrun_schema(self) -> dict:
35 schema = self.query_page('Winterrodeln:Datenschema/Rodelbahn/V1.json')
36 return page_json(schema)