pool.v0.trr379.de-knowledge/code/dfg-cp2pool-v0.py

47 lines
1.2 KiB
Python

import json
import sys
from urllib.parse import quote
# this is the main PID mapping from the DFG survey context to the metadata
# pooling system for person records
with open('dfg-cp_pid-map.json') as fobj:
dfgcp_id_map = json.load(fobj)
for line in sys.stdin:
j = json.loads(line)
if j['pid'] not in dfgcp_id_map:
msg = f'Unknown person PID, please map: {j!r}'
raise ValueError(msg)
if j['person_identity_consent'] != 'yes':
continue
# this PID must be a valid URI in the end, so we must quote any invalid chars.
# however, the mapping declared above should already take care of (most) of this
mpid = dfgcp_id_map[j["pid"]]
mpid = quote(mpid, safe='/:')
out = {
'pid': f'trr379root:contributors/{mpid}',
}
for k in (
'family_name',
'given_name',
'additional_names',
'orcid',
):
if k in j:
out[k] = j[k]
member_of = []
if 'primary_affiliation' in j:
member_of.append(j['primary_affiliation'])
if 'other_affiliations' in j:
member_of.extend(j['other_affiliations'])
if member_of:
out["part_of"] = member_of
print(json.dumps(out, ensure_ascii=False))