Skip to content
Snippets Groups Projects
README.rst 5.4 KiB
Newer Older
Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed

.. image:: https://img.shields.io/travis/mwclient/mwclient.svg
Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed
   :target: https://travis-ci.org/mwclient/mwclient
   :alt: Build status
Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed

.. image:: https://img.shields.io/coveralls/mwclient/mwclient.svg
   :target: https://coveralls.io/r/mwclient/mwclient
   :alt: Test coverage

.. image:: https://landscape.io/github/mwclient/mwclient/master/landscape.svg?style=flat
   :target: https://landscape.io/github/mwclient/mwclient/master
   :alt: Code health

.. image:: https://img.shields.io/pypi/v/mwclient.svg
Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed
   :target: https://pypi.python.org/pypi/mwclient
   :alt: Latest version
Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed

.. image:: https://img.shields.io/pypi/dw/mwclient.svg
Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed
   :target: https://pypi.python.org/pypi/mwclient
   :alt: Downloads

.. image:: https://img.shields.io/github/license/mwclient/mwclient.svg
   :target: http://opensource.org/licenses/MIT
   :alt: MIT license
Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed

.. image:: https://readthedocs.org/projects/mwclient/badge/?version=master
   :target: https://readthedocs.org/projects/mwclient/?badge=master
   :alt: Documentation Status

Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed
mwclient is a lightweight Python client library to the `MediaWiki API <https://mediawiki.org/wiki/API>`_
which provides access to most API functionality.
It requires Python 2.6 or 2.7 (Python 3.3 and 3.4 supported in the development version)
and supports MediaWiki 1.16 and above.
For functions not available in the current MediaWiki, a ``MediaWikiVersionError`` is raised.
Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed
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
Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed
`version 0.7.2 <https://github.com/mwclient/mwclient/archive/v0.7.2.zip>`_
was released on 18 July 2015, and is `available through PyPI <https://pypi.python.org/pypi/mwclient>`_:

.. code-block:: console

    $ pip install mwclient

Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed
The current `development version <https://github.com/mwclient/mwclient>`_
can be installed from GitHub:
    $ pip install git+git://github.com/mwclient/mwclient.git
Please see the 
Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed
`release notes <https://github.com/mwclient/mwclient/blob/master/RELEASE-NOTES.md>`_
Contributing
--------------------

Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed
mwclient ships with a test suite based on `pytest <https://pytest.org>`_.
Waldir Pimenta's avatar
Waldir Pimenta committed
Only a small part of mwclient is currently tested, but hopefully coverage
will improve in the future.

The easiest way to run tests is:

.. code-block:: console

    $ 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
Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed
`"editable" install <https://pip.readthedocs.org/en/latest/reference/pip_install.html#editable-installs>`_:

.. code-block:: console

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

To run tests with different Python versions in isolated virtualenvs, you
Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed
can use `Tox <https://testrun.org/tox/latest/>`_:

.. code-block:: console

    $ 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 <https://meta.wikimedia.org/wiki/User-Agent_policy>`_. 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:

.. code-block:: python
Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed
    useragent = 'YourBot, based on mwclient v0.7.2. Run by User:You, you@gmail.com'
    site = mwclient.Site(('https', 'en.wikipedia.org'), clients_useragent=useragent)


For more information, see the
Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed
`REFERENCE.md <https://github.com/mwclient/mwclient/blob/master/REFERENCE.md>`_ file
Dan Michael O. Heggø's avatar
Dan Michael O. Heggø committed
`documentation on the wiki <https://github.com/mwclient/mwclient/wiki>`_.

.. code-block:: python

	# Initialize Site object
	import mwclient
	site = mwclient.Site('commons.wikimedia.org')
Waldir Pimenta's avatar
Waldir Pimenta committed
	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