Manual:Pywikibot/Mac

This is a complete step-by-step guide on how to run your own pywikipedia bot on Wikimedia projects, from your Mac, hopefully more easy and readable than the original one.

Downloading MacPython
First of all, you need to download Python, the application that reads the scripts and does something with them. Download MacPython from here, choosing “Python 2.5.2 for Macintosh OS X”. Open the downloaded dmg file and install it. In Applications in Finder you should now find a folder named “MacPython 2.5”. Nothing else needs to be done here, close Finder.

NOTE: A new application will be installed on your Mac, with the name “IDLE”. It’s just difficult and confusing, and nothing worth spending time on trying to use.

Downloading SVN
Many Wikimedia projects require that you always should run the latest version of the Pywikipedia framework. The easiest option is to use SVN. This is no harder than running the bot itself. Download “My Subversion-1.4.4 Package” from this page. Open the Subversion-1.4.4 package, and complete the installation. Now, everything is all set to download the scripts.

Downloading the scripts
Open Terminal, you can find it by searching for it in Spotlight. Now, copy-paste the following into Terminal, and press enter/return: svn checkout http://svn.wikimedia.org/svnroot/pywikipedia/trunk/pywikipedia/ pywikipedia

A folder will be created in the “home” folder (look in the left column in the Finder for the house icon and your username), with the name “pywikipedia”, in which all the scripts will be when the downloading is finished. You can see when it’s finished by looking for the following in the last line in Terminal: NAME_OF_COMPUTER:~ USER_NAME$ This text is equal to the one appearing when you open Terminal.

Configuring
It may be difficult to create a new .py script with TextEdit. Simply open the “pywikipedia” folder, scroll down until you fint xmltest.py. Whilst pressing the alt/option key, drag the xmltest.py script to the side, so that you get a copy of it, xmltest 2.py. Then, rename it to “user-config.py”. Secondary-click on it, hold the cursor on Open in, then choose Other, scroll down and double-click on TextEdit. Now, erase all content (cmd+A, then backspace).

Language
mylang = 'xx' “xx” is the main language you want your bot to work on, equivalent to the language code of the Wikimedia project in which the bot’s going to run. Don’t close TextEdit.

Family
If the bot’s main wiki is Wikipedia, go right to the Username(s) section. If the bot’s main wiki is for instance Wikibooks, add this line to user-config.py: family = 'wikibooks' Don’t close TextEdit.

NOTE: Running bots on Wiktionary is more complicated than on the other projects, and is not recommended for beginners. This will not be described further in this guide. If you’re running your bot on Commons, both mylang and family are to be set to “commons”.

Username(s)
Now, you’ll define the desired username(s) of the bot. Don’t register the bot on the wikis yet. You can do this by using the following: usernames['FAMILY']['MYLANG'] = u'ExampleBot' “MYLANG” is the same as you defined in the mylang option, and the same with “FAMILY”. If you’re running the bot on Wikipedia, the family option is not defined, but should be “wikipedia” in the above line (se examples below). The line can be defined as many times as you like (only one per wiki), for each of your bots on different WM wikis. The default will be what’s defined in mylang, except when running interwiki; the bot will edit on all the wikis defined in user-config.py.

Encoding
It’s recommended that you change the encoding to UTF-8. Especially if you are editing a wiki with non-ASCII characters, this is important, if you don’t change the encoding, you’ll end up with “?”s all over the place. For example, the Spanish name María will render like Mar?a. Add this line to user-config.py: console_encoding = 'utf-8'

API login
Because of some problems with SUL bots, the bot must log in via API. Add this line to user-config.py: use_api_login = 'true'

Now, by using Save as, save the file in the “pywikipedia” folder. Then, close TextEdit.

EksempelBot on no.wikipedia
mylang = 'no' usernames['wikipedia']['no'] = u'EksempelBot'

console_encoding = 'utf-8' use_api_login = 'true'

ExampleBot on Commons
mylang = 'commons' family = 'commons' usernames['commons']['commons'] = u'ExampleBot'

console_encoding = 'utf-8' use_api_login = 'true'

BeispielBot on de.wikipedia and de.wikibooks, with de.wikipedia as main wiki
mylang = 'de' usernames['wikipedia']['de'] = u'BeispielBot' usernames['wikibooks']['de'] = u'BeispielBot'

