User:OrenBochman/Getting Python and PyWiki

Python Wikipedia Robot Framework edit

The Python Wikipedia Robot Framework is a collection of tools made to fit the maintenance need on Wikipedia, but it can also be used on other MediaWiki sites.

There is a trunk release which supports older mediawiki software..

There is the rewrite branch of the Python Wikipedia Robot Framework. It features several improvements, such as full API usage and a pythonic package layout.

Look at the matrix below, whether you should use the trunk or rewrite fork.

Requirements for a bot edit

To run a bot on your wiki, you will need Shell access to your hosting account. If you dont know whether you have Shell access or not, ask your hosting provider. You will need Python version > 2.5 and less 3.0 although Python 2.4.3 has also worked for at least one bot script. Preferred version is 2.7.2. or 2.7.3. To check whether you have Python installed and its version, just type "python" at the Shell prompt. Every python script (a .PY file) is run by entering the following on a Unix command prompt:

python myscript.py

Steps edit

What you'll be doing: You will be creating a bot username on your wiki, give rights to it, get the bot software, make two text files, upload the software, log the bot in and run a test script to see if the bot works.

  1. Make a username for your bot, like someone would make a regular user account on the wiki (e.g. SiteBot/password). Make sure to have a reasonably secure password (not something easy like "sitebot123").
  2. Logout from the bot account and login using a Bureaucrat account. In the User rights, give that username "bot" rights. If you have the Flagged revisions extension, give that bot "reviewer" and "editor" rights.
  3. Download The PYWikipedia bot software on your computer. Direct link to zip file or see Downloads section for other formats.
  4. Extract the "pywikipedia" directory to a folder on your computer. There will be many subfolders in that directory. All of this is the bot software. Beware of non-English (accented) letters in the path, at least under Linux they caused problems in several cases.
  5. Inside that "pywikipedia" folder, create a text file with the name "user-config.py" that has the following customized lines:
mylang='en'
family = 'yoursite'
usernames['yoursite']['en']=u'MySiteBot'
console_encoding = 'utf-8'

Where you see "yoursite" for the family variable, use the name of your site ($wgSiteName), all lower case letters.

For:

usernames['yoursite']['en']=u'SiteBot'

SiteBot is the name of your bot user account that you made in step 1. Use correct case.

Save this file.

7. Upload the "pywikipedia" directory to your server. This directory can be in the same location as your LocalSettings.php file.

8. Login to your Shell account and navigate to that uploaded directory on your server.

6. Now you'll create a family family for your site. First see if it can be generated automatically. On the Shell prompt, type "python generate_family_file.py" and press enter. If you get an error and the file is not generated, then follow the example below.

For the simplest case where there's only one wiki in the english language, here's an example:

# -*- coding: utf-8  -*-
import config, family, urllib
# MyWiki family                 
# user_config.py:
# usernames['mywiki']['en'] = 'user'
class Family(family.Family):
   def __init__(self):
       family.Family.__init__(self)
       self.name = 'mywiki'             #CHANGE
       self.langs = {
           'en': 'www.mywiki.com',           #CHANGE, Site URL
          }
       self.namespaces[4] = {
           '_default': [u'MyWiki', self.namespaces[4]['_default']],   #CHANGE
       }
       self.namespaces[5] = {
           '_default': [u'MyWiki talk', self.namespaces[5]['_default']], #CHANGE
       }
   def version(self, code):
       return "1.18.0"   #CHANGE if needed, version of your MW
   def scriptpath(self, code):
       return 
   def apipath(self, code):
       return '/w/api.php'  #CHANGE if needed. The path to your api script. Its in the same location as your LocalSettings.php.

MyWiki/mywiki should be changed to reflect your wiki's name ($wgSiteName)

This text file should be renamed "mywiki_family.py" (replace mywiki with your wiki's name, all lowercase) and be uploaded to the "families" folder (you'll find other family files there, they can be ignored). You now have a family file for your site.

7. Now you'll login to your bot and see if it works. On the Shell prompt in the "pywikipedia" directory, type in: "python login.py". It should prompt for the bot's password which you made in step 1. If it logs in, you'll see a 'success' message. You only have to do this once. Bots usually stay logged in.

8. To test if your bot works or not, you can use an existing script that adds text to the top of all pages in a certain category. On your site, find a category that has only a few pages in it, not more than 10, so you can revert them easily. If you don't have such a category, add a temporary category to any 3 pages on the site.

On your Shell command prompt, inside the pywikipedia directory again, run the add_text script:

python add_text.py -cat:catname -text:"This is a Test." -except:"\{\{([Tt]emplate:|)[Dd]ocumentation [Ss]ubpage" -up

Replace catname in "-cat:catname". The catname is the name of your 'test' category. For example if the title of the category page is "Category:Test Pages", you will write "-cat:test_pages" in that command.

This will insert the text "This is a Test." on top of all pages in that test category.

If the bot is working, you'll see the Shell command prompt change according.

9. Go to your Recent Changes and click on "show bots" to see if your bot made the edits.

For other bots, see: MW:Manual:Pywikipediabot/Scripts. If you cant find a bot that will do the stuff you want it to do, you can see the existing scripts for suggestions on how you could make your own bot.

When you run a new untested bot script, run it on a "test" wiki in case it goes out of control. You can also block it like you would block a regular wiki username. You can also quit the Shell prompt.

Notes edit