Manual:Pywikibot/Pywikibot and PAWS

Using Pywikibot with PAWS

Pywikibot and PAWS

edit

In this tutorial you'll learn about how to start using Pywikibot using Python 3 notebooks or the terminal in PAWS.

See PAWS documentation on Wikitech for more information and tutorials.

Python 3 Notebook or Terminal?

edit

From your PAWS control panel, you have two options that make it possible for you to work with the Pywikibot library, Python 3 Notebooks and the terminal available in PAWS. This tutorial will cover the basics of each.

Before You Start

edit

Using Python 3 Notebook to Work with Pywikibot in PAWS

edit

The below steps will demonstrate how to perform a few simple tasks using Pywikibot in a Python 3 notebook in PAWS. No additional software or files are necessary.

Note: If you are following this tutorial, please use Test Wikipedia to ensure that you don't inadvertently make mistakes on your wiki.

Get Started With a Python 3 Notebook

edit
  1. Launch PAWS in your browser.
  2. Create a new Python 3 notebook from the control panel
     
    Creating a new Python 3 notebook in PAWS.
  3. Now the Pywikibot library can be imported. In the code cell, enter the following code then click run:
import pywikibot
  1. Next, you will need to connect Pywikibot to the wiki you want to work with. For our tutorial we will connect to the Test Wikipedia page. No login credentials are required because you have already logged into PAWS using OAUTH. To connect to the wiki you want to work with, you will need to create an APISite object that includes the language and family of your wiki. In the code cell, enter the following code then click run:
site = pywikibot.Site('test', 'wikipedia')
  1. You are now connected to the Test Wikipedia and can begin to perform basic tasks using Pywikibot.
  2. Later, to connect to a different wiki, you can use the code above and swap out the "language" and "family" parameters respectively. For example, to connect to the English Wikipedia, enter the following code in the code cell and click run:
site = pywikibot.Site('en', 'wikipedia')
site
  1. The code above should produce the following output.
APIsite("en", "wikipedia")

Some Basic Tasks That Can Be Performed Using Python 3 Notebooks

edit

Create a page

edit
site = pywikibot.Site('test', 'wikipedia')
page = pywikibot.Page(site, 'Test:Pegasus')
page.save('test edit')

This code will output the following when run:

Page [[Test:Pegasus]] saved

Fetch a Page

edit
page = pywikibot.Page(site, '<code>Page name</code>')

This will fetch a page called "Test:Pegasus" from the Test Wikipedia site.

After a page has been fetched, page.exists() can be typed into the cell and will output the following:

True

In the case where the page does not exist, false will be returned instead.

Add Text to a Page

edit

Pywikibot can be used to add text to a page as well.

page = pywikibot.Page(site, 'Test:Pegasus')
page.text = 'A pegasus is a flying horse.'
page.save('test edit')

This code will output the following (or similar):

Sleeping for 9.3 seconds, 2020-10-09 17:05:14
Page [[Test:Pegasus]] saved

The text of the page can then be obtained by running page.text in the cell. When run, page.text will provide the following output.

'A pegasus is a flying horse.'

The examples above run in separate cells, but multiple lines can be run at once. For example, the following code...

import pywikibot

site = pywikibot.Site('test', 'wikipedia')
page = pywikibot.Page(site, 'test')
page.text = 'Hello world!'
page.save('test edit')
page.text

will produce the following output (or similar):

Sleeping for 9.3 seconds, 2020-10-09 17:05:24
Page [[Test]] saved

'Hello world!'

Using a Terminal to work with Pywikibot in PAWS

edit

This tutorial demonstrates how to perform a simple task using Pywikibot in a terminal in PAWS.

Note: If you are following this tutorial, please use Test Wikipedia to ensure that you don't inadvertently make mistakes on your wiki.

Set up user-config.py

edit

When you are ready to work with a wiki, you'll need to connect it to Pywikibot by setting up a user-config.py file that connects Pywikibot to Test Wikipedia. You can explore more examples of user-config.py files to gain a better understanding of what they are and how to set them up. You may also want to look up the code for the language of the wiki you plan to work with.

