forked from ror-community/ror-utilities
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch-other-ids-to-ror.py
66 lines (63 loc) · 2.32 KB
/
match-other-ids-to-ror.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
import argparse
import csv
import pycurl
import json
import math
import urllib
from io import BytesIO
import os.path
from os import path
from datetime import datetime
ROR_API_ENDPOINT = "https://api.ror.org/organizations"
INPUT_DIR = "input/"
OUTPUT_DIR = "output/"
def process_file(inputFile):
now = datetime.now()
output_file = OUTPUT_DIR + now.strftime("%Y-%m-%d") + "_matched_ror_ids.csv"
fields = ['input_id', 'ror_id']
with open(inputFile) as csv_file:
reader = csv.reader(csv_file, delimiter=',')
line_count = 0
with open(output_file, 'w') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(fields)
for row in reader:
input_id = row[0]
print "Finding ROR for: " + input_id
search_term = '"' + input_id + '"'
params = {'query': search_term}
ror_id=''
try:
c = pycurl.Curl()
data = BytesIO()
c.setopt(c.URL, ROR_API_ENDPOINT + '?' + urllib.urlencode(params))
c.setopt(pycurl.HTTPHEADER, ['Accept: application/json'])
c.setopt(c.WRITEFUNCTION, data.write)
c.perform()
response = json.loads(data.getvalue())
if response['number_of_results'] == 0:
ror_id = ''
elif response['number_of_results'] == 1:
ror_id = response['items'][0]['id']
else:
ror_id = ''
for items in response:
ror_id = ror_id + ", " + response['items'][0]['id']
print "Found match: " + ror_id
except ValueError:
ror_id = 'Error'
pass
finally:
writer.writerow([input_id, ror_id])
c.close()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--filename', type=str)
args = parser.parse_args()
input_file = INPUT_DIR + args.filename
if path.exists(input_file):
process_file(input_file)
else:
print "File " + input_file + " does not exist. Cannot process file."
if __name__ == '__main__':
main()