1

I am looking for a way to find out whether an item with a certain label and description already exists on Wikidata. This task should be performed by the Pywikibot. I don't want my Bot to create a new item if it already exists. So far, my code looks like this:

...                
def check_item_existence(self):
    transcript_file = self.transcript_file
    with open(transcript_file) as csvfile:
        transcript_dict = csv.DictReader(csvfile, delimiter="\t")
        for row in transcript_dict:
            site = pywikibot.Site("en", "TillsWiki")
            existing_item = pywikibot.ItemPage(site, row['Name'])
            title = existing_item.title()
svick
  • 236,525
  • 50
  • 385
  • 514
TIlls
  • 55
  • 5

1 Answers1

1

You can use the wbsearchentities api module from the Wikibase API. The code to check whether any item with specific English label exists in WikiData is:

from pywikibot.data import api
...
def wikiitemexists(label):
    params = {'action': 'wbsearchentities', 'format': 'json',
              'language': 'en', 'type': 'item', 'limit':1,
              'search': label}
    request = api.Request(site=acta_site, **params)
    result = request.submit()
    return True if len(result['search'])>0 else False

Notice that the labels in Wikidata are not unique and that API search for aliases as well.

Addshore
  • 568
  • 3
  • 16
david brick
  • 111
  • 1
  • 8