-
Notifications
You must be signed in to change notification settings - Fork 0
/
enhance.py
executable file
·178 lines (134 loc) · 5.59 KB
/
enhance.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
#!/usr/bin/env python3
from timeit import default_timer as timer
from PyPDF2 import PdfFileReader
from getpass import getpass
from requests import get
from starch import Archive
from sys import exit,argv,stdin,stdout,stderr
from os.path import basename
from io import BytesIO
from json import load,loads,dump,dumps
from copy import deepcopy
from traceback import print_exc
import starch
from starch.utils import pdf_to_textarray
from collections import Counter
def enhance(package, base):
t,s,c = Counter(), None, None
if 'aip.mets.metadata' in package and any(( path.endswith('_alto.xml') for path in package )):
# Alto
t,s,c = enhance_alto(package, base)
elif any(( package[path].get('mime_type', '') == 'application/pdf' for path in package )):
# PDF
t,s,c = enhance_pdf(package, base)
if c:
add_data_start = timer()
package.add(path='content.json', replace=True, data=dumps(c, indent=2).encode('utf-8'), type='Content', mime_type='application/json')
package.add(path='structure.json', replace=True, data=dumps(s, indent=2).encode('utf-8'), type='Structure', mime_type='application/json')
t.update({ 'add_data': timer() - add_data_start })
else:
return { 'status': 'no content' }, None, None
return t
def enhance_pdf(package, base):
print(f'Enhance PDF {base}', flush=True)
structure = []
content = []
timings = Counter()
partn = 1
for path in package:
info = package[path]
if info.get('mime_type', 'unknown') == 'application/pdf':
print(info['path'])
try:
get_bytes_start = timer()
b = BytesIO(package.get_raw(path).read())
get_bytes_end = timer()
print(f'get bytes: {get_bytes_end - get_bytes_start}')
part_id = f'{info["@id"]}#{partn}'
part = { '@id': part_id, '@type': 'Part', 'extracted_from': path, 'has_part': [] }
extract_content_start = timer()
text_array = pdf_to_textarray(b)
timings.update({ 'extract_content': timer() - extract_content_start })
for i,page_data in enumerate(text_array):
page_id = f'{part_id}-{i+1}'
page = { '@id': page_id, '@type': 'Page', 'content': [ page_data] }
content += [ deepcopy(page) ]
del(page['content'])
part['has_part'] += [ page ]
structure += [ part ]
added = True
except KeyboardInterrupt:
exit(1)
except Exception as e:
print(e, file=stderr)
print_exc()
print('', flush=True, file=stderr)
print('', flush=True, file=stdout)
continue
finally:
partn += 1
#enhance_pdf(package, path, part)
return timings, structure, content
def alto_content(package, base):
print(f'Enhance Alto {base}', flush=True)
structure = []
content = []
part = 1
mets = etree.fromstring(package.get_raw('aip.mets.metadata').read())
alto = etree.fromstring(package.get_raw(path).read())
for page in alto.findall('.//{http://www.loc.gov/standards/alto/ns-v2#}Page'):
page_id = page.attrib["ID"]
for area in alto.findall('.//{http://www.loc.gov/standards/alto/ns-v2#}ComposedBlock'):
area_id = area.attrib["ID"]
for block in area.findall('*'):
b = { '@id': f'{base}#{page_id}-{area_id}-{block.attrib["ID"]}', '@type': '', 'partOf': f'{base}#{page_id}-{area_id}' }
content = []
if block.tag == '{http://www.loc.gov/standards/alto/ns-v2#}TextBlock':
b['@type'] = 'Text'
for line in block.findall('{http://www.loc.gov/standards/alto/ns-v2#}TextLine'):
for x in line.findall('*'):
if x.tag == '{http://www.loc.gov/standards/alto/ns-v2#}String':
if 'SUBS_CONTENT' in x.attrib:
if x.attrib.get('SUBS_TYPE', None) == 'HypPart1':
content += [ x.attrib['SUBS_CONTENT'] ]
else:
content += [ x.attrib['CONTENT'] ]
elif x.tag == '{http://www.loc.gov/standards/alto/ns-v2#}SP':
content += [ ' ' ]
b['content'] = ''.join(content)
ret += [ b ]
return ret
def stripgen(i):
for l in i:
yield l.strip()
if __name__ == "__main__":
if len(argv) == 1:
print(f'usage: {basename(argv[0])} [options] <URL / location>')
exit(1)
loc = argv[1]
# if loc.startswith('http'):
# r = get(loc, verify=False)
#
# if r.status_code == 401:
# auth = (input('Username: '), getpass())
# else:
# auth = None
#
# a = Archive(loc, auth=auth)
# else:
# a = Archive(loc)
a = Archive('/data/archive', base='https://betalab.kb.se/')
for package_id in [ loc ]:
#for package_id in stripgen(stdin):
structure = []
content = []
print(package_id, flush=True)
partn = 1
get_package_start = timer()
package = a.get(package_id, mode='a')
get_package_end = timer()
if 'content.json' in package:
print('content.json already in package', flush=True)
continue
ret = enhance(package, a.base + package_id)
print(ret)