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
|
#!/usr/bin/env python3
import argparse
import json
import subprocess
import sys
from http.server import HTTPServer
import sentry_sdk
from sandcrawler import GrobidClient, JsonLinePusher, KafkaCompressSink, KafkaSink
from sandcrawler.ingest_file import IngestFileRequestHandler, IngestFileWorker
from sandcrawler.ingest_fileset import IngestFilesetWorker
def run_single_ingest(args):
request = dict(
ingest_type=args.ingest_type,
base_url=args.url,
ext_ids=dict(doi=args.doi),
fatcat=dict(release_ident=args.release_id),
)
if args.force_recrawl:
request["force_recrawl"] = True
if request["ingest_type"] in [
"dataset",
]:
ingester = IngestFilesetWorker(
try_spn2=not args.no_spn2,
ingest_file_result_stdout=True,
)
else:
grobid_client = GrobidClient(
host_url=args.grobid_host,
)
ingester = IngestFileWorker(
try_spn2=not args.no_spn2,
html_quick_mode=args.html_quick_mode,
grobid_client=grobid_client,
)
result = ingester.process(request)
print(json.dumps(result, sort_keys=True))
return result
def run_requests(args):
# TODO: switch to using JsonLinePusher
file_worker = IngestFileWorker(
try_spn2=not args.no_spn2,
html_quick_mode=args.html_quick_mode,
)
fileset_worker = IngestFilesetWorker(
try_spn2=not args.no_spn2,
)
for line in args.json_file:
request = json.loads(line.strip())
if request["ingest_type"] in [
"dataset",
]:
result = fileset_worker.process(request)
else:
result = file_worker.process(request)
print(json.dumps(result, sort_keys=True))
def run_file_requests_backfill(args):
"""
Special mode for persisting GROBID and pdfextract results to Kafka, but
printing ingest result to stdout.
Can be used to batch re-process known files.
"""
grobid_topic = "sandcrawler-{}.grobid-output-pg".format(args.kafka_env)
pdftext_topic = "sandcrawler-{}.pdf-text".format(args.kafka_env)
thumbnail_topic = "sandcrawler-{}.pdf-thumbnail-180px-jpg".format(args.kafka_env)
xmldoc_topic = "sandcrawler-{}.xml-doc".format(args.kafka_env)
htmlteixml_topic = "sandcrawler-{}.html-teixml".format(args.kafka_env)
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=None,
grobid_sink=grobid_sink,
thumbnail_sink=thumbnail_sink,
pdftext_sink=pdftext_sink,
xmldoc_sink=xmldoc_sink,
htmlteixml_sink=htmlteixml_sink,
try_spn2=False,
)
pusher = JsonLinePusher(
worker,
args.json_file,
)
pusher.run()
def run_spn_status(args):
worker = IngestFileWorker(
sink=None,
try_spn2=False,
)
resp = worker.spn_client.v2_session.get("https://web.archive.org/save/status/system")
resp.raise_for_status()
print(f"System status: {json.dumps(resp.json(), sort_keys=True)}")
resp = worker.spn_client.v2_session.get("https://web.archive.org/save/status/user")
resp.raise_for_status()
print(f"User status: {json.dumps(resp.json(), sort_keys=True)}")
def run_api(args):
port = 8083
print("Listening on localhost:{}".format(port))
server = HTTPServer(("", port), IngestFileRequestHandler)
server.serve_forever()
def main():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
"--enable-sentry",
action="store_true",
help="report exceptions to Sentry",
)
subparsers = parser.add_subparsers()
sub_single = subparsers.add_parser("single", help="ingests a single base URL")
sub_single.set_defaults(func=run_single_ingest)
sub_single.add_argument(
"ingest_type", default="pdf", help="type of ingest (pdf, html, etc)"
)
sub_single.add_argument(
"--release-id", help="(optional) existing release ident to match to"
)
sub_single.add_argument("--doi", help="(optional) existing release DOI to match to")
sub_single.add_argument(
"--force-recrawl",
action="store_true",
help="ignore GWB history and use SPNv2 to re-crawl",
)
sub_single.add_argument("--no-spn2", action="store_true", help="don't use live web (SPNv2)")
sub_single.add_argument(
"--html-quick-mode",
action="store_true",
help="don't fetch individual sub-resources, just use CDX",
)
sub_single.add_argument("url", help="URL of paper to fetch")
sub_single.add_argument(
"--grobid-host", default="https://grobid.qa.fatcat.wiki", help="GROBID API host/port"
)
sub_requests = subparsers.add_parser(
"requests", help="takes a series of ingest requests (JSON, per line) and runs each"
)
sub_requests.add_argument(
"--no-spn2", action="store_true", help="don't use live web (SPNv2)"
)
sub_requests.add_argument(
"--html-quick-mode",
action="store_true",
help="don't fetch individual sub-resources, just use CDX",
)
sub_requests.set_defaults(func=run_requests)
sub_requests.add_argument(
"json_file",
help="JSON file (request per line) to import from (or stdin)",
default=sys.stdin,
type=argparse.FileType("r"),
)
sub_api = subparsers.add_parser(
"api", help="starts a simple HTTP server that processes ingest requests"
)
sub_api.set_defaults(func=run_api)
sub_api.add_argument("--port", help="HTTP port to listen on", default=8033, type=int)
sub_file_requests_backfill = subparsers.add_parser(
"file-requests-backfill",
help="starts a simple HTTP server that processes ingest requests",
)
sub_file_requests_backfill.set_defaults(func=run_file_requests_backfill)
sub_file_requests_backfill.add_argument(
"json_file",
help="JSON file (request per line) to import from (or stdin)",
default=sys.stdin,
type=argparse.FileType("r"),
)
sub_file_requests_backfill.add_argument(
"--kafka-hosts",
default="localhost:9092",
help="list of Kafka brokers (host/port) to use",
)
sub_file_requests_backfill.add_argument(
"--kafka-env", default="dev", help="Kafka topic namespace to use (eg, prod, qa, dev)"
)
sub_file_requests_backfill.add_argument(
"--grobid-host", default="https://grobid.qa.fatcat.wiki", help="GROBID API host/port"
)
sub_spn_status = subparsers.add_parser(
"spn-status", help="checks save-page-now v2 API status for bot user"
)
sub_spn_status.set_defaults(func=run_spn_status)
args = parser.parse_args()
if not args.__dict__.get("func"):
parser.print_help(file=sys.stderr)
sys.exit(-1)
# configure sentry *after* parsing args
if args.enable_sentry:
try:
GIT_REVISION = (
subprocess.check_output(["git", "describe", "--always"]).strip().decode("utf-8")
)
except Exception:
print("failed to configure git revision", file=sys.stderr)
GIT_REVISION = None
sentry_sdk.init(release=GIT_REVISION, environment=args.env, max_breadcrumbs=10)
args.func(args)
if __name__ == "__main__":
main()
|