import requests
import pandas as pd
from SPARQLWrapper import SPARQLWrapper, JSON
import json
Set the SPARQL-Endpoint:
anno_lod_endpoint = "https://lod.onb.ac.at/sparql/anno"
Methods to query the endpoint and build the dataframe:
def get_sparql_result(service, query):
sparql = SPARQLWrapper(service)
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
return sparql.query()
def get_sparql_dataframe(service, query):
result = get_sparql_result(service, query)
processed_results = result.convert()
cols = processed_results['head']['vars']
out = []
for row in processed_results['results']['bindings']:
item = []
for c in cols:
item.append(row.get(c, {}).get('value'))
out.append(item)
return pd.DataFrame(out, columns=cols)
Select all newspapers and periodicals with subjectheading Statistik:
query = '''
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX edm: <http://www.europeana.eu/schemas/edm/>
PREFIX dcterms: <http://purl.org/dc/terms/>
SELECT ?title ?subjectURI ?manifest
WHERE {?subjectURI dc:subject <http://d-nb.info/gnd/4056995-0> .
?subjectURI dc:title ?title .
?subjectURI edm:isShownBy ?firstpage .
?subjectURI edm:rights <http://creativecommons.org/publicdomain/mark/1.0/> .
?firstpage dcterms:isReferencedBy ?manifest
}'''
Get list of IIIF Manifests URLs:
df = get_sparql_dataframe(anno_lod_endpoint, query)
manifests = list(df['manifest'])
manifests
Function to create a SACHA Collection (https://iiif.onb.ac.at/api#_collectionspostjsonprocessor):
def create_collection(description, list_of_manifest_ids_or_ids):
j = {
"description": description,
"elements": list_of_manifest_ids_or_ids
}
creation_link = 'https://iiif.onb.ac.at/presentation/collection'
result = requests.post(creation_link, json=j)
if result.status_code == 201:
print('SUCCESS: Create collection {}'.format(result.json()['url']))
print('View collection in Mirador: https://iiif.onb.ac.at/view/collection/mirador/' + result.json()['url'].split('/').pop())
elif result.status_code == 400:
print('ERROR: Request error creating collection')
print(result.text)
elif result.status_code == 500:
print('ERROR: Server error creating collection')
print(result.text)
else:
print('ERROR: General error creating collection, HTTP status = {}'.format(result.status_code))
Create the SACHA Collection:
create_collection("newspaper with subject heading Statistik", manifests)