Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
{
"cells": [
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import pandas as pd\n",
"from SPARQLWrapper import SPARQLWrapper, JSON\n",
"import json"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set the SPARQL-Endpoint:\n",
"* https://lod.onb.ac.at/sparql/anno for ANNO\n",
"* https://lod.onb.ac.at/sparql/akon for AKON"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"anno_lod_endpoint = \"https://lod.onb.ac.at/sparql/anno\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Methods to query the endpoint and build the dataframe:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def get_sparql_result(service, query):\n",
" sparql = SPARQLWrapper(service)\n",
" sparql.setQuery(query)\n",
" sparql.setReturnFormat(JSON)\n",
" return sparql.query()\n",
"\n",
"def get_sparql_dataframe(service, query):\n",
" result = get_sparql_result(service, query)\n",
"\n",
" processed_results = result.convert()\n",
" cols = processed_results['head']['vars']\n",
"\n",
" out = []\n",
" for row in processed_results['results']['bindings']:\n",
" item = []\n",
" for c in cols:\n",
" item.append(row.get(c, {}).get('value'))\n",
" out.append(item)\n",
"\n",
" return pd.DataFrame(out, columns=cols)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Select all newspapers and periodicals with subjectheading Statistik:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"query = '''\n",
"PREFIX dc: <http://purl.org/dc/elements/1.1/>\n",
"PREFIX edm: <http://www.europeana.eu/schemas/edm/>\n",
"PREFIX dcterms: <http://purl.org/dc/terms/>\n",
"SELECT ?title ?subjectURI ?manifest \n",
"WHERE {?subjectURI dc:subject <http://d-nb.info/gnd/4056995-0> .\n",
" ?subjectURI dc:title ?title .\n",
" ?subjectURI edm:isShownBy ?firstpage .\n",
" ?subjectURI edm:rights <http://creativecommons.org/publicdomain/mark/1.0/> .\n",
" ?firstpage dcterms:isReferencedBy ?manifest\n",
"}'''"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Get list of IIIF Manifests URLs:"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['http://iiif.onb.ac.at/presentation/ANNO/stm1875ag0001/manifest',\n",
" 'http://iiif.onb.ac.at/presentation/ANNO/stm1876ag0001/manifest',\n",
" 'http://iiif.onb.ac.at/presentation/ANNO/stm1877ag0001/manifest']"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = get_sparql_dataframe(anno_lod_endpoint, query)\n",
"manifests = list(df['manifest'])\n",
"manifests"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Function to create a SACHA Collection (https://iiif.onb.ac.at/api#_collectionspostjsonprocessor):"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"def create_collection(description, list_of_manifest_ids_or_ids):\n",
" j = {\n",
" \"description\": description,\n",
" \"elements\": list_of_manifest_ids_or_ids\n",
" }\n",
" creation_link = 'https://iiif.onb.ac.at/presentation/collection'\n",
" result = requests.post(creation_link, json=j)\n",
" if result.status_code == 201:\n",
" print('SUCCESS: Create collection {}'.format(result.json()['url']))\n",
" elif result.status_code == 400:\n",
" print('ERROR: Request error creating collection')\n",
" print(result.text)\n",
" elif result.status_code == 500:\n",
" print('ERROR: Server error creating collection')\n",
" print(result.text)\n",
" else:\n",
" print('ERROR: General error creating collection, HTTP status = {}'.format(result.status_code))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create the SACHA Collection:"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SUCCESS: Create collection https://iiif.onb.ac.at/presentation/collection/8hIOHDd7hW\n"
]
}
],
"source": [
"create_collection(\"newspaper with subject heading Statistik\", manifests)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}