Skip to main content

MediaWiki API client

Project description

http://img.shields.io/travis/mwclient/mwclient.svg?style=flat http://img.shields.io/pypi/v/mwclient.svg?style=flat http://img.shields.io/pypi/dw/mwclient.svg?style=flat

mwclient

mwclient is a lightweight Python client library to the MediaWiki API which provides access to most API functionality. It requires Python 2.6 or 2.7 (Python 3.x support planned) and supports MediaWiki 1.16 and above. For functions not available in the current MediaWiki, a MediaWikiVersionError is raised.

This framework was written by Bryan Tong Minh, who maintained the project until version 0.6.5, released on 6 May 2011. The current stable version 0.7.1 was released on 23 November 2014, and is available through PyPI:

$ pip install mwclient

The current development version can be installed from GitHub:

$ pip install git+git://github.com/mwclient/mwclient.git

Please see the release notes for a list of changes.

Contributing

mwclient ships with a test suite based on pytest. Only a small part of mwclient is currently tested, but hopefully coverage will improve in the future.

The easiest way to run tests is:

$ python setup.py test

This will make an in-place build and download test dependencies locally if needed. To make tests run faster, you can use pip to do an “editable” install:

$ pip install pytest pytest-pep8 responses
$ pip install -e .
$ py.test

To run tests with different Python versions in isolated virtualenvs, you can use Tox:

$ pip install tox
$ tox

Implementation notes

Most properties and generators accept the same parameters as the API, without their two-letter prefix. Exceptions to this rule:

  • Image.imageinfo is the imageinfo of the latest image. Earlier versions can be fetched using imagehistory()

  • Site.all*: parameter [ap]from renamed to start

  • categorymembers is implemented as Category.members

  • deletedrevs is deletedrevisions

  • usercontribs is usercontributions

  • First parameters of search and usercontributions are search and user respectively

Properties and generators are implemented as Python generators. Their limit parameter is only an indication of the number of items in one chunk. It is not the total limit. Doing list(generator(limit = limit)) will return ALL items of generator, and not be limited by the limit value. Default chunk size is generally the maximum chunk size.

HTTPS

To use https, specify the host as a tuple in the form of ('https', hostname).

User-agents

Bots that run on Wikimedia wikis require an informative user-agent for all API requests. To change the user-agent, you will need to include an appropriate parameter for clients_useragent when you initialize your Site, as shown in the following example:

useragent = 'YourBot, based on mwclient v0.6.5. Run by User:You, you@gmail.com'
site = mwclient.Site(('https', 'en.wikipedia.org'), clients_useragent=useragent)

Example

For more information, see the REFERENCE.md file or the documentation on the wiki.

# Initialize Site object
import mwclient
site = mwclient.Site('commons.wikimedia.org')
site.login(username, password)

# Edit page
page = site.Pages['Commons:Sandbox']
text = page.text()
print 'Text in sandbox:', text.encode('utf-8')
page.save(text + u'\nExtra data', summary = 'Test edit')

# Printing imageusage
image = site.Images['Example.jpg']
print 'Image', image.name.encode('utf-8'), 'usage:'
for page in image.imageusage():
        print 'Used:', page.name.encode('utf-8'), '; namespace', page.namespace
        print 'Image info:', image.imageinfo

# Uploading a file
site.upload(open('file.jpg'), 'destination.jpg', 'Image description')

# Listing all categories (don't do this in reality)
for category in site.allcategories():
        print category

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mwclient-0.7.1.tar.gz (18.2 kB view details)

Uploaded Source

File details

Details for the file mwclient-0.7.1.tar.gz.

File metadata

  • Download URL: mwclient-0.7.1.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for mwclient-0.7.1.tar.gz
Algorithm Hash digest
SHA256 d4d54ea8cd0f1b0f9dcd82bb4a8938426875dcc262acbb80350de9fdc1e05dc4
MD5 b9a6f5297e923cc9bb0b09692362ec18
BLAKE2b-256 ee1ab152267e1ed905b7a6cc3a14576633b491368877c80f7dadccc51a5d9473

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page