9  Linked open data API testing

9.1 Linked open data API testing

This notebook experiments with querying linked open data in a Jupyter Notebook rendered through Quarto.

The Python code below queries the NFDI4Culture Wikibase at https://wikibase.wbworkshop.tibwiki.io/wiki/Main_Page and returns data based on the ID inputted by the user.

This executes in Jupyter Notebook which is able to run the Python code and provides a static output when saved in the Notebook. It cannot be executed dynamically in Quarto since the Quarto front-end does not support stdin input requests.

To reset the output, run ‘Kernel > Restart kernel and clear all outputs’.

9.2 Wikibase API testing

import requests

# Global variables
endpoint_url = 'https://wikibase.wbworkshop.tibwiki.io'
resource_url = '/w/api.php'

entity = input("What's the Q number (including the 'Q')? ")
print('Check out ' + endpoint_url + '/wiki/' + entity + ' to see the GUI.')
resourceUrl = '/w/api.php?action=wbgetclaims&format=json&entity='+entity
uri = endpoint_url + resourceUrl
r = requests.get(uri)
data = r.json()
claims = data['claims']
print('subject: ', entity)
print()
for property, values in claims.items():
    print('property: ', property)
    for value in values:
        try:
            # print Q ID if the value is an item
            print('item value: ', value['mainsnak']['datavalue']['value']['id'])
        except:
            try:
                # print the string value if the value is a literal
                print('literal value: ', value['mainsnak']['datavalue']['value'])
            except:
                # print the whole snak if the value is something else
                print('other value: ', value['mainsnak'])
    print()