Tools:Installing pywikipedia

From DispersiveWiki
Jump to navigationJump to search

The pywikipedia package is a python package commonly used to edit the wikipedia and other mediawiki projects. It can be used to automate editing of the wiki. This page explains how to install pywikipedia to edit the DispersiveWiki. This package provides scripts which can be called directly from the command line and python code which can be called in your own scripts.

More information on installing pywikipedia and using it on non-wikipedia wikis can be found at:

http://meta.wikimedia.org/wiki/Pywikipedia
http://meta.wikimedia.org/wiki/Pywikipedia_bot_on_non-wikimedia_projects

Currently, the dispersivewiki has no policy on using bots.

Installing the pywikipedia bot for the dispersive wiki

The following discussion assumes you are using a *nix based system.

  • Download a recent snapshot of pywikipedia from sourceforge
http://sourceforge.net/project/showfiles.php?group_id=93107
  • Unzip the file
 math$ unzip  pywikipedia-05-09.zip 

This creates a directory with the pywikipedia code. In the 5 September snapshot in the example, this is called "bot2".

  • Save the following text as bot2/families/DispersiveWiki_family.py
 
# -*- coding: utf-8  -*-

import family

# The dispersive wiki
# Taken from the example at: 
#  http://meta.wikimedia.org/wiki/Pywikipedia_bot_on_non-wikimedia_projects

class Family(family.Family):

    def __init__(self):

        family.Family.__init__(self)

        self.name          = 'DispersiveWiki'
        self.langs         = { 'en':         'tosio.math.toronto.edu', }
        self.namespaces[4] = { '_default':  u'DispersiveWiki',       }
        self.namespaces[5] = { '_default':  u'DispersiveWiki talk',  }

    def version(self, code):
        return "1.6.8"

    def path(self, code):
        return '/wiki/index.php'
  • Save the following text as bot2/user-config.py, with YourUserName replaced by your user name.
family='DispersiveWiki'
mylang='en'
usernames['DispersiveWiki']['en'] = 'YourUserName'

  • Login for the first time.
math$ python login.py

This will ask for your password. Cookie information regarding your login and session is recorded in bot2/login-data/DispersiveWiki-en-YourUserName-login.data. The important thing is that other pywikipedia scripts will not ask for your password again.


pywikipedia examples

  • The pywikipedia bot provides several scripts which can be called directly from the command line. For example, to replace "big" by "huge" on the sandbox page, in the bot2 directory, type:
math$ python replace.py -page:sandbox "big" "huge"

You can get help from most of the scripts by reading the code or

math$ python replace.py -help
  • The pywikipedia bot provides python code which can be used in your own scripts. Important commands are wikipedia.getSite(), wikipedia.Page(site,'pagename'), page.get(), and page.put('text'), wikipedia.setAction('text'). Scripts should end with wikipedia.stopme() to properly exit. Here's an example which posts "Hello World" to the sandbox.
# -*- coding: utf-8  -*-
"""
This writes "Hello World" in the sandbox page. 
Most of pywikipedia bot appears to be released under the MIT license. 
"""

from __future__ import generators
import sys, re
import wikipedia, pagegenerators, catlib, config

msg = "DispersiveWiki python test"
site = wikipedia.getSite()
page = wikipedia.Page(site, 'sandbox')
oldtext = page.get()

EditMsg = 'pywikipedia bot testing'
wikipedia.setAction( EditMsg )

newtext = 'Hello World. \n '

page.put(newtext)

wikipedia.stopme()