The below steps set up a basic user-config.py file for working with PAWS.

  1. Launch PAWS in your browser
  1. Create a new text file from the control panel
     
    Creating a new Python 3 notebook in PAWS.
  1. Give the user-config.py a title.
  1. Add the code to indicate the language and family of your document, as well as the bot's username. See the code below.
mylang = 'test'
family = 'wikipedia'
usernames['wikipedia']['test'] = 'BOTNAME'
  1. Under the File tab click Save.

The user-config.py file should now be visible in the index files of the PAWS control panel. This file can be altered any time.

Note: When you are working with the terminal and PAWS and wish to use a different wiki, make sure to change your user-config.py to reflect this.

For example, if you want to work with the English Wikipedia, the user-config.py will include:

mylang = 'en'
family = 'wikipedia'

If you want to work with the Wikimedia Commons, the user-config.py will include:

mylang = 'commons'
family = 'commons'

You can find more information about the user-config.py file in the Pywikibot manual on Mediawiki.

Get Started with the PAWS Terminal

edit

Creating a new Terminal

edit
  1. Launch PAWS in your browser
  1. Create a new terminal from the control panel
     
    Creating a new Python 3 notebook in PAWS.
  1. You will be taken to a terminal.
     
    Screenshot of terminal welcome message.

If you wish to see the commands available, type ls /bin/ into the terminal, then hit Enter.

ls /bin/ will produce the following output.

bash*          date*           lessecho*       pwd*         uname*
bunzip2*       dd*             lessfile@       rbash@       uncompress*
bzcat*         df*             lesskey*        readlink*    vdir*
bzcmp@         dir*            lesspipe*       rm*          wdctl*
bzdiff*        dmesg*          ln*             rmdir*       which*
bzegrep@       dnsdomainname@  login*          rnano@       ypdomainname@
bzexe*         domainname@     ls*             run-parts*   zcat*
bzfgrep@       echo*           lsblk*          sed*         zcmp*
bzgrep*        egrep*          mkdir*          sh@          zdiff*
bzip2*         false*          mknod*          sh.distrib@  zegrep*
bzip2recover*  fgrep*          mktemp*         sleep*       zfgrep*
bzless@        findmnt*        more*           stty*        zforce*
bzmore*        grep*           mount*          su*          zgrep*
cat*           gunzip*         mountpoint*     sync*        zless*
chgrp*         gzexe*          mv*             tar*         zmore*
chmod*         gzip*           nano*           tempfile*    znew*
chown*         hostname*       nisdomainname@  touch*
cp*            kill*           pidof@          true*
dash*          less*           ps*             umount*

Log in to Test Wikipedia

edit
  1. Type the following text into the terminal. Login credentials are not necessary because you have already logged into PAWS using OAUTH.
pwb.py login
  1. The terminal should now indicate that you are logged in to Test Wikipedia.
     
    Screenshot of terminal after successful login to test Wikipedia.

Some Basic Tasks That Can Be Performed Using the PAWS Terminal

edit
Create a Page
edit

The following example will create a personal User Talk page on the Test Wiki.

  1. Type the following into the terminal, replacing <username> with your own username (created above).
pwb.py add_text -up -talk -page:"User talk:<username>" -text:"Hello. ~~~~"
  1. The code will produce the following output in the terminal. An option to accept the changes is provided.
     
    Screenshot of terminal after successful page creation on test Wikipedia.
Fetch a Page
edit

The following example will fetch a page from the Test Wiki and save it to your PAWS control panel.

  1. Type the following into the terminal:
pwb.py listpages -page:"Test:Pegasus" -save
  1. The code will produce the following output in the terminal:
     
    Screenshot of successful page fetch of "Pegasus" on test Wikipedia.
  1. Once the image has been created, it will appear in the PAWS control panel as a .txt file.
     
    PAWS control panel file list.
  1. The page contents can be inspected by opening the .txt file.
     
    Contents of the test page "Pegasus".

Explore Pywikibot Scripts

edit

This tutorial covers basic functions that can be performed with Pywikibot in PAWS. A list of more complex scripts can be found at:

Pywikibot and Wikidata

edit

Many people use Pywikibot to work with Wikidata. To see more about this topic, see the Wikimedia section of the Pywikibot manual on this wiki and the Pywikibot - Python 3 Tutorial on Wikidata.

Other Documentation

edit

Example Notebooks

edit
edit