-
Notifications
You must be signed in to change notification settings - Fork 51
wiki.Wiki
The Wiki object is used to handle logins, store session data, and store data specific to the MediaWiki wiki being used. Wiki objects are hashable and can be compared. 2 wikis are considered equal if they have the same apibase.
Wiki has 3 constructor parameters
-
url
- The URL to the API for the website. Defaults to "https://en.wikipedia.org/w/api.php" -
httpuser
- Username for sites that require HTTP Auth -
httppass
- Password for sites that require HTTP Auth. If httpuser is set and this is left blank, it will propmpt for a password to be entered interactively
setSiteinfo()
Gets basic information about the wiki, populates most of the member variables, and sets the Namespace attributes (see wiki for documentation). This is called automatically during the constructor or as part of login() if the wiki requires login to read. Returns the Wiki object.
login(username, password=False, verify=True, domain=None)
Logs into the wiki using the given username and password. If no password is given, you will be prompted to enter it interactively. If verify
is True it will call isLoggedIn to verify that it is storing the session properly. domain
is the domain name used for certain auth systems like LDAP
Returns True if logged in properly (or if there were no errors and verify is False) or False if there is an error or the verification check failed. It will also add your username to the user-agent string.
logout()
Logs out of the wiki, resets the session, and resets some of the member variables to their default values. Returns True.
isLoggedIn(username=None)
Checks if you're currently logged in. By default it only checks that you're logged in at all. Set username
to check that you're logged in to a specific account. Returns True or False.
setMaxlag(maxlag = 5)
Sets the maximum DB server lag to allow during use. If the lag is higher, it will wait, then try the request again. It must be set to an integer. The default value is 5 seconds. A negative value will bypass the maxlag check. Returns the maxlag value
setUserAgent(useragent)
Can be used to set a custom user-agent header. The default is "python-wikitools/VERSION" if logged out and "python-wikitools/VERSION (User:Username)" if logged in. Returns the user-agent string.
setAssert(value)
On wikis with the AssertEdit extension, sets the "assert" value to be sent with every request. If the assertion fails, it will return an error. "nassert" is not currently supported via this interface. See the extension documentation for full details. Common options are:
- 'user' - Verify a logged-in account
- 'bot' - Verify an account with the bot flag
getToken(tokentype)
Get a token for editing or other actions. Possible values for tokentype
and behavior depend on which version of MediaWiki is installed and extensions that may add their own tokens.
On wikis older than version 1.24, only "edit" tokens can be retrieved. These also work for most other actions. Accepted aliases are: 'delete', 'protect', 'move', 'block', 'unblock', 'email', 'csrf'
On wikis with 1.24+, any type available via action=query&meta=tokens can be retrieved, including 'csrf' (used for most actions), 'patrol', 'rollback', 'userrights', and 'watch'.
-
apibase
- The URL passed to the constructor -
session
- A requests.Session object -
username
- Your wiki username, an empty string if logged out -
domain
- The full domain name, including the protocol (e.g. "https://en.wikipedia.org") -
auth
- A tuple with the httpuser and httppass values, or None if not using HTTP Auth -
maxlag
- The number of seconds of DB lag allowed during use. See setMaxlag. Default = 5 -
maxwaittime
- If an HTTP error occurs on a non-write query, it will wait 5 seconds before retrying. If it fails repeatedly, it will wait 10 seconds, then 15, and so on up to maxwaittime, at which point it will raise an Exception. Also, if lag > maxlag, it will wait either the number of seconds lagged or maxwaittime, whichever is lesser, before trying again. Default = 120. -
useragent
- The User-Agent header. See setUserAgent for information and defaults. -
limit
- The maximum number of results that can be retrieved in a query. Default is 500, increased to 5000 during login() if the user has the apihighlimits right (usually assigned to sysops and bots) -
siteinfo
- A dict with information retrieved from action=query&meta=siteinfo, such as the MediaWiki version, language, and timezone. Set during setSiteInfo() -
namespaces
- A dict with namespace information with namespace numbers as keys. Includes the namespace name in the site language, canonical name (if available), title capitalization, and whether subpages are allowed. Set during setSiteInfo() -
NSaliases
- A dict mapping namespace aliases to their corresponding namespace numbers. Set during setSiteInfo() -
assertval
- See setAssert. Default = None -
newtoken
- True or False, whether to use the new token retrieval method introduced in MW 1.24. Set during setSiteInfo()
Create a wiki object, login, and get an edit token:
from wikitools import wiki
site = wiki.Wiki("https://www.mediawiki.org/w/api.php")
if not site.login("Example", "Password", verify=True):
print("Login failed")
return
print(site.getToken('csrf'))
will print something like:
e8a7c21eab803718e5c7666aa3d4210e54d1b8b9+\
Get information about a namespace:
from wikitools import wiki
site = wiki.Wiki("https://www.mediawiki.org/w/api.php")
print site.namespaces[site.NS_TALK]
will return
{'canonical': 'Talk', 'case': 'first-letter', 'subpages': '', 'id': 1, '*': 'Talk'}