From charlesreid1

Covers how to configure Pywikibot to talk to third-party wiki sites.

Directory Structure

The directory structure for your pywikibot script should be a top-level directory containing your pywikibot script, and a copy of the pywikibot repository.

For example, a bot named my_bot would be set up as follows (note that core_stable is a copy of the pywikibot core repository):

my_bot/
    my_bot_script.py
    user-config.py
    core_stable/
        pwb.py
        generate_*.py
        ...

IMPORTANT:

When running scripts like my_bot_script.py, run them from the my_bot/ directory.

When generating configuration files or setting up the wiki login credentials, run scripts from the core_stable/ pywikibot repo directory.

Create MediaWiki Bot User

You will need to create credentials that the bot can use to make edits.

Go to Special:BotPasswords to create a bot password.

MediaWiki Documentation: https://www.mediawiki.org/wiki/Manual:Pywikibot/BotPasswords

Create Family File

In order to run Pywikibot on a third-party wiki, you have to tell your wiki how to interact with the third-party wiki. To do that, you need a family file.

Family files can be generated using the generate_family_file.py script. Run the script from the core_stable/ directory.

py generate_family_file.py

This will write a family file in my_bot/core_stable/pywikibot/families:

Writing /path/to/my_bot/core_stable/pywikibot/families/mythirdpartywiki_family.py...

Set Up Login Credentials

Next step is to store credentials for the bot account we created.

Go to the core_stable/ directory and use the generate_user_files.py script:

py generate_user_files.py

This will prompt for credentials, then generate some files:

BotPassword's "bot name" for Ch4zm: Ch4zmBat
BotPassword's "password" for "Ch4zmBat" (no characters will be shown):
'/Users/charles/golly/wiki-article-generator/bot/core_stable/user-config.py' written.
'/Users/charles/golly/wiki-article-generator/bot/core_stable/user-password.py' written.

Use Credentials to Log In

Now that we have stored the credentials, use them to log in and set up a bot session that pywikibot can use.

Go to core_stable/ directory and use the provided login script to log in:

$ py pwb.py scripts/login.py
WARNING: No user is logged in on site gollywiki:en
Logging in to gollywiki:en as Ch4zm@Ch4zmBat
Logged in on gollywiki:en as Ch4zm.

Where Credentials Are Stored

The #Set Up Login Credentials step prompted for MediaWiki bot credentials. Those credentials are stored in the file user-password.py in the pywikibot repository directory.

The contents of the file look like this:

$ cat orig_core_stable/user-password.py
# This is an automatically generated file used to store
# BotPasswords.
#
# As a simpler (but less secure) alternative to OAuth, MediaWiki allows bot
# users to uses BotPasswords to limit the permissions given to a bot.
# When using BotPasswords, each instance gets keys. This combination can only
# access the API, not the normal web interface.
#
# See https://www.mediawiki.org/wiki/Manual:Pywikibot/BotPasswords for more
# information.
('YourUserAccount', BotPassword('YourBotAccount', 'bot_password'))

There is also an auto-generated user-config.py file that stores configuration details for the wiki.


Template:PywikibotFlag