5  Image from linked open data API

5.1 Image from linked open data API

5.1.1 Image - Siege III: The Fortress of Raab occupied by the Turks, 1594. Painting, https://wikibase.wbworkshop.tibwiki.io/wiki/Item:Q505

The below Python code experiments with retrieving data from Wikibase using the API. This takes approx. 18 seconds to run due to the size of the images.

from PIL import Image
import requests

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

def get_entity (entity_id):
    resourceUrl = '/w/api.php?action=wbgetentities&format=json&ids='+entity_id
    uri = endpoint_url + resourceUrl
    r = requests.get(uri)
    data = r.json()
    return data

media_data = get_entity(entity_id)
claims = media_data['entities'][entity_id]['claims']
for property, values in claims.items():
    if property == 'P22':
        for value in values:
            image_url = value['mainsnak']['datavalue']['value']
            im = Image.open(requests.get(image_url, stream=True).raw)
    elif property == 'P23':
        for value in values:
            entity_id = value['mainsnak']['datavalue']['value']['id']
            object_data = get_entity(entity_id)
print('English title: ', object_data['entities'][entity_id]['labels']['en']['value'])
display(im)
English title:  Siege III: The Fortress of Raab occupied by the Turks, 1594