-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.py
executable file
·530 lines (428 loc) · 16.4 KB
/
tool.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
#!/usr/bin/env python3
"""
Build winter pool documents
Usage:
tool.py (-h | --help)
tool.py [--quiet] [--loop] [--loop-sleep=NUMBER] [--spec=FILE]
Options:
-h, --help Show a brief usage summary.
--quiet Decrease logging verbosity.
--spec=FILE Job specification file.
[default: ./jobspec.yaml]
--loop Keep running the pipeline. If not specified,
the script exits after the first run.
--loop-sleep=NUMBER If looping, number of seconds to sleep between
iterations [default: 600]
"""
import csv
import logging
import os
import random
import re
import secrets
import tempfile
import time
import docopt
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload, MediaFileUpload
import httplib2shim
import jinja2.loaders
import latex
import latex.jinja2
from oauth2client import service_account
import textract
import yaml
# If modifying these scopes, delete the file token.json.
SCOPES = [
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.appfolder',
'https://www.googleapis.com/auth/drive.readonly',
]
PAGE_SIZE = 200
UCAS_PERSONAL_ID_PATTERN = re.compile(r'UCAS Personal ID:? ([0-9]+)')
# For persnal id match lines, this regex tries to match the name
NAME_PATTERN = re.compile(r'([^\s].*)\s+[0-9]+\sUCAS Personal ID')
LOG = logging.getLogger(__name__)
def main():
# Seed the random number generator with OS randomness
random.seed(os.urandom(32), version=2)
opts = docopt.docopt(__doc__)
with open(opts['--spec']) as fobj:
spec = yaml.load(fobj)
logging.basicConfig(
level=logging.WARN if opts['--quiet'] else logging.INFO
)
http = httplib2shim.Http()
credentials_path = spec.get('credentials_path', './credentials.json')
LOG.info('Using %s for client secrets', credentials_path)
creds = (
service_account.ServiceAccountCredentials.from_json_keyfile_name(
credentials_path, SCOPES
)
)
if not creds or creds.invalid:
LOG.error('Credentials are invalid')
service = build('drive', 'v3', http=creds.authorize(http))
loop_sleep = int(opts['--loop-sleep'])
while True:
run_pipeline(service, spec)
if not opts['--loop']:
break
LOG.info('Sleeping for %s seconds', loop_sleep)
time.sleep(loop_sleep)
def run_pipeline(service, spec):
incoming_folder_id = spec['incoming_folder_id']
processed_folder_id = spec['processed_folder_id']
def fetch_incoming_files():
return fetch_incoming_files_from_folder(service, incoming_folder_id)
def fetch_processed_files():
return fetch_processed_files_from_folder(service, processed_folder_id)
while True:
did_things = []
LOG.info('Copying new incoming files')
did_things.append(copy_new_incoming_files(
service, fetch_incoming_files(), fetch_processed_files(),
processed_folder_id))
LOG.info('OCR-ing any non-OCR-ed files')
did_things.append(ocr_files(
service, fetch_processed_files(), processed_folder_id))
LOG.info('Extracting applicant info from OCR-ed text')
did_things.append(extract_ucas_personal_id(
service, fetch_processed_files()))
LOG.info('Did things: %r', did_things)
if any(did_things):
LOG.info('Generating index and summary documents')
processed = fetch_processed_files()
generate_index(service, processed, processed_folder_id)
generate_summary(service, processed, processed_folder_id)
# We're done as soon as we had nothing to do
if not any(did_things):
break
def list_all_files(service, shuffled=True, **kwargs):
files = []
pageToken = None
while True:
results = service.files().list(pageToken=pageToken, **kwargs).execute()
files.extend(results.get('files', []))
pageToken = results.get('nextPageToken', '')
if pageToken == '':
break
if shuffled:
random.shuffle(files)
return files
def fetch_incoming_files_from_folder(service, incoming_folder_id):
return list_all_files(
service,
pageSize=PAGE_SIZE,
includeTeamDriveItems=True,
supportsTeamDrives=True,
q=(
f"'{incoming_folder_id}' in parents"
" and mimeType = 'application/pdf'"
" and trashed = false"
),
fields="nextPageToken, files(id, name, mimeType)"
)
def fetch_processed_files_from_folder(service, processed_folder_id):
return list_all_files(
service,
pageSize=PAGE_SIZE,
includeTeamDriveItems=True,
supportsTeamDrives=True,
q=f"'{processed_folder_id}' in parents and trashed = false",
fields=(
"nextPageToken, "
"files(id, name, appProperties, mimeType, webViewLink)"
)
)
def file_has_properties(file, keys):
appProperties = file.get('appProperties', {})
return all(k in appProperties for k in keys)
def copy_new_incoming_files(service, incoming, processed, processed_folder_id):
processed_file_sources = [
cf for cf in (
item.get('appProperties', {}).get('copiedFrom')
for item in processed
) if cf is not None
]
for item in incoming:
if item['id'] in processed_file_sources:
continue
name = secrets.token_urlsafe() + '.pdf'
LOG.info('Copying %s to %s', item['name'], name)
service.files().copy(
fileId=item['id'],
supportsTeamDrives=True,
body={
'name': name,
'copyRequiresWriterPermission': True,
'appProperties': {
'copiedFrom': item['id'],
},
'parents': [processed_folder_id],
},
).execute()
# After a successful processing of a file, return to let other
# pipeline stages progress
return True
return False
def ocr_files(service, processed, processed_folder_id):
for item in processed:
appProperties = item.get('appProperties', {})
copiedFrom = appProperties.get('copiedFrom')
if copiedFrom is None or item['mimeType'] != 'application/pdf':
continue
if file_has_properties(item, ['ocrTextFileId']):
continue
basename = '.'.join(item['name'].split('.')[:-1])
LOG.info('Downloading and OCR-ing %s', item["name"])
with tempfile.TemporaryDirectory() as tmp_dir:
request = service.files().get_media(
fileId=item['id'], supportsTeamDrives=True)
download_path = os.path.join(tmp_dir, 'file.pdf')
with open(download_path, 'wb') as fobj:
downloader = MediaIoBaseDownload(fobj, request)
while True:
_, done = downloader.next_chunk()
if done:
break
text_path = os.path.join(tmp_dir, 'file.txt')
text = textract.process(download_path, method='tesseract')
with open(text_path, 'w') as fobj:
fobj.write(text.decode('utf8', errors='replace'))
LOG.info("Uploading text")
media = MediaFileUpload(text_path, mimetype='text/plain')
text_file = service.files().create(
body={
'name': basename + '.txt',
'copyRequiresWriterPermission': True,
'appProperties': {
'pdfSourceFileId': item['id'],
},
'parents': [processed_folder_id],
},
supportsTeamDrives=True,
media_body=media,
fields='id'
).execute()
service.files().update(
fileId=item['id'],
supportsTeamDrives=True,
body={
'appProperties': {
'ocrTextFileId': text_file.get('id'),
},
}
).execute()
# After a successful processing of a file, return to let other pipeline
# stages progress
return True
return False
def extract_ucas_personal_id(service, processed):
processed_by_id = {item['id']: item for item in processed}
# Grep text values
for pdf_item in processed:
pdf_appProperties = pdf_item.get('appProperties', {})
ocrTextFileId = pdf_appProperties.get('ocrTextFileId')
if ocrTextFileId is None or pdf_item['mimeType'] != 'application/pdf':
continue
required_properties = [
'ucasPersonalId', 'totalMatchCount', 'consistentMatchCount',
'extractedName',
]
if file_has_properties(pdf_item, required_properties):
continue
item = processed_by_id.get(ocrTextFileId)
if item is None:
LOG.warn('Could not find OCR-ed text id: %s', ocrTextFileId)
continue
LOG.info('Scanning text of %s', item['id'])
with tempfile.TemporaryDirectory() as tmp_dir:
request = service.files().get_media(
fileId=item['id'], supportsTeamDrives=True)
download_path = os.path.join(tmp_dir, 'file.pdf')
with open(download_path, 'wb') as fobj:
downloader = MediaIoBaseDownload(fobj, request)
while True:
_, done = downloader.next_chunk()
if done:
break
with open(download_path) as fobj:
matched_lines_and_personal_ids = [
(line, m.group(1))
for line, m in (
(line, UCAS_PERSONAL_ID_PATTERN.search(line))
for line in fobj.readlines()
)
if m
]
# Match names
matched_names = [
m.group(1)
for m in (
NAME_PATTERN.search(line)
for line, _ in matched_lines_and_personal_ids
)
if m
]
name_table = {}
for name in matched_names:
name_table[name] = 1 + name_table.get(name, 0)
names_by_count = sorted(
list(name_table.items()),
key=lambda v: -v[1]
)
best_name = (
names_by_count[0][0]
if len(names_by_count) > 0
else 'Unknown'
)
count_table = {}
for _, id in matched_lines_and_personal_ids:
count_table[id] = 1 + count_table.get(id, 0)
matches_by_count = sorted(
list(count_table.items()),
key=lambda v: -v[1]
)
if len(matches_by_count) == 0:
LOG.warn('No UCAS id matches!')
continue
if matches_by_count[0][1] < 3:
LOG.warn(
'Too few consistent id matches (%s)',
matches_by_count[0][1])
continue
id = matches_by_count[0][0]
service.files().update(
fileId=pdf_item['id'],
supportsTeamDrives=True,
body={
'appProperties': {
'ucasPersonalId': id,
'consistentMatchCount': matches_by_count[0][1],
'totalMatchCount': len(
matched_lines_and_personal_ids),
'extractedName': best_name,
},
}
).execute()
# After a successful processing of a file, return to let other pipeline
# stages progress
return True
return False
def generate_index(service, processed, processed_folder_id):
required_properties = ['ucasPersonalId', 'extractedName']
fully_processed_files = [
item for item in processed
if file_has_properties(item, required_properties)
]
# Sort files
fully_processed_files = sorted(
fully_processed_files,
key=lambda f: (
f.get('appProperties', {}).get('extractedName', '')
.split(' ')[-1]
)
)
# Don't try to generate report if no files
if len(fully_processed_files) == 0:
return
# Do we have an index already?
index_files = [
item for item in processed
if item.get('appProperties', {}).get('isIndex', False)
]
# Make PDF
env = latex.jinja2.make_env(loader=jinja2.loaders.FileSystemLoader(
os.path.join(os.path.dirname(__file__), 'templates')
))
template = env.get_template('report.template.tex')
pdf = latex.build_pdf(template.render(files=fully_processed_files))
with tempfile.TemporaryDirectory() as tmpdir:
outpath = os.path.join(tmpdir, 'index.pdf')
with open(outpath, 'wb') as fobj:
fobj.write(bytes(pdf))
# Upload PDF
media = MediaFileUpload(outpath, mimetype='application/pdf')
api_params = {
'body': {
'name': 'index.pdf',
'copyRequiresWriterPermission': True,
'appProperties': {
'isIndex': True,
},
},
'supportsTeamDrives': True,
'media_body': media,
'fields': 'id'
}
if len(index_files) == 0:
api_params['body']['parents'] = [processed_folder_id]
service.files().create(**api_params).execute()
else:
for item in index_files:
service.files().update(
fileId=item['id'], **api_params).execute()
def generate_summary(service, processed, processed_folder_id):
processed_by_id = {item['id']: item for item in processed}
required_properties = ['ucasPersonalId', 'extractedName']
fully_processed_files = [
item for item in processed
if file_has_properties(item, required_properties)
]
# Sort files
fully_processed_files = sorted(
fully_processed_files,
key=lambda f: (
f.get('appProperties', {}).get('extractedName', '')
.split(' ')[-1]
)
)
# Don't try to generate summary if no files
if len(fully_processed_files) == 0:
return
# Do we have an summary already?
summary_files = [
item for item in processed
if item.get('appProperties', {}).get('isSummary', False)
]
with tempfile.TemporaryDirectory() as tmpdir:
# Make CSV
outpath = os.path.join(tmpdir, 'summary.csv')
with open(outpath, 'w') as fobj:
w = csv.writer(fobj)
w.writerow([
'UCAS Personal ID', 'Extracted Name', 'PDF', 'Extracted text'
])
for item in fully_processed_files:
appProperties = item.get('appProperties', {})
text_item = processed_by_id.get(
appProperties.get('ocrTextFileId'))
w.writerow([
appProperties['ucasPersonalId'],
appProperties['extractedName'],
item['webViewLink'],
text_item['webViewLink'] if text_item is not None else ''
])
# Upload - allow downloads otherwise this is a little pointless
media = MediaFileUpload(outpath, mimetype='text/csv')
api_params = {
'body': {
'name': 'summary.csv',
'appProperties': {
'isSummary': True,
},
},
'supportsTeamDrives': True,
'media_body': media,
'fields': 'id'
}
if len(summary_files) == 0:
api_params['body']['parents'] = [processed_folder_id]
service.files().create(**api_params).execute()
else:
for item in summary_files:
service.files().update(
fileId=item['id'], **api_params).execute()
if __name__ == '__main__':
main()