aboutsummaryrefslogtreecommitdiffstats
path: root/python/fatcat/orcid_importer.py
blob: 69b184d5e0bffbdb312117a52514e46b7c5b017a (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

import sys
import json
import itertools
import fatcat_client
from fatcat.importer_common import FatcatImporter


def value_or_none(e):
    if type(e) == dict:
        e = e.get('value')
    if type(e) == str and len(e) == 0:
        e = None
    # TODO: this is probably bogus; patched in desperation; remove?
    if e:
        try:
            e.encode()
        except UnicodeEncodeError:
            # Invalid JSON?
            print("BAD UNICODE")
            return None
    return e

class FatcatOrcidImporter(FatcatImporter):

    def parse_orcid_dict(self, obj):
        """
        obj is a python dict (parsed from json).
        returns a CreatorEntity
        """
        name = obj['person']['name']
        if name is None:
            return None
        extra = None
        given = value_or_none(name.get('given-names'))
        sur = value_or_none(name.get('family-name'))
        display = value_or_none(name.get('credit-name'))
        if display is None:
            # TODO: sorry human beings
            if given and sur:
                display = "{} {}".format(given, sur)
            elif sur:
                display = sur
            elif given:
                display = given
            else:
                # must have *some* name
                return None
        ce = fatcat_client.CreatorEntity(
            orcid=obj['orcid-identifier']['path'],
            given_name=given,
            surname=sur,
            display_name=display,
            extra=extra)
        return ce

    def create_row(self, row, editgroup_id=None):
        obj = json.loads(row)
        ce = self.parse_orcid_dict(obj)
        if ce is not None:
            ce.editgroup_id = editgroup_id
            self.api.create_creator(ce)

    def create_batch(self, batch, editgroup_id=None):
        """Reads and processes in batches (not API-call-per-line)"""
        objects = [self.parse_orcid_dict(json.loads(l))
                   for l in batch if l != None]
        objects = [o for o in objects if o != None]
        for o in objects:
            o.editgroup_id = editgroup_id
        self.api.create_creator_batch(objects)