aboutsummaryrefslogtreecommitdiffstats
path: root/python/ingest_file.py
blob: 73e6a139557107f8768f50f0a74c0eb199ac7a32 (plain)
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
#!/usr/bin/env python3

import sys
import json
import argparse

from http.server import HTTPServer
from sandcrawler.ingest import IngestFileRequestHandler, IngestFileWorker


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
    ingester = IngestFileWorker(
        try_spn2=not args.no_spn2,
    )
    result = ingester.process(request)
    print(json.dumps(result, sort_keys=True))
    return result

def run_requests(args):
    # TODO: switch to using JsonLinePusher
    ingester = IngestFileWorker()
    for l in args.json_file:
        request = json.loads(l.strip())
        result = ingester.process(request)
        print(json.dumps(result, 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)
    subparsers = parser.add_subparsers()

    sub_single= subparsers.add_parser('single',
        help="ingests a single file URL")
    sub_single.set_defaults(func=run_single_ingest)
    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('--ingest-type',
        default="pdf",
        help="type of ingest (pdf, html, etc)")
    sub_single.add_argument('url',
        help="URL of paper to fetch")

    sub_requests = subparsers.add_parser('requests',
        help="takes a series of ingest requests (JSON, per line) and runs each")
    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)

    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()