console_encoding = 'utf-8' use_api_login = 'true'

Registering the username on the wanted wikis
It is recommended that you are registered on the wikis where you want to run your bot. Then, when you’re logged into your own user account, write “Special:Userlogin” in the search box, then, choose to register an account. Now fill out the required fields on the page and continue. If the account creaton is successful, you should now be able to run the bot.

With SUL it’s easy to run bots on several wikis. Just register the account on the main wiki, then merge it in Special:MergeAccount. Now, when logging in, the other accounts will automatically be registered as you enter the same password for all wikis.

Bot flag and test edits
Don’t forget to apply for bot flag/permission to run the bot! On en.wikipedia and he.wikipedia (there may be more), you have to apply before you make test edits. On other wikis you can apply and make test edits right away. When you make test edits, use the argument -pt:15, which tells the bot to wait 15 seconds between each edit (4 edits/min), so the recent changes list doesn’t get flooded with the bot’s edits.

Navigating to the right folder
To run the scripts, you first need to navigate to the folder in which the scripts are. Write the following in Terminal: cd /Users/USER_NAME/pywikipedia “USER_NAME” is the home text to the right of the house icon in Finder.

Tip
The above can be unnecessarily boring to write each time you want to use the bot. Terminal saves every command you use, and you can use old commands by pressing arrow up. With one press on the arrow up button, you see the last command used. Most likely, the commans you see is not the right one; simply keep pressing arrow up until you see the right one. Then, press enter/return. Now, you can start running the bot. When you’re running a script followed by another when the first one is done, you don’t have to use the navigation command once again; only if you close Terminal.

Logging in
Now, you need to log into the bot accounts on the wiki(s), by typing the following into Terminal: login.py Terminal will ask for the password. Type the password you used when you registered the bot’s username.

If you have accounts on multiple wikis, use login.py -all Type in the passwords, as above.

Logging in is only necessary once, the bot stays logged in.

Running the scripts
After navigating to the right folder, you can now select scripts, by writing: SCRIPT_NAME.py

To see an overview of some scripts with description pages, look here.

Arguments
Arguments are written by using a dash: - and then the name of the argument, -pt:15 for instance. Many scripts require arguments, for example interwiki.py: interwiki.py -start:! -autonomous As default, the bot will run interwiki from the main language and project, and correct them on all registered wikis. The -start and -autonomous arguments says that it’ll check all pages on the wiki and add/modify interwikis. It will not remove anything, and it will give up when discovering conflicts, see interwiki.py.

Global arguments
All scripts recognize the following arguments:
 * -help
 * Shows the script’s help text


 * -lang:xx
 * Selects the language you want to work on. Overrides the mylang option in user-config.


 * -family:xyz
 * Selects the family you want to work on. Overrides the family option in user-config.


 * -log
 * Enable the logfile. Logs will be stored in the logs subdirectory.


 * -log:xyz
 * Enable the logfile, using “xyz” as the filename.


 * -nolog
 * Disable the logfile (if it's enabled by default).


 * -putthrottle:nn or short -pt:nn
 * Set the minimum time (in seconds) the bot will wait between saving pages. The default value is zero.

Updating
To update your scripts, simply write the following into Terminal: svn update pywikipedia When it’s done, it will say “At revision XXXX” (“XXXX” depending on the latest revision). If you have modified some of the scripts, don’t worry, your modifications will be merged with the changes made by updating.

If you have experience using Automator, you can do the Shell process in a workflow and save it to your desktop or on your dock, so you can reach it more easily. It’s a good idea to secondary click on it, then, while pressing Alt/Option, hold the cursor over Always open in, then choose Automator runner.

Modifying
You might want to go through the script text before you run it, because for some languages the script may be bot specific. Secondary-click on it, hold the cursor on Open in, then choose Other, scroll down and double-click on TextEdit. However, this is not mandatory, and not recommended for beginners, but if you have to; be careful when editing the scripts!

Reverting
If you want to revert your changes to a script, simply run the following: svn revert NAME_OF_SCRIPT.py

If a script suddenly doesn’t work, and produces the output  SyntaxError: invalid syntax , you have encountered an SVN conflict.

Revert the script as described above. Then try to run it. If it works, run the following in Terminal: svn resolved NAME_OF_SCRIPT.py

You can now try to make the same edits again, if desired.