Manual:Pywikibot/Draft:Pywikibot and PAWS

Using Pywikibot with PAWS

Pywikibot and PAWS
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?
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

 * Determine whether PAWS is right for your project.
 * Sign up for a Wikipedia account.
 * Note: Wikipedia accounts now work on all of Wikipedia's sister sites (Wikimedia, Wikitech, etc.) meaning a single account can be used.
 * Read the Getting Started with PAWS tutorial.
 * Use Test Wikipedia to test scripts before running them on live wikis.
 * (For notebook users) Ensure that the cells of your notebook will run in the correct order.
 * Set up a  file if you are using Pywikibot from the terminal.

Using Python 3 Notebook to Work with Pywikibot in PAWS
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

 * 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.png
 * 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   and   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")

Create a page
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
page = pywikibot.Page(site, ' ')

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

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

True

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

Add Text to a Page
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  in the cell. When run,  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
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
When you are ready to work with a wiki, you'll need to connect it to Pywikibot by setting up a  file that connects Pywikibot to Test Wikipedia. You can explore more examples of  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  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.png


 * 1) Give the   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  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  to reflect this.

For example, if you want to work with the English Wikipedia, the  will include:

mylang = 'en' family = 'wikipedia'

If you want to work with the Wikimedia Commons, the  will include:

mylang = 'commons' family = 'commons'

You can find more information about the  file in the Pywikibot manual on Mediawiki.

Creating a new Terminal

 * 1) Launch PAWS in your browser


 * 1) Create a new terminal from the control panel Creating a New Python 3 Notebook in PAWS.png


 * 1) You will be taken to a terminal. Terminal Welcome.jpg

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

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

 * 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. Terminal Login.png

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


 * 1) Type the following into the terminal, replacing   with your own username (created above).

pwb.py add_text -up -talk -page:"User talk: " -text:"Hello. ~"


 * 1) The code will produce the following output in the terminal. An option to accept the changes is provided.Terminal Page Creation.jpg

Fetch a Page
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:" " -save


 * 1) The code will produce the following output in the terminal: Terminal Page Fetch.jpg


 * 1) Once the image has been created, it will appear in the PAWS control panel as a .txt file. Control Panel Files.png


 * 1) The page contents can be inspected by opening the .txt file. Test Page Contents.png</li>

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


 * Global bot scripts
 * Scripts package

Pywikibot and Wikidata
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

 * Pywikibot - Documentation for Pywikibot is located at the Pywikibot home page Pywikibot home page on MediaWiki
 * PAWS - Documentation for PAWS is located at the PAWS home page on Wikitech.

Example Notebooks

 * Add Copyright to Items in Wikidata
 * Add copyright, creator to items in Wikidata
 * Add awards to Wikidata category Sports Hall of Fame
 * Add references to items already in Wikidata
 * Auto Wikiproject
 * Add short descriptions to biographies on Wikipedia EN
 * Add items to Wikidata
 * Change qualifier in P39 statements - Wikidata
 * Make changes to pages using MyPySQL and Pywikibot - HY Wikipedia
 * Remove broken files
 * Investigate bot issues
 * Policy changes - ZH Wikipedia
 * Teahouse archives answers
 * Analyze number of new editors per month
 * Categorize images after the end of Wiki Loves Love
 * Clean history merge list - Wikiproject history
 * Fix BDA Ids on Wikidata
 * Fix titles on Wikidata
 * Get articles without images
 * Global replace in Wikipedia DE
 * Get files with NASA image template - Commons
 * Remove redirect class
 * Check userpage authorship - RU Wikipedia
 * Fix bad interwiki links
 * Upload text
 * Parse data from talk pages
 * Add a property to a category - Wikidata
 * Autostatus update for Wikiproject
 * Batch delete and unlink images
 * Identify unhelpful file names on Commons
 * Bulk deprecate a template
 * Bulk deprecate an index parameter
 * Add statements to candidates in Canada elections - Wikidata
 * Move all pages from one subcategory to another
 * Create new user pages
 * Redirect a talk page
 * Relicense uploads to Wikimedia Commons
 * Replace page text

Other Links

 * See Pywikibot Communication for links to Pywikibot discussion groups and lists.