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:

!pipinstallwikibase-api

Read

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

https://nakamura196.wikibase.cloud/

fawrprpbroi=im_=nuwtwrWb(ili.rkke)i=inbbta"aishstetey_t(.apagpspei:it/_(i/u"mnrQpal1ok="raa)tmpuiWr_iauk1ri9lb6)a.sweikibase.cloud/w/api.php"

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.

ccaaooccnnccsseeuussmmssee__rrts__oekskceeerycne=r=txez=xtzax=zayayy

Load and set the authentication credentials using load_dotenv.

ffialo}wrrmpoaboopiaummo_dt=ru_h""""wdtrd_ccaaWiolocoocciktotrnncckies=eesseeibnnduussbav"vemmssash(nee__seitotrrtse_mtvi__oe(appeakskcaposrleeerpir:rsycneit/i"r"t_i/d=:e:"umlnet:rpoa={o"oloakTs:so=rdar..sat_mugog.pdueesegiWor)t.te_itaegetuke1nenerin9vtvnlbv6(e(v,a."n"(swcva"oeio(caakn"ccuiscectbuosehamnss_ses_scerut_r._moseckekedlereceoy_nrnu"s"etd)e)ti/,c,"awr)l/e,sat=p"oi)a.,upthhp_"credentials)

You can create an empty item by executing the following:

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

For example, you get a result like this:

{''e'''''''snildacslutdaelliaci'bsiatsct:ecaieteylrsmlrs''siesies:Q'ps'nv'4:t':ki:{9i:sd''{o{''1t,}n{}::}y,s},p',{1e:}2',8:{}},',item',

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.

{''d''werlaleoreagntsiieodn'n'g:':s:'{1:''0tc0{io}'tn}mltaeei'nn:t':'wIa{ts'e:*m':":Q"4,'9U'an,nrdectohgeniozneldypcaornatmreitbeurt:orsuwmamsar"y[.['S}p}e,cial:Contributions/Nakamura|Nakamura]]"([[Usertalk:Nakamura|talk]])',

Let’s execute the following query.

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

The following result was obtained.

{}"}"}"]"w"},s",s{},sam"ese""""}""""""}ura*aeaitpd"}"},rucldm"""cni"rardiail"",d""eroaeatltcin:crc"tgsavlevlplnbstyaeen"hch:lepbaasaao"ceccpnxsg:"ih"eilelnclns:elrhegtssUn":""dalugrugip"i""u"""{nf:Q:"y"euieut"t:p::a:::ro[4:":"ap"aohutge""3"::gt:grtr"i{"e"1{c:j"I8{eieytiJol"Joa,t4{""o"""p"ana:ag{pe,J:ni::s:p"bpnama"s:a:e"ain:p":l""/"nlenz"Qaeaelh"""n"e4nn{nnnt,i,"d3""d"cats,",akplp,clasaao"m:nru,u/danr/mtancer1aoty9kue6anri.mt:nwuriryoEkafai1ifsb9nsta6es.EtAewa.s.is"icktali"obA,uasdsi/eaw."ic,kliuIdt/eemn:tQi4t3y"/,Q43",

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.