summaryrefslogtreecommitdiffstats
path: root/python/fatcat_tools/raw_api_client.py
blob: 75151ebbea9fdf1fa4b80f413f1227159edb7fd6 (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

import sys
import json
import requests


class RawFatcatApiClient:

    def __init__(self, host_url):
        self.host_url = host_url
        self.session = requests.Session()
        self._issn_map = dict()

    def get(self, path, data=None):
        headers = {"content-type": "application/json"}
        return self.session.get(self.host_url + path, json=data,
            headers=headers)

    def post(self, path, data=None):
        headers = {"content-type": "application/json"}
        return self.session.post(self.host_url + path, json=data,
            headers=headers)

    def new_editgroup(self):
        rv = self.post('/v0/editgroup', data=dict(
            editor_id=1))
        print(rv)
        print(rv.json())
        assert rv.status_code == 201
        editgroup_id = rv.json()['id']
        return editgroup_id

    def accept_editgroup(self, eg):
        rv = self.post('/v0/editgroup/{}/accept'.format(eg))
        assert rv.status_code == 200
        return rv

    def import_issn_file(self, json_file, create_containers=False, batchsize=100):
        eg = self.new_editgroup()
        i = 0
        with open(json_file, 'r') as file:
            for line in file:
                if i % batchsize == 0:
                    sys.stdout.write('\n{}: '.format(i))
                if (i+1) % 20 == 0:
                    sys.stdout.write('.')
                i = i + 1
                obj = json.loads(line)
                if not ("author" in obj and "title" in obj):
                    continue
                try:
                    self.import_crossref_dict(obj, editgroup=eg,
                        create_containers=create_containers)
                except Exception as e:
                    print("ERROR: {}".format(e))
                if i % batchsize == 0:
                    self.accept_editgroup(eg)
                    eg = self.new_editgroup()
        if i % batchsize != 0:
            self.accept_editgroup(eg)
        print("done!")

    def health(self):
        rv = self.get("/health")
        assert rv.status_code == 200
        return rv.json()