6  Linked open data query from SPARQL

6.1 SPARQL query

The below Python code experiments with using SPARQLWrapper to retrieve data from NFDI4Culture’s Wikibase based on a SPARQL query.

from SPARQLWrapper import SPARQLWrapper, JSON
from PIL import Image
from ipypublish import nb_setup
import numpy as np
import pandas
import requests
import sparql_dataframe

def get_delay(date):
    try:
        date = datetime.datetime.strptime(date, '%a, %d %b %Y %H:%M:%S GMT')
        timeout = int((date - datetime.datetime.now()).total_seconds())
    except ValueError:
        timeout = int(date)
    return timeout

def get_image(url, headers):
    r = requests.get(url, headers=headers, stream=True)
    if r.status_code == 200:
        im = Image.open(r.raw)
        return im
    if r.status_code == 500:
        return None
    if r.status_code == 403:
        return None
    if r.status_code == 429:
        timeout = get_delay(r.headers['retry-after'])
        print('Timeout {} m {} s'.format(timeout // 60, timeout % 60))
        time.sleep(timeout)
        get_image(url, headers)

# Example using NFDI4Culture Wikibase

# Specify the NFDI4Culture Wikibase SPARQL endpoint
endpoint_url = 'https://query.wbworkshop.tibwiki.io/proxy/wdqs/bigdata/namespace/wdq/sparql'

# Query
query = """
SELECT ?item ?itemLabel ?creationDateStart ?creationDateEnd ?inscription ?materialLabel ?methodLabel ?description ?media
WHERE { 
    ?item tibt:P25 tib:Q60.
    ?item tibt:P79 ?creationDateStart.
    ?item tibt:P80 ?creationDateEnd.
    ?item tibt:P18 ?inscription.
    ?item tibt:P38 ?material.
    ?item tibt:P39 ?method.
    ?item tibt:P26 ?description.
    ?item tibt:P16 ?media
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} 
#All paintings from "Belagerungsszenen des Langen Türkenkriegs"
"""

dataframe = sparql_dataframe.get(endpoint_url, query, post=True)

dataframe

# Example using Wikidata

# Specify the Wikidata SPARQL endpoint
#endpoint_url = 'https://query.wikidata.org/bigdata/namespace/wdq/sparql'

#user_agent = 'cp4c_bot/0.0 (https://github.com/SimonXIX/cp4c; ad7588@coventry.ac.uk)'
#sparql = SPARQLWrapper(endpoint_url, agent=user_agent)

# query = """
# #Cats, with pictures
# #defaultView:ImageGrid
# SELECT ?item ?itemLabel ?pic
# WHERE
# {
# ?item wdt:P31 wd:Q146 .
# ?item wdt:P18 ?pic
# SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" }
# }
# LIMIT 2
# """

# Retrieve results and convert to JSON format
#sparql.setQuery(query)
#sparql.setReturnFormat(JSON)
#result = sparql.query().convert()

#for item in result['results']['bindings']:
    # image_url=item['pic']['value']
    # headers = {'User-Agent': 'cp4c_bot/0.0 (https://github.com/SimonXIX/cp4c; ad7588@coventry.ac.uk)'}
    # im = get_image(image_url, headers)

    # print('Cat name: ', item['itemLabel']['value'])
    # display(im)
item itemLabel creationDateStart creationDateEnd inscription materialLabel methodLabel description media
0 https://wikibase.wbworkshop.tibwiki.io/entity/... Siege VII: The city of Waitzen occupied by the... 1602-01-01T00:00:00Z 1604-01-01T00:00:00Z „Statt Waitzen wie die von vom Türcken beleger... Canvas Oil painting https://wikibase.wbworkshop.tibwiki.io/entity/... https://wikibase.wbworkshop.tibwiki.io/entity/...
1 https://wikibase.wbworkshop.tibwiki.io/entity/... Siege I: The Fortress of Tottis retaken by the... 1602-01-01T00:00:00Z 1604-01-01T00:00:00Z „Vestung Tottis, wie die von den Christen bei ... Canvas Oil painting https://wikibase.wbworkshop.tibwiki.io/entity/... https://wikibase.wbworkshop.tibwiki.io/entity/...
2 https://wikibase.wbworkshop.tibwiki.io/entity/... Siege II: The Fortress of Gran occupied by the... 1602-01-01T00:00:00Z 1604-01-01T00:00:00Z „Vestung Gran wie die von Christen belegert ge... Canvas Oil painting https://wikibase.wbworkshop.tibwiki.io/entity/... https://wikibase.wbworkshop.tibwiki.io/entity/...
3 https://wikibase.wbworkshop.tibwiki.io/entity/... Siege XII: The Fortress of Gran occupied by th... 1602-01-01T00:00:00Z 1604-01-01T00:00:00Z „Vestung Gran wie die vom Türcken belegert gew... Canvas Oil painting https://wikibase.wbworkshop.tibwiki.io/entity/... https://wikibase.wbworkshop.tibwiki.io/entity/...
4 https://wikibase.wbworkshop.tibwiki.io/entity/... Siege XI: The capital city Offen occupied by t... 1602-01-01T00:00:00Z 1604-01-01T00:00:00Z „Hauptstatt Offen, wie die von Christn beleger... Canvas Oil painting https://wikibase.wbworkshop.tibwiki.io/entity/... https://wikibase.wbworkshop.tibwiki.io/entity/...
5 https://wikibase.wbworkshop.tibwiki.io/entity/... Siege III: The Fortress of Raab occupied by th... 1602-01-01T00:00:00Z 1604-01-01T00:00:00Z „Vestung Raab, wie die vom Türcken belegert ge... Canvas Oil painting https://wikibase.wbworkshop.tibwiki.io/entity/... https://wikibase.wbworkshop.tibwiki.io/entity/...
6 https://wikibase.wbworkshop.tibwiki.io/entity/... Siege IV: The Fortress of Comorna occupied by ... 1602-01-01T00:00:00Z 1604-01-01T00:00:00Z „Vestung Comorna wie die vom Türckn belegert g... Canvas Oil painting https://wikibase.wbworkshop.tibwiki.io/entity/... https://wikibase.wbworkshop.tibwiki.io/entity/...
7 https://wikibase.wbworkshop.tibwiki.io/entity/... Siege V: The Fortress of Gran retaken by the C... 1602-01-01T00:00:00Z 1604-01-01T00:00:00Z „Vestung Gran wie die von den Christen wider e... Canvas Oil painting https://wikibase.wbworkshop.tibwiki.io/entity/... https://wikibase.wbworkshop.tibwiki.io/entity/...
8 https://wikibase.wbworkshop.tibwiki.io/entity/... Siege VI: The Fortress of Vizzegrad occupied b... 1602-01-01T00:00:00Z 1604-01-01T00:00:00Z “Vestung Vizzegrad wie die von Christen belege... Canvas Oil painting https://wikibase.wbworkshop.tibwiki.io/entity/... https://wikibase.wbworkshop.tibwiki.io/entity/...
9 https://wikibase.wbworkshop.tibwiki.io/entity/... Siege VIII: The Fortress of Raab, which was re... 1602-01-01T00:00:00Z 1604-01-01T00:00:00Z „Vestung Raab, die Christen beÿ der Nacht wide... Canvas Oil painting https://wikibase.wbworkshop.tibwiki.io/entity/... https://wikibase.wbworkshop.tibwiki.io/entity/...
10 https://wikibase.wbworkshop.tibwiki.io/entity/... Siege IX: The capital city Offen occupied by t... 1602-01-01T00:00:00Z 1604-01-01T00:00:00Z „Hauptstatt Offen. wie die von Christen belege... Canvas Oil painting https://wikibase.wbworkshop.tibwiki.io/entity/... https://wikibase.wbworkshop.tibwiki.io/entity/...
11 https://wikibase.wbworkshop.tibwiki.io/entity/... Siege X: The capital city Offen occupied by th... 1602-01-01T00:00:00Z 1604-01-01T00:00:00Z „Hauptstatt Offen, wie die von Christen belege... Canvas Oil painting https://wikibase.wbworkshop.tibwiki.io/entity/... https://wikibase.wbworkshop.tibwiki.io/entity/...