Wikidata:Pywikibot - Python 3 Tutorial/Setting sitelinks

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

Danger Zone edit

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 Example edit

You probably remember that we first need to call the site, the repo and then the item to look at a Wikidata-Item. We used print(dir(item)) to get a list of all the methods with which we can interact with the item. Just as a reminder here is the list:

['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', '_cache_attrs', '_cmpkey', '_cosmetic_changes_hook', '_defined_by', '_diff_to', '_getInternals', '_get_parsed_page', '_latest_cached_revision', '_link', '_namespace', '_normalizeData', '_normalizeLanguages', '_revisions', '_save', 'addClaim', 'applicable_protections', 'aslink', 'autoFormat', 'backlinks', 'botMayEdit', 'canBeEdited', 'categories', 'change_category', 'clear_cache', 'concept_uri', 'concept_url', 'content_model', 'contributingUsers', 'contributors', 'coordinates', 'create_short_link', 'data_item', 'data_repository', 'defaultsort', 'delete', 'depth', 'editAliases', 'editDescriptions', 'editEntity', 'editLabels', 'editTime', 'embeddedin', 'encoding', 'entity_type', 'exists', 'expand_text', 'extlinks', 'fromPage', 'from_entity_uri', 'fullVersionHistory', 'full_url', 'get', 'getCategoryRedirectTarget', 'getCreator', 'getDeletedRevision', 'getID', 'getLatestEditors', 'getMovedTarget', 'getOldVersion', 'getRedirectTarget', 'getReferences', 'getRestrictions', 'getSitelink', 'getTemplates', 'getVersionHistory', 'getVersionHistoryTable', 'getdbName', 'id', 'image_repository', 'imagelinks', 'interwiki', 'isAutoTitle', 'isCategory', 'isCategoryRedirect', 'isDisambig', 'isEmpty', 'isFlowPage', 'isImage', 'isIpEdit', 'isRedirectPage', 'isStaticRedirect', 'isTalkPage', 'is_categorypage', 'is_filepage', 'is_flow_page', 'is_valid_id', 'iterlanglinks', 'iterlinks', 'itertemplates', 'langlinks', 'lastNonBotUser', 'latestRevision', 'latest_revision', 'latest_revision_id', 'linkedPages', 'loadDeletedRevisions', 'markDeletedRevision', 'mergeInto', 'merge_history', 'move', 'moved_target', 'namespace', 'oldest_revision', 'pageAPInfo', 'page_image', 'pageid', 'permalink', 'preloadText', 'previousRevision', 'previous_revision_id', 'properties', 'protect', 'protection', 'purge', 'put', 'put_async', 'removeClaims', 'removeSitelink', 'removeSitelinks', 'repo', 'revision_count', 'revisions', 'save', 'section', 'sectionFreeTitle', 'setSitelink', 'setSitelinks', 'set_redirect_target', 'site', 'templates', 'text', 'title', 'titleForFilename', 'titleWithoutNamespace', 'title_pattern', 'toJSON', 'toggleTalkPage', 'touch', 'undelete', 'urlname', 'userName', 'version', 'watch']

As you can see, we are interested in getSitelink(), setSitelink() (and setSitelinks()), removeSitelink() (and removeSitelinks()) in this chapter. We will look at the whole example because it should be easy to figure out what the lines do. Then run the example (let it fail the first time to make sure your using test.wikidata):

# -*- coding: utf-8  -*-
import pywikibot
"""
To get the sitelink of a specific wiki. Use "item.getSitelink('INSERT DBNAME')"
"""
site = pywikibot.Site("wikidata", "wikidata")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, "Q42")

item.getSitelink('enwiki')

#Result in: 'Douglas Adams'

The item.getSitelink('enwiki') will get the title of dbname 'enwiki`; Douglas Adams, in this case.

Adding sitelink edit

Using dictionary edit

# -*- coding: utf-8  -*-
import pywikibot

site = pywikibot.Site("test", "wikidata")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, "Q210194")

sitedict = {'site':'enwiki', 'title':'Douglas Adams'}

item.setSitelink(sitedict, summary=u'Setting (/updating?) sitelink.')

Using Page edit

# -*- coding: utf-8  -*-
import pywikibot

#DISCLAIMER! This adds sitelink to wikidata.org, not the test site!
site = pywikibot.Site("en", "wikipedia")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, u"Q42")
page = pywikibot.Page(site, u"Douglas Adams")

item.setSitelink(page, summary=u'Setting (/updating?) sitelink.')

Removing sitelink edit

# -*- coding: utf-8  -*-
import pywikibot

site = pywikibot.Site("test", "wikidata")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, "Q210194")

item.removeSitelink('enwiki',summary=u'removing sitelink enwiki.') #Removes the sitelink connected to 'enwiki'. Can also be a Site object.

#use 'item.removeSitelinks(list)' to remove multiple sitelinks, 
#send a list with values either being Site objects, or dbNames. Like the adding example, but with list.

Using Site Obj. edit

# -*- coding: utf-8  -*-
import pywikibot

#DISCLAIMER! This remove sitelink from wikidata.org, not the test site!
site = pywikibot.Site("en", "wikipedia")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, u"Q42")

item.removeSitelink(site,summary=u'removing sitelink enwiki.')

Adding and removing sitelinks edit

# -*- coding: utf-8  -*-
import pywikibot

site = pywikibot.Site("test", "wikidata")
repo = site.data_repository()
item = pywikibot.ItemPage(repo, "Q210194")

data = [{'site':'enwiki', 'title':'Douglas Adams'}, 
        {'site':'nowiki', 'title':'Douglas Adams'}, 
        {'site':'dewiki', 'title':'Douglas Adams'}]

item.setSitelinks(data)

data2 = ['enwiki','nowiki','dewiki']
item.removeSitelink(data2)

Adding multiple sitelinks should be a list. Each item in the list can either be a Page object, or a dict with a value for 'site' and 'title'. Removing sitelinks should be a list, with values either being Site objects, or dbNames.