-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_orcid_persons.py
135 lines (109 loc) · 5.18 KB
/
load_orcid_persons.py
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
import os
import sys
import logging
import tarfile
import json
import gzip
import lib.xml_parse
logging.basicConfig(format = "%(asctime)s %(levelname)s: %(message)s",
filename = __file__.split(".")[0] + ".log",
level = logging.INFO)
# url from https://orcid.figshare.com/articles/dataset/ORCID_Public_Data_File_2022/21220892
SOURCE_URL = "https://orcid.figshare.com/ndownloader/files/37635374"
TMP_DIR = "data/tmp/"
OUT_FILE = "data/input/ORCID_persons.jsonl.gz"
ORG_FILE = "data/input/ORCID_orgUnits.jsonl.gz"
# the configuration for XML parsing using lib.xml_parse.XMLHandler
SEARCH_FOR = {
"/record:record/person:person/person:name/personal-details:given-names": {"elementName": "firstName"},
"/record:record/person:person/person:name/personal-details:family-name": {"elementName": "lastName"},
"/record:record/person:person/person:name/personal-details:credit-name": {"elementName": "publishedName"},
"/record:record/person:person/other-name:other-names/other-name:other-name/other-name:content": {"elementName": "otherNames"},
"/record:record/person:person/address:addresses/address:address/address:country": {"elementName": "country"},
"/record:record/activities:activities-summary/activities:employments/activities:affiliation-group/employment:employment-summary/common:start-date/common:year": {
"elementName": "startYear",
"groupName": "affiliations",
"groupPath": "/record:record/activities:activities-summary/activities:employments/activities:affiliation-group/employment:employment-summary"
},
"/record:record/activities:activities-summary/activities:employments/activities:affiliation-group/employment:employment-summary/common:end-date/common:year": {
"elementName": "endYear",
"groupName": "affiliations",
"groupPath": "/record:record/activities:activities-summary/activities:employments/activities:affiliation-group/employment:employment-summary"
},
"/record:record/activities:activities-summary/activities:employments/activities:affiliation-group/employment:employment-summary/common:role-title": {
"elementName": "role",
"groupName": "affiliations",
"groupPath": "/record:record/activities:activities-summary/activities:employments/activities:affiliation-group/employment:employment-summary"
},
"/record:record/activities:activities-summary/activities:employments/activities:affiliation-group/employment:employment-summary/common:organization/common:name": {
"elementName": "orgName",
"groupName": "affiliations",
"groupPath": "/record:record/activities:activities-summary/activities:employments/activities:affiliation-group/employment:employment-summary"
},
"/record:record/activities:activities-summary/activities:employments/activities:affiliation-group/employment:employment-summary/common:organization/common:disambiguated-organization/common:disambiguated-organization-identifier": {
"elementName": "orgID",
"groupName": "affiliations",
"groupPath": "/record:record/activities:activities-summary/activities:employments/activities:affiliation-group/employment:employment-summary"
},
"/record:record/activities:activities-summary/activities:employments/activities:affiliation-group/employment:employment-summary/common:organization/common:disambiguated-organization/common:disambiguation-source": {
"elementName": "orgIDType",
"groupName": "affiliations",
"groupPath": "/record:record/activities:activities-summary/activities:employments/activities:affiliation-group/employment:employment-summary"
}
}
def isolate_orgUnit(affil):
result = {}
if "orgID" in affil:
result["id"] = affil["orgID"]
elif "orgName" in affil:
result["id"] = affil["orgName"]
if "orgIDType" in affil:
result["type"] = affil["orgIDType"]
del affil["orgIDType"]
if "orgName" in affil:
result["name"] = affil["orgName"]
del affil["orgName"]
return result
def run():
logging.info("start transforming persons ...")
localName = SOURCE_URL.strip().split("/")[-1]
fileName = localName + ".gz"
# download data
os.system("mkdir -p " + TMP_DIR)
os.system("wget -O " + TMP_DIR + fileName + " " + SOURCE_URL)
# parse and save data
count = 0
tar = tarfile.open(TMP_DIR + fileName)
parser = lib.xml_parse.Parser()
orgIDs = set()
with gzip.open(OUT_FILE, 'wt', encoding='utf-8') as outPerson:
with gzip.open(ORG_FILE, 'wt', encoding='utf-8') as outOrg:
for member in tar:
if member.isfile():
handler = lib.xml_parse.XMLHandler(SEARCH_FOR)
person = parser.parse(handler, tar.extractfile(member))
member_fileName = member.name.split("/")[-1]
person["id"] = member_fileName.split(".")[0]
orgUnit = isolate_orgUnit(person)
if orgUnit:
json.dump(orgUnit, outOrg)
outOrg.write('\n')
if "affiliations" in person:
affils = person["affiliations"]
for affil in affils:
orgUnit = isolate_orgUnit(affil)
if not orgUnit["id"] in orgIDs:
json.dump(orgUnit, outOrg)
outOrg.write('\n')
orgIDs.add(orgUnit["id"])
json.dump(person, outPerson)
outPerson.write('\n')
count += 1
# clean up temp file
os.system("rm " + TMP_DIR + fileName)
logging.info(".. " + str(count) + " persons transformed")
if "__main__" == __name__:
try:
run()
except:
logging.exception(sys.exc_info()[0])