MediaWiki API for Python 3
Project description
mwng
(MediaWiki API NG) is a MediaWiki API library for Python 3.
MediaWiki API
We will use mw
to call mwng
in this document. import mwng as mw
!
API = mw.API(api_php)
Create an mw.API
instance.
api_php
: URL to theapi.php
, usually at$SITEROOT/w/api.php
.
API.login(username,botpassword)
Login via botpassword.
username
: the username supplied by Special:Botpasswordsbotpassword
: the passowrd supplied by Special:Botpasswords As of MediaWiki 1.27, using the main account for login is not supported. Obtain credentials via Special:BotPasswords or use clientlogin method. We have no plan to introduce clientlogin, so you have to use a botpassword, or do it yourself via rawAPI.get
andAPI.post
requests.
API.edit(page,content,[token,timestamp])
Edit a page, by its ID or title.
page
: either the page ID or the page titlecontent
: either a dictionary or a string, if it is a string, replace the entire page withcontent
.token
: (Optional) thecsrf
token, can be obtained via the first returned value ofAPI.csrf()
, or generated by the library itself.timestamp
: (Optional) a valid timestamp for detecting edit confident, can be obtained via the second returned value ofAPI.csrf()
. Not detecting edit confident by default.
API.token(type)
Get a action token.
type
: The token type, can be one of these:createaccount
,csrf
,deleteglobalaccount
,login
,patrol
,rollback
,setglobalaccountstatus
,userrights
,watch
API.logintoken()
Get a login token (type: login
).
API.csrf()
Get a CSRF token (type: csrf
).
API.get(body)
and API.post(body)
Do RAW API requests, by GET or POST.
body
: the request parameters
Error handling
mw.MWAPIError
The father of all MediaWiki Error Class. The following data are given:
dump
: the RAW JSON data, in case you have to do more things to the datacodes
: List of error codesmessage
: The error description text (if there are only one), or human-readable list of error codes (if there are more than one)
mw.MWLoginError
Error during login. There are no error codes (an empty list), only dump
and message
are given.
mw.MWEditError
Error during editing, the exact same as mw.MWAPIError
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
mwng-0.0.4.tar.gz
(14.2 kB
view hashes)
Built Distribution
mwng-0.0.4-py3-none-any.whl
(15.4 kB
view hashes)