Overview

I had the opportunity to use the Wikibase API from a Python client, so this is a memo of the process.

I used the following library.

https://wikibase-api.readthedocs.io/en/latest/index.html

Installation

Install with the following:

!pip install wikibase-api

Read

This time, we will work with the following Wikibase instance.

https://nakamura196.wikibase.cloud/

from wikibase_api import Wikibase

api_url = "https://nakamura196.wikibase.cloud/w/api.php"
wb = Wikibase(api_url=api_url)

r = wb.entity.get("Q1")
print(r)

With the above, we were able to retrieve information about Q1.

Create

Obtaining Authentication Credentials

When creating items, authentication needed to be performed using one of the following methods:

  • Authentication using OAuth
  • Authentication with a user account

The latter involves creating Bot passwords, and the creation method is also introduced in the following article.

This time, we will try the former method.

Select “OAuth consumer registration” from “Special pages.”

There are two creation methods: “Request a token for a new OAuth 1.0a consumer.” and “Request a token for a new OAuth 2.0 client.” Select the former.

Fill in the fields.

For testing purposes, all items were checked.

As a result, a page like the following appears and authentication credentials can be obtained.

Execution

Create a .env file.

consumer_key=xxx
consumer_secret=yyy
access_token=zzz
access_secret=aaa

Load and set the authentication credentials using load_dotenv.

from wikibase_api import Wikibase
from dotenv import load_dotenv
import os

api_url = "https://nakamura196.wikibase.cloud/w/api.php"

load_dotenv(override=True)

oauth_credentials = {
    "consumer_key": os.getenv("consumer_key"),
    "consumer_secret": os.getenv("consumer_secret"),
    "access_token": os.getenv("access_token"),
    "access_secret": os.getenv("access_secret"),
}

wb = Wikibase(api_url=api_url, oauth_credentials=oauth_credentials)

You can create an empty item by executing the following:

r = wb.entity.add("item")

For example, you get a result like this:

{'entity': {'type': 'item',
  'id': 'Q49',
  'labels': {},
  'descriptions': {},
  'aliases': {},
  'claims': {},
  'sitelinks': {},
  'lastrevid': 128},
 'success': 1}

I plan to cover item editing in a separate article.

Deletion

When deleting, it was necessary to prefix with Item: or Property:.

wb.entity.remove("Item:Q49")

The following result was obtained and the page was deleted.

{'warnings': {'main': {'*': 'Unrecognized parameter: summary.'}},
'delete': {'title': 'Item:Q49',
 'reason': 'content was: "", and the only contributor was "[[Special:Contributions/Nakamura|Nakamura]]" ([[User talk:Nakamura|talk]])',
 'logid': 100}}

Let’s execute the following query.

wb.entity.search("japan", "en")

The following result was obtained.

{
	"warnings": {
		"main": {
			"*": "Unrecognized parameter: offset."
		}
	},
	"searchinfo": {
		"search": "japan"
	},
	"search": [
		{
			"id": "Q43",
			"title": "Item:Q43",
			"pageid": 84,
			"display": {
				"label": {
					"value": "Japan",
					"language": "en"
				},
				"description": {
					"value": "island country in East Asia",
					"language": "en"
				}
			},
			"repository": "local",
			"url": "https://nakamura196.wikibase.cloud/wiki/Item:Q43",
			"concepturi": "https://nakamura196.wikibase.cloud/entity/Q43",
			"label": "Japan",
			"description": "island country in East Asia",
			"match": {
				"type": "label",
				"language": "en",
				"text": "Japan"
			}
		}
	],
	"success": 1
}

Summary

I tried using the Wikibase API from a Python client. By connecting Wikibase and external systems through the API, it seems possible to use them in various convenient ways.

We hope this serves as a useful reference for others.