In [1]:

    import requests
    import pandas as pd
    from SPARQLWrapper import SPARQLWrapper, JSON
    import json

Set the SPARQL-Endpoint:

- https://lod.onb.ac.at/sparql/anno for ANNO
- https://lod.onb.ac.at/sparql/akon for AKON

In [2]:

    anno_lod_endpoint = "https://lod.onb.ac.at/sparql/anno"

Methods to query the endpoint and build the dataframe:

In [3]:

    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:

In [4]:

    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:

In [5]:

    df = get_sparql_dataframe(anno_lod_endpoint, query)
    manifests = list(df['manifest'])
    manifests

Out[5]:

    ['http://iiif.onb.ac.at/presentation/ANNO/stm1875ag0001/manifest',
     'http://iiif.onb.ac.at/presentation/ANNO/stm1876ag0001/manifest',
     'http://iiif.onb.ac.at/presentation/ANNO/stm1877ag0001/manifest',
     'http://iiif.onb.ac.at/presentation/ANNO/stm1878ag0001/manifest']

Function to create a SACHA Collection (https://iiif.onb.ac.at/api#_collectionspostjsonprocessor):

In [6]:

    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:

In [7]:

    create_collection("newspaper with subject heading Statistik", manifests)

    SUCCESS: Create collection https://iiif.onb.ac.at/presentation/collection/R9kE0IcrIE
    View collection in Mirador: https://iiif.onb.ac.at/view/collection/mirador/R9kE0IcrIE