-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.py
196 lines (160 loc) · 9.78 KB
/
manifest.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
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
# this script takes a binary MARC file and a folder full of JP2s to produce a IIIF-compliant JSON manifest
# usage: |manifest.py records.mrc hdl_passwd| where records.mrc is a binary marc file containing bibliographic records
# for the items for which you're creating manifests and hdl_passwd is the Handle password. This script must be run from
# within a folder containing all of the relevant images formatted as JP2s, and the images must also be uploaded to the
# library IIIF server before running the script.
import json
import os
from datetime import datetime
from pymarc import MARCReader, Field
import requests
import sys
def main():
check_requirements()
hdl_create_statements = []
hdl_passwd = sys.argv[2]
with open(sys.argv[1], 'rb') as bibs:
reader = MARCReader(bibs)
# initial for-loop lets you process a collection with multiple records if necessary
for record in reader:
# gather file / record identifiers
long_identifier = str(record['001'])
identifier = long_identifier[6:len(long_identifier)]
# create a list of jp2s in the jp2 directory that contain the identifier for the record in question
# this list gets passed into the json building functions.
file_list = []
for file in os.listdir('jp2s/'):
if identifier in file:
file_list.append(file)
# this list will be unordered, so sort it
file_list.sort()
# figure out which library it's from and route it to the proper function
if record['510'] and "BCLL RBR" in record['510']['a']:
manifest = build_law_metadata(file_list, record, identifier)
else:
manifest = build_burns_metadata(file_list, record, identifier)
# add if statement to look for cartographic records, determined by an 'e' in the 6th position of the LDR
write_manifest_file(identifier, manifest)
view = build_view(identifier, record)
write_view_file(identifier, view)
hdl_create_statements.append(build_handles(identifier, hdl_passwd))
bibs.close()
write_hdl_batchfile(hdl_create_statements)
def write_view_file(identifier, view):
view_file = open('view/' + identifier, 'w')
view_file.write(view)
view_file.close()
def write_manifest_file(identifier, manifest):
manifest_file = open('manifests/' + identifier + '.json', 'w')
manifest_file.write(json.dumps(manifest))
manifest_file.close()
def write_hdl_batchfile(hdl_create_statements):
hdl_file_title = datetime.now().strftime("%m-%d-%Y-%H-%M-%S")
hdl_out = open('hdl/handles-' + hdl_file_title + '.txt', 'w')
all_hdl_create_statements = '\n'.join(hdl_create_statements)
hdl_out.write(all_hdl_create_statements)
hdl_out.close()
def build_law_metadata(file_list, record, identifier):
# gather metadata
title = record.title()
attribution = "Though the copyright interests have not been transferred to Boston College, all of the items in the " \
"collection are in the public domain."
publication_year = record.pubyear()
date = publication_year if publication_year is not None else ''
room = record['510']['a']
if record['510']['c'] is not None:
room = str(record['510']['a']) + " " + str(record['510']['c'])
citation = str(title) + ", " + str(date) + ", " + room + ", Daniel R. Coquillette Rare Book Room, Boston College Law Library, https://hdl.handle.net/2345.2/" + identifier + "."
# build all the json
blob = {'@context': 'https://iiif.io/api/presentation/2/context.json', '@id': 'https://library.bc.edu/iiif/manifests/'
+ identifier + '.json', '@type':'sc:Manifest', 'label':str(title), 'thumbnail':'https://iiif.bc.edu/' + identifier
+ '_0001.jp2/full/!200,200/0/default.jpg', 'viewingHint':'paged', 'attribution':attribution, 'metadata':[
{'handle': 'https://hdl.handle.net/2345.2/' + identifier},{'label':'Preferred Citation', 'value':citation}],
'sequences': build_sequence(file_list), 'structures':build_structures(file_list)}
return blob
def build_burns_metadata(file_list, record, identifier):
# gather metadata
title = record.title()
attribution = "Though the copyright interests have not been transferred to Boston College, all of the items in the " \
"collection are in the public domain."
publication_year = record.pubyear()
date = publication_year if publication_year is not None else ''
citation = title + ", " + date + ", " + ", John J. Burns Library, Boston College, https://hdl.handle.net/2345.2/" \
+ identifier + "."
# build all the json
blob = {'@context': 'https://iiif.io/api/presentation/2/context.json',
'@id': 'https://library.bc.edu/iiif/manifests/'
+ identifier + '.json', '@type': 'sc:Manifest', 'label': title,
'thumbnail': 'https://iiif.bc.edu/' + identifier
+ '_0001.jp2/full/!200,200/0/default.jpg', 'viewingHint': 'paged', 'attribution': attribution,
'metadata': [
{'handle': '"https://hdl.handle.net/2345.2/' + identifier},
{'label': 'Preferred Citation', 'value': citation}],
'sequences': build_sequence(file_list), 'structures': build_structures(file_list)}
return blob
# eventually need to add a new function, build_cartographic_metadata. Information on metadata formatting to be provided
# by Burns Public Services.
def build_sequence(file_list):
sequence = [{'@type':'sc:Sequence', 'canvases':[]}]
for file in file_list:
short_name = file[0:file.index('.')]
counter = short_name[len(short_name)-4:len(short_name)]
cui = short_name[0:len(short_name)-5]
call = requests.get('https://iiif.bc.edu/' + file + '/info.json')
info = call.json()
try:
height = info['height']
width = info['width']
blob = {'@id':'https://iiif.bc.edu/' + cui + '/canvas/' + counter, '@type':'sc:Canvas', 'label':short_name, 'width': width,
'height':height, 'images':[{'@id':'https://iiif.bc.edu/' + cui + '/' + counter + '/annotation/1',
'@type':'oa:Annotation', 'on':'https://iiif.bc.edu/' + cui + '/canvas/' + counter, 'motivation':'sc:painting',
'resource':{'@id':'https://iiif.bc.edu/' + file + '/full/full/0/default.jpg', '@type':'dctypes:Image',
'format':'image/jpeg', 'width':width, 'height':height, 'service':{'@context':'https://iiif.io/api/image/2/context.json',
'@id':'https://iiif.bc.edu/' + file, 'profile':'https://iiif.io/api/image/2/level2.json'}}}]}
sequence[0]['canvases'].append(blob)
except KeyError:
print(file + ' is not on the server or the server is otherwise not responding as expected.')
continue
return sequence
def build_structures(file_list):
structures = []
index = 0
for file in file_list:
short_name = file[0:file.index('.')]
counter = short_name[len(short_name)-4:len(short_name)]
cui = short_name[0:len(short_name)-5]
blob = {'@id':'https://iiif.bc.edu/'+ cui + '/range/r-' + str(index), '@type':'sc:Range', 'label':short_name,
'canvases':['https://iiif.bc.edu/' + cui +'/canvas/' + counter]}
index += 1
structures.append(blob)
return structures
def build_view(identifier,record):
title = record.title()
blob = '<!DOCTYPE html>\n<html>\n<head>\n<script async src="https://www.googletagmanager.com/gtag/js?id=UA-3008279-23"></script>' \
'\n<script src="/iiif/bc-mirador/gtag.js"></script>\n<title>' + identifier + '</title>\n' \
'<meta charset="UTF-8">\n<meta name="viewport" content="width=device-width, initial-scale=1.0">\n' \
'<link rel="stylesheet" type="text/css" href="/iiif/build/mirador/css/mirador-combined.css"></link>\n' \
'<link rel="stylesheet" type="text/css" href="/iiif/bc-mirador/mirador-bc.css"></link>\n' \
'<link rel="stylesheet" type="text/css" href="/iiif/bc-mirador/slicknav.css"></link>\n' \
'<script type="text/javascript" src="/iiif/build/mirador/mirador.js"></script>\n' \
'<script type="text/javascript" src="/iiif/bc-mirador/jquery.slicknav.min.js"></script>\n' \
'<script type="text/javascript" src="/iiif/bc-mirador/downloadMenu.js"></script>\n' \
'</head>\n<body>\n<div id="viewer"></div>\n<script type="text/javascript">\nwindow.mdObj = {MIRADOR_DATA: [' \
'{"manifestUri": "https://library.bc.edu/iiif/manifests/' + identifier + '.json","location": "Boston College",' \
'"title":"' + title + '"}],MIRADOR_WOBJECTS: [{"canvasID": "https://iiif.bc.edu/' + identifier +'/canvas/0001",' \
'"loadedManifest": "https://library.bc.edu/iiif/manifests/' + identifier + '.json","viewType": "ImageView"}],' \
'MIRADOR_BUTTONS: [{"label": "View Library Record","iconClass": "fa fa-external-link","attributes": {' \
'"class": "handle","href": "https://hdl.handle.net/2345.2/' + identifier + '","target": "_blank"}}]};\n' \
'</script>\n<script type="text/javascript" src="/iiif/bc-mirador/bcViewer.js"></script>\n' \
'</body>\n</html>'
return blob
def build_handles(identifier, hdl_password):
return f'CREATE 2345.2/{identifier}\n' \
f'100 HS_ADMIN 86400 1110 ADMIN 300:111111111111:2345.2/{identifier}\n' \
f'300 HS_SECKEY 86400 1100 UTF8 {hdl_password}\n' \
f'201 URL 86400 1110 UTF8 https://bclib.bc.edu/libsearch/bc/mms/{identifier}\n'
def check_requirements():
# Uses Literal String Interpolation, available only in Python 3.6+
if sys.version_info < (3, 6):
raise Exception('Requires python 3.6 or higher')
main()