Wikidata:Pywikibot - Python 3 Tutorial/Labels

From Wikidata
Jump to navigation Jump to search

This chapter will introduce the concept of editing labels, descriptions and aliases.

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 (not 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 practive edits. If you copy-paste the examples and run them 2 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'}]]

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__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', '_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', 'content_model', 'contributingUsers', 'contributors', 'coordinates', 'data_item', 'data_repository', 'defaultsort', 'delete', 'editAliases', 'editDescriptions', 'editEntity', 'editLabels', 'editTime', 'embeddedin', 'encoding', 'exists', 'expand_text', 'extlinks', 'fromPage', '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_flow_page', 'iterlanglinks', 'iterlinks', 'itertemplates', 'langlinks', 'lastNonBotUser', 'latestRevision', 'latest_revision', 'latest_revision_id', 'linkedPages', 'loadDeletedRevisions', 'markDeletedRevision', 'mergeInto', 'move', 'moved_target', 'namespace', 'oldest_revision', 'pageAPInfo', 'permalink', 'preloadText', 'previousRevision', 'previous_revision_id', 'properties', 'protect', 'protection', 'purge', 'put', 'put_async', 'removeClaims', 'removeImage', 'removeSitelink', 'removeSitelinks', 'replaceImage', 'repo', 'revision_count', 'revisions', 'save', 'section', 'sectionFreeTitle', 'setSitelink', 'setSitelinks', 'set_redirect_target', 'site', 'templates', 'text', 'title', 'titleForFilename', 'titleWithoutNamespace', 'toJSON', 'toggleTalkPage', 'touch', 'undelete', 'urlname', 'userName', 'version', 'watch']

As you can see, we are interested in editLabels(), editDescriptions(), editAliases() 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):

import pywikibot

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

new_labels = {"en": "bear", "de": "Bär"}
new_descr = {"en": "gentle creature of the forrest", "de": "Friedlicher Waldbewohner"}
new_alias = {"en": ["brown bear", "grizzly bear", "polar bear"], "de": ["Braunbär", "Grizzlybär", "Eisbär"]}
item.editLabels(labels=new_labels, summary="Setting new labels.")
item.editDescriptions(new_descr, summary="Setting new descriptions.")
item.editAliases(new_alias, summary="Setting new aliases.")

As you can see from the output the bot waits a while during edits. Make sure that the edits are correct otherwise cancel the bot by pressing Ctrl + C.

Take some time to look at the edit history of the item. You will see that your bot edited the item 3 times and used our edit summaries (keyword summary=) for all languages. This can be confusing for humans that quickly scan the history what your bot did. In the next example we will improve the edit history.

Better Edit Summary[edit]

An easy way to give a little more detail about your edits in the edit history is to iterate over the dictionaries to customize the edit summary. The following example shows the concept with 3 for-loops, but you could try to write a function that works for all the different dictionaries.

import pywikibot

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

new_labels = {"en": "bear", "de": "Bär"}
new_descr = {"en": "gentle creature of the forrest",
             "de": "Friedlicher Waldbewohner"}
new_alias = {"en": ["brown bear", "grizzly bear", "polar bear"],
             "de": ["Braunbär", "Grizzlybär", "Eisbär"]}

for key in new_labels:
    item.editLabels(labels={key: new_labels[key]},
        summary="Setting label: {} = '{}'".format(key, new_labels[key]))

for key in new_descr:
    item.editDescriptions({key: new_descr[key]},
        summary="Setting description: {} = '{}'".format(key, new_descr[key]))

for key in new_alias:
    item.editAliases({key: new_alias[key]},
        summary="Settings aliases: {} = '{}'".format(key, new_alias[key]))

If you check the edit history again, you can see that "Setting label: {} = '{}'" appears for example as Setting label: de = 'Bär'.

Alternative: all in one edit[edit]

You can change also multiple labels, descriptions and aliases in a single edit.

Example:

import pywikibot

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

data = {"labels": {"en": "bear", "de": "Bär"},
  "descriptions": {"en": "gentle creature of the forrest", "de": "Friedlicher Waldbewohner"},
       "aliases": {"en": ["brown bear", "grizzly bear", "polar bear"], "de": ["Braunbär", "Grizzlybär", "Eisbär"]},
     "sitelinks": [{"site": "enwiki", "title": "Bear"}, {"site": "dewiki", "title": "Bär"}]}
item.editEntity(data, summary=u'Edited item: set labels, descriptions, aliases')

Make sure that the edits are correct otherwise cancel the bot by pressing Ctrl + C.

Looking at the item history, you will see that your bot edited the item only one time and changed multiple labels, descriptions and aliases with te specified edit summary (keyword summary=; one single summary for all changes made in this edit).