Manual:Pywikibot/Mac

This is a complete step-by-step guide on how to install and run your own pywikipedia bot on Wikimedia projects from a Mac.

All of the Bot software can be run on your local Mac against the particular Wikimedia server -- a "Shell Account" on that Wikimedia server is not necessary. However, a "Bot Account" is!
 * You can also run against a Wikimedia installation running on your own Mac under OSX Server!
 * (OSX Server includes a standard distribution WIKI. Mediawiki software needs to be installed separately.)


 * While a number of the actions can be performed directly from the Finder, most require that you are familiar with the use of the Terminal window. This provides what Unix and Linux users call "Shell access." The OSX Terminal application defaults to the Bourne Shell.
 * It is also recommended that you are conversant with a "Text Editor," not a "Word Processor."
 * While "Text Edit" is an application supplied by Apple, it is NOT well suited to most of the work which needs to be done.
 * One solution is to use one of the "traditional" command line editors supplied with OSX -- pico(nano) or emacs; another is to use a full GUI based text editor such as TextWrangler (free) or BBEdit (commercial) from BareBones Software.
 * Experienced users can also use the facilities of Xcode.

Downloading Python
Apple provides an installation of Python with every Mac OS X installation, by default. You don’t need to install Python.


 * OSX 10.8.4 (Mountain Lion) provides (i.e. provided by Apple) both Python and Pythonw 2.6; and Python and Pythonw 2.7. The default is Python 2.6 -- "$man python" for details on switching between the two if necessary.

Downloading SVN

 * OSX 10.8.4 (Mountain Lion) provides (i.e. provided by Apple) SVN: Subversion command-line client, version 1.6.18

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.
 * Note that if you are familiar with Xcode, you can also use it for many of these actions. However, using Xcode is beyond the scope of this article.

Downloading the scripts
Open Terminal. (In Lion (10.7) and Mountain Lion (10.8) Terminal is found under "/Applications/Utilities" in the Finder.) Now, copy-paste the following into Terminal, and press enter/return: svn checkout http://svn.wikimedia.org/svnroot/pywikipedia/trunk/pywikipedia/ pywikipedia

Using Apple's SVN under Mountain Lion 10.8.4, the following Error will be generated by Apple's security interface (Gatekeeper): Error validating server certificate for 'https://svn.toolserver.org:443': - The certificate is not issued by a trusted authority. Use the fingerprint to validate the certificate manually! Certificate information: - Hostname: *.toolserver.org - Valid: from Sun, 26 May 2013 09:34:24 GMT until Wed, 28 May 2014 17:08:01 GMT - Issuer: GeoTrust, Inc., US - Fingerprint: bf:35:7f:3e:62:4b:89:6c:bc:39:c9:c3:38:81:9e:53:26:43:be:f4 (R)eject, accept (t)emporarily or accept (p)ermanently? Responding "p/t" may then result in: svn: warning: Error handling externals definition for 'pywikipedia/externals/pycolorname': svn: warning: OPTIONS of 'https://svn.toolserver.org/svnroot/drtrigon/externals/pycolorname': Could not read status line: connection was closed by server (https://svn.toolserver.org)
 * ( Note: This is assumed to be a transient SVN error and will likely go away with the next SVN update.)

A folder will be created in your “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 (it is called the command line or terminal prompt).

Before you begin
There are a number of files in the SVN download to which one needs to pay attention. While they can be read from the Finder, you cannot perform the necessary actions on them from there.
 * README
 * CONTENTS
 * docs/README


 * Note that when viewed from the Finder, the directory (folder) is displayed in CASE INSENSITIVE alphabetical order (upper and lower case are intermixed), while when viewed from a "ls" in Terminal, they appear in CASE SENSITIVE order -- i.e. the Capitalized names will appear at the top.


 * The top-level README file is a standard inclusion in such software distributions, however in this case, it's contents are simply cosmetic. They provide no useful information.
 * The CONTENTS file contains important information describing the individual files in the distribution.

The first step in installing pywikipedia is to run the python code:  . This cannot be done from the Finder.
 * Note that if you have Xcode installed, double clicking on this script from the Finder window will launch Xcode.

cd pywikipedia $ python generate_user_files.py Which will generate the following:
 * In a Terminal window:
 * Note 1: For your first attempt, generate both files
 * Note 2: The list of WIkis are those who are advertised to Mediawiki.org; for a private Wiki, select "27 - test"
 * Note 3: The default language is english
 * Note 4: for "Username" enter the username associated with your bot; typically 
 * Note 5: Choose the "Small" (S) version of the config file for your first attempt. The extend version contains many options beyond the scope of this article.

1: Create user_config.py file 2: Create user_fixes.py file 3: The two files What do you do? 3 <-- Note 1: generate both files 1: anarchopedia 2: battlestarwiki 3: botwiki 4: celtic 5: commons 6: fon 7: gentoo 8: i18n 9: incubator 10: krefeldwiki 11: lockwiki 12: loveto 13: lyricwiki 14: mediawiki 15: memoryalpha 16: meta 17: mozilla 18: oldwikivoyage 19: omegawiki 20: openttd 21: osm 22: piratenwiki 23: southernapproach 24: species 25: strategy 26: supertux 27: test 28: twcareer 29: ubuntutw 30: uncyclopedia 31: vikidia 32: wekey 33: wesolve 34: wikia 35: wikibond 36: wikibooks 37: wikidata 38: wikimediachapter 39: wikinews 40: wikipedia 41: wikiquote 42: wikisource 43: wikitech 44: wikitravel 45: wikitravel_shared 46: wikiversity 47: wikivoyage 48: wiktionary 49: wowwiki Select family of sites we are working on (default: wikipedia): 27 <-- Note 2: use Test to start The language code of the site we're working on (default: 'en'): <-- Note 3: (hit return to accept the default) Username (en test):  <-- Note 4: your bot's username -- typically  Which variant of user_config.py: [S]mall or [E]xtended (with further information)? S <-- Note 5: Choose the Small version 'user-config.py' written. 'user-fixes.py' written.

Now you can begin configuring your bot.

Configuring
The  created by the "Small" option consists of 4 lines: family = 'test' mylang = 'en' usernames['test']['en'] = u'username-bot' You need to configure (customize) your bot before you can use it.
 * 1) -*- coding: utf-8  -*-

This can be done using the applications Text Edit or TextWranger, or the comnand line tools in a Terminal Window -- emacs or pico(nano).

If you choose to use Text Edit or TextWrangler, simply use the finder to navigate to , in the pywikipedia folder, and drag and drop that file on top of the appropriate icons.

If your language uses non-ASCII characters, make certain to retain the first line in the file:

Edit or add the following lines to  as appropriate for your Bot:

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

Remember to save.

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 creation is successful, you should now be able to run the bot.

With SUL it’s easy to run bots on several wikis. If you’ve created the bot account recently, the account is already merged.

If you created it some time ago and it’s not merged, or if it was created some time ago on other wikis, you’ll have to 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 command 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: python 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: python 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: python 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.