-
Notifications
You must be signed in to change notification settings - Fork 3
/
query.py
74 lines (67 loc) · 1.92 KB
/
query.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
#Query Scholix API
#
#Example query:
#curl -X GET --header 'Accept: application/json' 'http://api.scholexplorer.openaire.eu/v2/Links/?targetPid=10.1109%2FICIP.2015.7351744'
#
import fileinput
import json
import requests
API = 'http://api.scholexplorer.openaire.eu/v2/Links'
out = 'records.json'
hits = 'hits.out'
success = 0
no_link = 0
found = []
links = {} #dict where key is dro-doi and value is json record describing research data
count = 0
def clean(d):
"""
Remove http, https etc and trailing blanks
"""
d = d.rstrip()
d = d.replace('http://doi.org/',"")
d = d.replace('http://dx.doi.org/',"")
d = d.replace('https://doi.org/',"")
d = d.replace('https://dx.doi.org/',"")
return d
print('opening output file ' + out)
g = open(out, 'w')
fh = open(hits, 'w')
for doi in fileinput.input():
doi = clean(doi)
count += 1
print("################ " + str(count) + " #################")
if not doi.strip():
#do nothing
print('found empty line...ignored')
break
payload = {'targetPid': doi.rstrip()}
r = requests.get(API, params=payload)
if r.raise_for_status() == None:
print('URL: ' + r.url)
print('Status: ')
print(r.status_code)
print('Headers: ')
print(r.headers)
print('Encoding: ')
print(r.encoding)
try:
data = r.json()
res = data['result']
if len(res) > 0:
#print(res)
success += 1
fh.write(doi + '\n')
else:
print('Did not find any links for doi ' + doi)
no_link += 1
except ValueError:
print('Invalid JSON')
""" json_string = json.dumps(r.json())
print(json_string)
print('\n')
"""
fh.close()
print('Total DOIs in sample ' + str(success + no_link))
print('Links found ' + str(success))
print('Did not find a link ' + str(no_link))