Wikidata:Pywikibot - Python 3 Tutorial/Setting sources

This chapter will introduce the concept of adding, removing and modifying sources.

Danger ZoneEdit

Make sure before running the examples that the bot is editing test.wikidata.org.

Check your user-config.py

family = 'wikidata'
mylang = 'test'
usernames['wikidata']['test'] = u'YOUR_BOT_OR_USER_NAME'

Check your login (note the wikidata:test):

$ python3 pwb.py login
Logged in on wikidata:test as YOUR_BOT_OR_USER_NAME.

Then make sure that each script calls the test-site:

site = pywikibot.Site("test", "wikidata")

All examples use the Wikidata Sandbox (Q4115189) item to further prevent accidental vandalism to Wikidata-proper by people copy-pasting code. Because the item does not exist on test.wikidata you can just create a new item (https://test.wikidata.org/wiki/Special:NewItem) for your practice edits. If you copy-paste the examples and run them, two things might happen: You will edit Wikidata Sandbox (Q4115189) or you will see an error message of test.wikidata that tells you that the item does not exist on that site.

pywikibot.data.api.APIError: no-such-entity: Could not find such an entity (Can't access entity Q4115189, revision may have been deleted.) [help:See https://test.wikidata.org/w/api.php for API usage; messages:[{'parameters': [], 'html': {'*': 'Could not find such an entity'}, 'name': 'wikibase-api-no-such-entity'}]]

ID's of properties and values do differ between test and live, so unexpected errors (like: ValueError: wikidata:test:Q101352 is not type <class 'str'>.) may arise if you use property and qualifier ID's from live.

Simple ExampleEdit

# -*- coding: utf-8  -*-
import pywikibot
import time
"""
Adding sources to newly-created claim/statement
"""
site = pywikibot.Site("test", "wikidata")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, "Q210194")

#CLAIM
claim = pywikibot.Claim(repo, u'P131') #Adding located in the administrative territorial entity (P131)
target = pywikibot.ItemPage(repo, u"Q4546") #Connecting P131 with Cambridge (Q350), that is a Q-id.
claim.setTarget(target) #Set the target value in the local object.
item.addClaim(claim, summary=u'Adding claim to Q210194') #Inserting value with summary to Q210194

#ADDS TWO REFERENCES
#FIRST REF.
today = date.today() #Date today
ref = pywikibot.Claim(repo, u'P248') #stated in (P248)
ref.setTarget(pywikibot.ItemPage(repo, 'Q648625')) #Connecting P248 with Google Knowledge Graph (Q648625), that is a Q-id. example stated in -> Google Knowledge Graph).

#SECOND REF.
retrieved = pywikibot.Claim(repo, u'P813') #retrieved (P813). Data type: Point in time
dateCre = pywikibot.WbTime(year=int(today.strftime("%Y")), month=int(today.strftime("%m")), day=int(today.strftime("%d"))) #retrieved -> %DATE TODAY%. Example retrieved -> 29.11.2020
retrieved.setTarget(dateCre) #Inserting value

claim.addSources([ref, retrieved], summary=u'Adding sources to administrative territorial entity (P131).')

Simple example on how to use addSources() on a newly-created claim/statement. Make your own tests to make sure that it is added correctly.

Adding sources to existing statements/claimsEdit

UNDER CONSTRUCTION

Remove sources from statements/claimsEdit

UNDER CONSTRUCTION