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
|
#!/usr/bin/env python3
"""
These are generally for continuously running workers that consume from Kafka.
Outputs might either be pushed back into Kafka, or directly into sandcrawler-db
or S3 (SeaweedFS).
"""
import argparse
import os
import sys
import raven
from sandcrawler import *
from sandcrawler.persist import (
PersistCrossrefWorker,
PersistHtmlTeiXmlWorker,
PersistXmlDocWorker,
)
# Yep, a global. Gets DSN from `SENTRY_DSN` environment variable
try:
git_sha = raven.fetch_git_sha("..")
except Exception:
git_sha = None
sentry_client = raven.Client(release=git_sha)
def run_grobid_extract(args):
consume_topic = "sandcrawler-{}.ungrobided-pg".format(args.env)
produce_topic = "sandcrawler-{}.grobid-output-pg".format(args.env)
sink = KafkaSink(
kafka_hosts=args.kafka_hosts,
produce_topic=produce_topic,
)
grobid_client = GrobidClient(
host_url=args.grobid_host,
)
wayback_client = WaybackClient(
host_url=args.grobid_host,
)
worker = GrobidWorker(
grobid_client=grobid_client,
wayback_client=wayback_client,
sink=sink,
)
pusher = KafkaJsonPusher(
worker=worker,
kafka_hosts=args.kafka_hosts,
consume_topic=consume_topic,
group="grobid-extract",
batch_size=1,
)
pusher.run()
def run_pdf_extract(args):
consume_topic = "sandcrawler-{}.unextracted".format(args.env)
pdftext_topic = "sandcrawler-{}.pdf-text".format(args.env)
thumbnail_topic = "sandcrawler-{}.pdf-thumbnail-180px-jpg".format(args.env)
pdftext_sink = KafkaCompressSink(
kafka_hosts=args.kafka_hosts,
produce_topic=pdftext_topic,
)
thumbnail_sink = KafkaSink(
kafka_hosts=args.kafka_hosts,
produce_topic=thumbnail_topic,
)
wayback_client = WaybackClient(
host_url=args.grobid_host,
)
worker = PdfExtractWorker(
wayback_client=wayback_client,
sink=pdftext_sink,
thumbnail_sink=thumbnail_sink,
)
pusher = KafkaJsonPusher(
worker=worker,
kafka_hosts=args.kafka_hosts,
consume_topic=consume_topic,
group="pdf-extract",
batch_size=1,
push_timeout_sec=120,
)
pusher.run()
def run_persist_grobid(args):
consume_topic = "sandcrawler-{}.grobid-output-pg".format(args.env)
worker = PersistGrobidWorker(
db_url=args.db_url,
s3_url=args.s3_url,
s3_bucket=args.s3_bucket,
s3_access_key=args.s3_access_key,
s3_secret_key=args.s3_secret_key,
s3_only=args.s3_only,
db_only=args.db_only,
)
kafka_group = "persist-grobid"
if args.s3_only:
kafka_group += "-s3"
pusher = KafkaJsonPusher(
worker=worker,
kafka_hosts=args.kafka_hosts,
consume_topic=consume_topic,
group=kafka_group,
push_batches=True,
batch_size=25,
)
pusher.run()
def run_persist_pdftext(args):
consume_topic = "sandcrawler-{}.pdf-text".format(args.env)
worker = PersistPdfTextWorker(
db_url=args.db_url,
s3_url=args.s3_url,
s3_bucket=args.s3_bucket,
s3_access_key=args.s3_access_key,
s3_secret_key=args.s3_secret_key,
s3_only=args.s3_only,
db_only=args.db_only,
)
kafka_group = "persist-pdf-text"
if args.s3_only:
kafka_group += "-s3"
pusher = KafkaJsonPusher(
worker=worker,
kafka_hosts=args.kafka_hosts,
consume_topic=consume_topic,
group=kafka_group,
push_batches=True,
batch_size=25,
)
pusher.run()
def run_persist_thumbnail(args):
consume_topic = "sandcrawler-{}.pdf-thumbnail-180px-jpg".format(args.env)
worker = PersistThumbnailWorker(
s3_url=args.s3_url,
s3_bucket=args.s3_bucket,
s3_access_key=args.s3_access_key,
s3_secret_key=args.s3_secret_key,
s3_extension=".180px.jpg",
s3_folder="pdf",
)
pusher = KafkaJsonPusher(
worker=worker,
kafka_hosts=args.kafka_hosts,
consume_topic=consume_topic,
group="persist-pdf-thumbnail",
push_batches=False,
raw_records=True,
batch_size=25,
)
pusher.run()
def run_persist_xml_doc(args: argparse.Namespace) -> None:
consume_topic = f"sandcrawler-{args.env}.xml-doc"
worker = PersistXmlDocWorker(
s3_url=args.s3_url,
s3_bucket=args.s3_bucket,
s3_access_key=args.s3_access_key,
s3_secret_key=args.s3_secret_key,
)
pusher = KafkaJsonPusher(
worker=worker,
kafka_hosts=args.kafka_hosts,
consume_topic=consume_topic,
group="persist-xml-doc",
push_batches=False,
batch_size=25,
)
pusher.run()
def run_persist_html_teixml(args: argparse.Namespace) -> None:
consume_topic = f"sandcrawler-{args.env}.html-teixml"
worker = PersistHtmlTeiXmlWorker(
s3_url=args.s3_url,
s3_bucket=args.s3_bucket,
s3_access_key=args.s3_access_key,
s3_secret_key=args.s3_secret_key,
)
pusher = KafkaJsonPusher(
worker=worker,
kafka_hosts=args.kafka_hosts,
consume_topic=consume_topic,
group="persist-html-teixml",
push_batches=False,
batch_size=25,
)
pusher.run()
def run_persist_pdftrio(args):
consume_topic = "sandcrawler-{}.pdftrio-output".format(args.env)
worker = PersistPdfTrioWorker(
db_url=args.db_url,
)
pusher = KafkaJsonPusher(
worker=worker,
kafka_hosts=args.kafka_hosts,
consume_topic=consume_topic,
group="persist-pdftrio",
push_batches=True,
batch_size=100,
)
pusher.run()
def run_ingest_file(args):
spn_cdx_retry_sec = 9.0
if args.bulk:
consume_group = "sandcrawler-{}-ingest-file-bulk".format(args.env)
consume_topic = "sandcrawler-{}.ingest-file-requests-bulk".format(args.env)
elif args.priority:
spn_cdx_retry_sec = 45.0
consume_group = "sandcrawler-{}-ingest-file-priority".format(args.env)
consume_topic = "sandcrawler-{}.ingest-file-requests-priority".format(args.env)
else:
spn_cdx_retry_sec = 1.0
consume_group = "sandcrawler-{}-ingest-file".format(args.env)
consume_topic = "sandcrawler-{}.ingest-file-requests-daily".format(args.env)
produce_topic = "sandcrawler-{}.ingest-file-results".format(args.env)
grobid_topic = "sandcrawler-{}.grobid-output-pg".format(args.env)
pdftext_topic = "sandcrawler-{}.pdf-text".format(args.env)
thumbnail_topic = "sandcrawler-{}.pdf-thumbnail-180px-jpg".format(args.env)
xmldoc_topic = "sandcrawler-{}.xml-doc".format(args.env)
htmlteixml_topic = "sandcrawler-{}.html-teixml".format(args.env)
sink = KafkaSink(
kafka_hosts=args.kafka_hosts,
produce_topic=produce_topic,
)
grobid_sink = KafkaSink(
kafka_hosts=args.kafka_hosts,
produce_topic=grobid_topic,
)
grobid_client = GrobidClient(
host_url=args.grobid_host,
)
pdftext_sink = KafkaCompressSink(
kafka_hosts=args.kafka_hosts,
produce_topic=pdftext_topic,
)
thumbnail_sink = KafkaSink(
kafka_hosts=args.kafka_hosts,
produce_topic=thumbnail_topic,
)
xmldoc_sink = KafkaSink(
kafka_hosts=args.kafka_hosts,
produce_topic=xmldoc_topic,
)
htmlteixml_sink = KafkaSink(
kafka_hosts=args.kafka_hosts,
produce_topic=htmlteixml_topic,
)
worker = IngestFileWorker(
grobid_client=grobid_client,
sink=sink,
grobid_sink=grobid_sink,
thumbnail_sink=thumbnail_sink,
pdftext_sink=pdftext_sink,
xmldoc_sink=xmldoc_sink,
htmlteixml_sink=htmlteixml_sink,
# don't SPNv2 for --bulk backfill
try_spn2=not args.bulk,
spn_cdx_retry_sec=spn_cdx_retry_sec,
)
pusher = KafkaJsonPusher(
worker=worker,
kafka_hosts=args.kafka_hosts,
consume_topic=consume_topic,
group=consume_group,
batch_size=1,
)
pusher.run()
def run_persist_ingest_file(args):
consume_topic = "sandcrawler-{}.ingest-file-results".format(args.env)
worker = PersistIngestFileResultWorker(
db_url=args.db_url,
)
pusher = KafkaJsonPusher(
worker=worker,
kafka_hosts=args.kafka_hosts,
consume_topic=consume_topic,
group="persist-ingest",
push_batches=True,
batch_size=100,
)
pusher.run()
def run_persist_crossref(args):
grobid_client = GrobidClient(host_url=args.grobid_host)
consume_topic = "fatcat-{}.api-crossref".format(args.env)
worker = PersistCrossrefWorker(
db_url=args.db_url,
grobid_client=grobid_client,
parse_refs=args.parse_refs,
)
pusher = KafkaJsonPusher(
worker=worker,
kafka_hosts=args.kafka_hosts,
consume_topic=consume_topic,
group="persist-crossref",
push_batches=True,
# small batch size because doing GROBID processing
batch_size=20,
)
pusher.run()
def main():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
"--kafka-hosts",
default="localhost:9092",
help="list of Kafka brokers (host/port) to use",
)
parser.add_argument(
"--env", default="dev", help="Kafka topic namespace to use (eg, prod, qa, dev)"
)
parser.add_argument(
"--grobid-host", default="https://grobid.qa.fatcat.wiki", help="GROBID API host/port"
)
parser.add_argument(
"--db-url",
help="postgresql database connection string",
default="postgres:///sandcrawler",
)
parser.add_argument("--s3-url", help="S3 (seaweedfs) backend URL", default="localhost:9000")
parser.add_argument(
"--s3-access-key",
help="S3 (seaweedfs) credential",
default=os.environ.get("SANDCRAWLER_BLOB_ACCESS_KEY")
or os.environ.get("MINIO_ACCESS_KEY"),
)
parser.add_argument(
"--s3-secret-key",
help="S3 (seaweedfs) credential",
default=os.environ.get("SANDCRAWLER_BLOB_SECRET_KEY")
or os.environ.get("MINIO_SECRET_KEY"),
)
parser.add_argument(
"--s3-bucket", help="S3 (seaweedfs) bucket to persist into", default="sandcrawler-dev"
)
subparsers = parser.add_subparsers()
sub_grobid_extract = subparsers.add_parser(
"grobid-extract",
help="daemon that consumes CDX JSON objects from Kafka, uses GROBID to extract XML, pushes to Kafka",
)
sub_grobid_extract.set_defaults(func=run_grobid_extract)
sub_pdf_extract = subparsers.add_parser(
"pdf-extract",
help="daemon that consumes CDX JSON objects from Kafka, extracts text and thumbnail, pushes to Kafka",
)
sub_pdf_extract.set_defaults(func=run_pdf_extract)
sub_persist_grobid = subparsers.add_parser(
"persist-grobid",
help="daemon that consumes GROBID output from Kafka and pushes to S3 (seaweedfs) and postgres",
)
sub_persist_grobid.add_argument(
"--s3-only",
action="store_true",
help="only upload TEI-XML to S3 (don't write to database)",
)
sub_persist_grobid.add_argument(
"--db-only",
action="store_true",
help="only write status to database (don't upload TEI-XML to S3)",
)
sub_persist_grobid.set_defaults(func=run_persist_grobid)
sub_persist_pdftext = subparsers.add_parser(
"persist-pdftext",
help="daemon that consumes pdftext output from Kafka and pushes to S3 (seaweedfs) and postgres",
)
sub_persist_pdftext.add_argument(
"--s3-only",
action="store_true",
help="only upload TEI-XML to S3 (don't write to database)",
)
sub_persist_pdftext.add_argument(
"--db-only",
action="store_true",
help="only write status to database (don't upload TEI-XML to S3)",
)
sub_persist_pdftext.set_defaults(func=run_persist_pdftext)
sub_persist_thumbnail = subparsers.add_parser(
"persist-thumbnail",
help="daemon that consumes thumbnail output from Kafka and pushes to S3 (seaweedfs) and postgres",
)
sub_persist_thumbnail.set_defaults(func=run_persist_thumbnail)
sub_persist_xml_doc = subparsers.add_parser(
"persist-xml-doc",
help="daemon that consumes xml-doc output from Kafka and pushes to S3 (seaweedfs) bucket",
)
sub_persist_xml_doc.set_defaults(func=run_persist_xml_doc)
sub_persist_html_teixml = subparsers.add_parser(
"persist-html-teixml",
help="daemon that consumes html-teixml output from Kafka and pushes to S3 (seaweedfs) bucket",
)
sub_persist_html_teixml.set_defaults(func=run_persist_html_teixml)
sub_persist_pdftrio = subparsers.add_parser(
"persist-pdftrio",
help="daemon that consumes pdftrio output from Kafka and pushes to postgres",
)
sub_persist_pdftrio.set_defaults(func=run_persist_pdftrio)
sub_ingest_file = subparsers.add_parser(
"ingest-file",
help="daemon that consumes requests from Kafka, ingests, pushes results to Kafka",
)
sub_ingest_file.add_argument(
"--bulk",
action="store_true",
help="consume from bulk kafka topic (eg, for ingest backfill)",
)
sub_ingest_file.add_argument(
"--priority",
action="store_true",
help="consume from priority kafka topic (eg, for SPN requests)",
)
sub_ingest_file.set_defaults(func=run_ingest_file)
sub_persist_ingest_file = subparsers.add_parser(
"persist-ingest-file",
help="daemon that consumes ingest-file output from Kafka and pushes to postgres",
)
sub_persist_ingest_file.set_defaults(func=run_persist_ingest_file)
sub_persist_crossref = subparsers.add_parser(
"persist-crossref",
help="daemon that persists crossref to postgres; also does GROBID ref transform",
)
sub_persist_crossref.add_argument(
"--grobid-host", default="https://grobid.qa.fatcat.wiki", help="GROBID API host/port"
)
sub_persist_crossref.add_argument(
"--parse-refs",
action="store_true",
help="use GROBID to parse any unstructured references (default is to not)",
)
sub_persist_crossref.set_defaults(func=run_persist_crossref)
args = parser.parse_args()
if not args.__dict__.get("func"):
parser.print_help(file=sys.stderr)
sys.exit(-1)
args.func(args)
if __name__ == "__main__":
main()
|