]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blobdiff - wrpylib/mwapi.py
Use "no ssl" for simple http: requests.
[philipp/winterrodeln/wrpylib.git] / wrpylib / mwapi.py
index 2fc2c01453718d5db0d7a3e4ab86a51e1baa07cd..5ec5df372952b49dbdfd6f322bc2b103de6fe0f9 100644 (file)
@@ -1,17 +1,36 @@
-#!/usr/bin/python2.6
-# -*- coding: iso-8859-15 -*-
-# $Id$
-# $HeadURL$
-"""This module contains general functions for using the MediaWiki API. There is one very
-good Python wrapper implementation of the MediaWiki API:
-* wikitools http://code.google.com/p/python-wikitools/
+"""Helper functions for using the MediaWiki API."""
+import configparser
+import json
+from functools import lru_cache
+from typing import List
 
-Therefore this module doesn't need to have much content.
-"""
+from pywikiapi import Site
 
-def to_title(value):
-    """Line 2237 of includes/Title.php says: $this->mTextform = str_replace( '_', ' ', $dbkey );
-    No check for None because a missing title is an error."""
-    return value.replace(u'_', u' ')
 
+def page_json(page: dict) -> dict:
+    slot = page['revisions'][0]['slots']['main']
+    assert slot['contentmodel'] == 'json'
+    return json.loads(slot['content'])
 
+
+class WikiSite(Site):
+    def __init__(self, ini_files: List[str]):
+        config = configparser.ConfigParser()
+        config.read(ini_files)
+
+        api_url = config.get('robot', 'wikiurl')
+        api_user = config.get('robot', 'botpassword_bot')
+        api_password = config.get('robot', 'botpassword_password')
+        super().__init__(api_url)
+        if api_url.startswith('http:'):
+            self.no_ssl = True
+        self.login(api_user, api_password, True)
+
+    def query_page(self, title: str) -> dict:
+        rv_props = ['ids', 'contentmodel', 'content']
+        return next(self.query_pages(titles=title, prop='revisions', rvprop=rv_props, rvslots='main'))
+
+    @lru_cache
+    def sledrun_schema(self) -> dict:
+        schema = self.query_page('Winterrodeln:Datenschema/Rodelbahn/V1.json')
+        return page_json(schema)