TIlls
TIlls

Reputation: 75

Check whether an item with a certain label and description already exists on Wikidata by Pywikibot

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()

Upvotes: 1

Views: 455

Answers (1)

david brick
david brick

Reputation: 123

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.

Upvotes: 1

Related Questions