-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsplit.py
executable file
·64 lines (51 loc) · 2.1 KB
/
split.py
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
#! /usr/bin/env python3
import json
from urllib import request
import os
import base64
def loadJson(url):
with request.urlopen(url) as data:
return json.loads(data.read())
def createAnnoPage():
annoPage = {
"@context": "http://iiif.io/api/presentation/3/context.json",
"type": "AnnotationPage",
"partOf": {
"id": "https://zooniverse.github.io/iiif-annotations/annotations/titles.json",
"type": "AnnotationCollection"
},
"items": []
}
return annoPage
if __name__ == "__main__":
base = "https://glenrobson.github.io/iiif_stuff/zooniverse/split"
annotations = loadJson('https://zooniverse.github.io/iiif-annotations/annotations/titles/0.json')
# split annotations in to one annotation page per canvas
annoMap = {}
for anno in annotations['items']:
canvas = anno['target'].split("#")[0]
if canvas not in annoMap:
annoMap[canvas] = createAnnoPage()
annoMap[canvas]['items'].append(anno)
# add links to annotation pages to correct canvas
manifest = loadJson('https://api.bl.uk/metadata/iiif/ark:/81055/vdc_100022589176.0x000002/manifest.json')
canvasNo = 0
for canvas in manifest['sequences'][0]['canvases']:
if canvas['@id'] in annoMap:
print ('Adding annotations to canvas no: {}'.format(canvasNo))
# Write out annotation page
filename = "annoPage-{}.json".format(canvasNo)
annoPage = annoMap[canvas['@id']]
annoPageURL = "{}/{}".format(base, filename)
annoPage['id'] = annoPageURL
with open(filename, 'w') as outfile:
json.dump(annoPage, outfile)
# Add link to manifest pointing to annotation page
canvas['annotations'] = [{
"id": annoPageURL, # add the URL to the annotation page to the manifest
"type": "AnnotationPage"
}]
canvasNo += 1
# Write out the manifest with annotation links to file
with open('manifest.json', 'w') as outfile:
json.dump(manifest, outfile)