Meta:Tools/Wiki Bots/Python/mwclient

提供: wiki
移動先: 案内検索

Mediawiki API and mwclient

Mediawiki has a php API that can be used to obtain dat about the wiki.
Go at http://wiki.blender.org/api.php to see some example of it or have a look at the queries you can do.

Reading the clients list I've discovered an excellent python client called mwclient.

Here is an example of how to build a list with all the pages in the "Main" namespace and in the "User:" mainspace:

#! /usr/bin/env python

import mwclient
site = mwclient.Site('wiki.blender.org',path='')
at_root=0
mylist=[]
for page in site.allpages(namespace=0):
  mylist.append(page.name)
  if '/' not in page.name: at_root+=1
titles=sorted(mylist)
mylist=[]
for page in site.allpages(namespace=2):
  mylist.append(page.name)
titles+=sorted(mylist)
print '\nThere are %s pages, %s at the main root\n' % (len(titles),at_root)

Today (2008/12/14) you'll find about 4400 pages. 300 of them are at the main root (that is, not within a structure).

If you want to try it, place this script in a folder as 'bwiki.py', then download the mwclient folder in the same folder of bwiki.py. In the shell, go to the folder that contains bwiki.py and mwclients folder and run python. Then type

>exec file('bwiki.py')

or use the complete path to bwiki.py if you have problems.