MediaWiki API
This MIT-licensed library provides a very simple convenience wrapper around the MediaWiki API, including support for authenticated sessions. It requires Python 3 and that your wiki is using MediaWiki 1.15.3 or greater.
- Installation:
pip install mwapi - Documentation: https://pythonhosted.org/mwapi
- Repository: https://github.com/mediawiki-utilities/python-mwapi
- License: MIT
Examples
Single query
>>> import mwapi
>>>
>>> session = mwapi.Session('https://en.wikipedia.org')
>>>
>>> print(session.get(action='query', meta='userinfo'))
{'query': {'userinfo': {'anon': '', 'name': '75.72.203.28', 'id': 0}},
'batchcomplete': ''}
>>>
>>> print(session.get(action='query', prop='revisions', revids=32423425))
{'query': {'pages': {'1429626': {'ns': 0, 'revisions': [{'user':
'Wknight94', 'parentid': 32276615, 'comment':
'/* References */ Removing less-specific cat', 'revid': 32423425,
'timestamp': '2005-12-23T00:07:17Z'}], 'title': 'Grigol Ordzhonikidze',
'pageid': 1429626}}}, 'batchcomplete': ''}
Query with continuation
import mwapi
from mwapi.errors import APIError
session = mwapi.Session('https://en.wikipedia.org/')
# If passed a `continuation` parameter, returns an iterable over a continued query.
# On each iteration, a new request is made for the next portion of the results.
continued = session.get(
formatversion=2,
action='query',
generator='categorymembers',
gcmtitle='Category:17th-century classical composers',
gcmlimit=100, # 100 results per request
continuation=True)
pages = []
try:
for portion in continued:
if 'query' in portion:
for page in portion['query']['pages']:
pages.append(page['title'])
else:
print("MediaWiki returned empty result batch.")
except APIError as error:
raise ValueError(
"MediaWiki returned an error:", str(error)
)
print("Fetched {} pages".format(len(pages)))
Asynchronous single query
import asyncio
import aiohttp
import mwapi
async def query():
async with aiohttp.ClientSession() as s:
session = mwapi.AsyncSession(
'https://en.wikipedia.org',
user_agent='mwapi async demo',
session=s)
response = await asyncio.create_task(
session.get(action='query', prop='revisions', revids=32423425)
)
print(response)
asyncio.run(query())
Asynchronous query with continuation
import asyncio
import aiohttp
import mwapi
from mwapi.errors import APIError
async def query():
async with aiohttp.ClientSession() as s:
session = mwapi.AsyncSession(
'https://en.wikipedia.org',
user_agent='mwapi async demo',
session=s)
continued = await asyncio.create_task(
session.get(
formatversion=2,
action='query',
generator='categorymembers',
gcmtitle='Category:17th-century classical composers',
gcmlimit=100, # 100 results per request
continuation=True)
)
pages = []
try:
async for portion in continued:
if 'query' in portion:
for page in portion['query']['pages']:
pages.append(page['title'])
else:
print("MediaWiki returned empty result batch.")
except APIError as error:
raise ValueError(
"MediaWiki returned an error:", str(error)
)
print("Fetched {} pages".format(len(pages)))
asyncio.run(query())
Authors
- YuviPanda -- https://github.com/yuvipanda
- Aaron Halfaker -- https://github.com/halfak
Metadata
Release files for mwapi 0.6.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mwapi-0.6.1.tar.gz | 10.6 kB | Without provenance | Details for mwapi-0.6.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mwapi-0.6.1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for mwapi-0.6.1-py2.py3-none-any.whl |
Total release size: 22.8 kB
Release files / mwapi-0.6.1.tar.gz
| Download URL | mwapi-0.6.1.tar.gz |
|---|---|
| Size | 10.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bc8917d675b78860b085d85bc9883e96ae66f80d42a3d214742ff46c32bd38fe
|
|
BLAKE2b-256 checksum How to use checksums |
9f2f2dca9cba4f080be2d09bcc8fbc7215eba6f49c2b967c407e0d3f7ac0ae05
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.26.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.15.0 CPython/3.8.10
|
Release files / mwapi-0.6.1-py2.py3-none-any.whl
| Download URL | mwapi-0.6.1-py2.py3-none-any.whl |
|---|---|
| Size | 12.2 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
bf38a712915405953f4618c879dd6562f668464b7138df7951a4ac17ffc07173
|
|
BLAKE2b-256 checksum How to use checksums |
43989b7cc0be4f664233d2ea420ac62fbf8a1b5f3c1bca067e795723b401fa5b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.26.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.15.0 CPython/3.8.10
|
Security for mwapi 0.6.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.