Help:The shortest way to your own bot
We show how to create a basic robot in the pywikipedia from work which reads the sandbox in the wikiversity:sandbox, make some changes, and put it back.
For the sake of argument, we assume the machine is a windows XP.
Here are the steps. (If at any stage there is any problem, see meta:pywikipedia further more details.) From level zero:
- Download
- Download and install python, version 2.4 or above, from here:[1] for windows
- Download the file wikipedia.py from here:[2], putting it in the same directory as your python.exe
- a dip into python
- at the command prompt (via "Start"->"All programs" ->"Command Prompt"), go to the directory of your "python.exe" file, and run "python"
- write at the ">>>" prompt:
print ' hello, world! \n'
- Setup
- Open your notepad and write:
family='wikiversity' mylang='beta' usernames['wikiverity']['beta'] = 'WRITE YOUR USER NAME HERE' console_encoding = 'utf-8'
and save it in the file user-config.py in the same directory as the python.exe . This sets your bot up for wikiversity beta.
- Login
- Open a command prompt (see "All Programs"), and go to the directory of your python.exe, and run "python.exe login.py". This will log you in forever - the programme creates a file in the subdirectory (whose name I have forgotten).
- am I properly logged in?
- run "python test.py" and it will tell you
- the robot
- Open your notepad and write (note: the comments after the sharp signs "#" are not necessary):
#crap.py
import wikipedia #including the wikipedia.py module
site = wikipedia.getSite() #starting object "site" corresponding to beta.wikiversity
sand = wikipedia.Page(site, 'wikiversity:sandbox') # creating the object "sand" for the page [[wikiversity:sandbox]]
t = sand.get() #getting the text of sand
t = t +'some crap' # appending some crap to t
sand.put(t,'adding some crap to [[wikiversity:sandbox]]') #putting the new text to the sandbox, with comment
wikipedia.stopme()
and save it as "crap.py" in the same directory as "python.exe"
- run it
- go to the directory of "python.exe" and run:
python.exe crap.py
- result
read special:recentchanges and see what has happened.
- unicode
- if you want to write "some crap" in other, richer languages, use a unicode string: replace
'some crap'
byu'SOMECRAPINYOURLANGUAGE'
. Remember to choose UTF-8 when you save your programme in notepad.
- to learn more
You may start by downloading the rest of the pywikipedia framework in sourceforge [3] and reading the descriptions here [4]. The Page class has plenty of functions for you to implement.
- Python.org has very complete documentations and tutorials:
- index: http://docs.python.org/index.html
- python tutorial: http://docs.python.org/tut/tut.html
- reference manual: http://docs.python.org/ref/ref.html