Loading 3.3 - IIIF collection from SPARQL.ipynb +10 −2 Original line number Diff line number Diff line %% Cell type:code id: tags: ``` python import requests import pandas as pd from SPARQLWrapper import SPARQLWrapper, JSON import json ``` %% Cell type:markdown id: tags: Set the SPARQL-Endpoint: * https://lod.onb.ac.at/sparql/anno for ANNO * https://lod.onb.ac.at/sparql/akon for AKON %% Cell type:code id: tags: ``` python anno_lod_endpoint = "https://lod.onb.ac.at/sparql/anno" ``` %% Cell type:markdown id: tags: Methods to query the endpoint and build the dataframe: %% Cell type:code id: tags: ``` python 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) ``` %% Cell type:markdown id: tags: Select all newspapers and periodicals with subjectheading Statistik: %% Cell type:code id: tags: ``` python 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 }''' ``` %% Cell type:markdown id: tags: Get list of IIIF Manifests URLs: %% Cell type:code id: tags: ``` python df = get_sparql_dataframe(anno_lod_endpoint, query) manifests = list(df['manifest']) manifests ``` %% Output ['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/stm1877ag0001/manifest', 'http://iiif.onb.ac.at/presentation/ANNO/stm1878ag0001/manifest'] %% Cell type:markdown id: tags: Function to create a SACHA Collection (https://iiif.onb.ac.at/api#_collectionspostjsonprocessor): %% Cell type:code id: tags: ``` python 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)) ``` %% Cell type:markdown id: tags: Create the SACHA Collection: %% Cell type:code id: tags: ``` python create_collection("newspaper with subject heading Statistik", manifests) ``` %% Output SUCCESS: Create collection https://iiif.onb.ac.at/presentation/collection/8hIOHDd7hW SUCCESS: Create collection https://iiif.onb.ac.at/presentation/collection/R9kE0IcrIE View collection in Mirador: https://iiif.onb.ac.at/view/collection/mirador/R9kE0IcrIE %% Cell type:code id: tags: ``` python ``` Loading
3.3 - IIIF collection from SPARQL.ipynb +10 −2 Original line number Diff line number Diff line %% Cell type:code id: tags: ``` python import requests import pandas as pd from SPARQLWrapper import SPARQLWrapper, JSON import json ``` %% Cell type:markdown id: tags: Set the SPARQL-Endpoint: * https://lod.onb.ac.at/sparql/anno for ANNO * https://lod.onb.ac.at/sparql/akon for AKON %% Cell type:code id: tags: ``` python anno_lod_endpoint = "https://lod.onb.ac.at/sparql/anno" ``` %% Cell type:markdown id: tags: Methods to query the endpoint and build the dataframe: %% Cell type:code id: tags: ``` python 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) ``` %% Cell type:markdown id: tags: Select all newspapers and periodicals with subjectheading Statistik: %% Cell type:code id: tags: ``` python 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 }''' ``` %% Cell type:markdown id: tags: Get list of IIIF Manifests URLs: %% Cell type:code id: tags: ``` python df = get_sparql_dataframe(anno_lod_endpoint, query) manifests = list(df['manifest']) manifests ``` %% Output ['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/stm1877ag0001/manifest', 'http://iiif.onb.ac.at/presentation/ANNO/stm1878ag0001/manifest'] %% Cell type:markdown id: tags: Function to create a SACHA Collection (https://iiif.onb.ac.at/api#_collectionspostjsonprocessor): %% Cell type:code id: tags: ``` python 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)) ``` %% Cell type:markdown id: tags: Create the SACHA Collection: %% Cell type:code id: tags: ``` python create_collection("newspaper with subject heading Statistik", manifests) ``` %% Output SUCCESS: Create collection https://iiif.onb.ac.at/presentation/collection/8hIOHDd7hW SUCCESS: Create collection https://iiif.onb.ac.at/presentation/collection/R9kE0IcrIE View collection in Mirador: https://iiif.onb.ac.at/view/collection/mirador/R9kE0IcrIE %% Cell type:code id: tags: ``` python ```