Pywikipediabot
Pywikipediabot (PWB) is the "Python Wikipediabot Framework is a set of bot scripts to do things on Wikipedia or other wikis using MediaWiki. It has been written by several people using the language Python. On this page, we give the general information for people who want to use the bot software."
Needed skills
This course assumes that you know/have:
- Installed Python on your operating system
- Using necessary tools
- Python basics
- Introduction into Wikimedia projects
Some general basics
Installing pywikipediabot framework
Documentation on Pywikipediabot page is very good. Use it for initial instructions.
In short, make a subdirectory inside of your working directory and go there:
<source lang="bash"> $ mkdir mybot $ cd mybot </source>
Invoke Subversion checkout to download pywikipediabot:
<source lang="bash"> $ svn checkout http://svn.wikimedia.org/svnroot/pywikipedia/trunk/pywikipedia/ pywikipedia </source>
If you have a really slow connection, don't download spell-checking files:
<source lang="bash"> $ svn checkout --ignore-externals http://svn.wikimedia.org/svnroot/pywikipedia/trunk/pywikipedia/ pywikipedia </source>
Pywikipediabot is a very active project. Because of that you should periodically (maybe daily, maybe weekly) update your copy:
<source lang="bash"> $ cd pywikipedia/ $ svn update </source>
Logging in
Your bot should be logged in to the particular project.
If you don't have a bot account on some project, open it. On bot's user page state clearly that this is a bot account, that you are the owner of the bot and that bot will not do anything outside of getting pages and testing inside of its and your user space. Also, note that you will ask community for any other bot action. Such bot doesn't need a bot flag. However, be careful with contamination of Recent changes. This means that you shouldn't have more then, let's say, 10 edits per day on some bigger project, which means that you should use some bigger project for learning.
Go into the directory "pywikipedia":
<source lang="bash"> $ cd pywikipedia/ </source>
Make file "user-config.py" by using your favorite editor (emacs, vim, joe, nano...). It should contain something like:
<source lang="python"> mylang = 'en' family = 'wikinews' usernames['wikinews']['en'] = 'My Bot Name' usernames['wikinews']['pl'] = 'My Bot Name' usernames['wikinews']['sr'] = 'My Bot Name II' # you may use different names </source>
By invoking "login.py" program...
<source lang="bash"> $ python login.py </source>
... you will get a prompt for inserting your password...
Checked for running processes. 1 processes currently running, including the current process. Password for user My Bot Name on wikinews:en:
... and, if everything is good, program will give to you a message like:
Logging in to wikinews:en as My Bot Name Should be logged in now
However, if you have more then one account (like it was described) and you have one password for all bots (which is little bit less safe, but much more practical), you should use options "-all" and "-pass":
millosh@Malatesta:~/wikinews/pywikipedia$ python login.py -all -pass Checked for running processes. 1 processes currently running, including the current process. Password for all accounts: Logging in to wikinews:sr as My Bot Name II Should be logged in now Logging in to wikinews:en as My Bot Name Should be logged in now Logging in to wikinews:pl as My Bot Name Should be logged in now millosh@Malatesta:~/wikinews/pywikipedia$
Invoking Python
Your bot should assume that it is multilingual. Because of that, use UTF-8 (or some better character set) and declare it at the beginning of your Python script:
<source lang="python">
- !/usr/bin/python
- -*- coding: utf-8 -*-
</source>
Pywikipediabot basics
Importing basic modules
Usually, I am using this approach in calling basic PWB functions (if you have some other approach, please add it here):
<source lang="python"> mydir = "./" pwbdir = mydir + "pywikipedia/" import sys sys.path.append(pwbdir) from wikipedia import * </source>