added example code that uses the urls
[wikipedia-api-cdsw] / README.rst.mwclient
1 mwclient
2 ========
3
4 Mwclient is a client to the `MediaWiki API <//mediawiki.org/wiki/API>`_
5 which provides access to most API functionality.
6 It depends heavily on Bob Ippolito's `SimpleJSON <//github.com/simplejson/simplejson>`_,
7 requires Python 2.4 and supports MediaWiki 1.11 and above.
8 For functions not available in the current MediaWiki, a ``MediaWikiVersionError`` is raised.
9
10 This framework was written by Bryan Tong Minh, who released the latest stable 
11 `version 0.6.5 <//github.com/mwclient/mwclient/archive/REL_0_6_5.zip>`_ on 6 May 2011.
12 The current `development version <//github.com/mwclient/mwclient>`_
13 can be installed directly off github:
14
15 .. code-block:: console
16
17     $ pip install git+git://github.com/mwclient/mwclient.git
18
19 Please see the `release notes <//github.com/mwclient/mwclient/blob/master/RELEASE-NOTES.md>`_
20 for a list of changes.
21
22 Implementation notes
23 --------------------
24
25 Most properties and generators accept the same parameters as the API,
26 without their two-letter prefix. Exceptions to this rule:
27
28 * ``Image.imageinfo`` is the imageinfo of the latest image.
29   Earlier versions can be fetched using ``imagehistory()``
30 * ``Site.all*``: parameter ``[ap]from`` renamed to ``start``
31 * ``categorymembers`` is implemented as ``Category.members``
32 * ``deletedrevs`` is ``deletedrevisions``
33 * ``usercontribs`` is ``usercontributions``
34 * First parameters of ``search`` and ``usercontributions`` are ``search`` and ``user`` 
35   respectively
36
37 Properties and generators are implemented as Python generators.
38 Their limit parameter is only an indication of the number of items in one chunk.
39 It is not the total limit.
40 Doing ``list(generator(limit = limit))`` will return ALL items of generator,
41 and not be limited by the limit value.
42 Default chunk size is generally the maximum chunk size.
43
44
45 HTTPS
46 -----
47
48 To use https, specify the host as a tuple in the form of ``('https', hostname)``.
49
50
51 Example
52 -------
53
54 For more information, see the
55 `REFERENCE.md <//github.com/mwclient/mwclient/blob/master/REFERENCE.md>`_ file.
56
57 .. code-block:: python
58
59         # Initialize Site object
60         import mwclient
61         site = mwclient.Site('commons.wikimedia.org')
62         site.login(username, password)  # Optional
63
64         # Edit page
65         page = site.Pages['Commons:Sandbox']
66         text = page.edit()
67         print 'Text in sandbox:', text.encode('utf-8')
68         page.save(text + u'\nExtra data', summary = 'Test edit')
69
70         # Printing imageusage
71         image = site.Images['Example.jpg']
72         print 'Image', image.name.encode('utf-8'), 'usage:'
73         for page in image.imageusage():
74                 print 'Used:', page.name.encode('utf-8'), '; namespace', page.namespace
75                 print 'Image info:', image.imageinfo
76
77         # Uploading a file
78         site.upload(open('file.jpg'), 'destination.jpg', 'Image description')
79
80         # Listing all categories (don't do this in reality)
81         for category in site.allcategories():
82                 print category

Benjamin Mako Hill || Want to submit a patch?