-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheverything.py
97 lines (69 loc) · 2.46 KB
/
everything.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
#!/usr/bin/python3
import json
import urllib.request
import cgitb
import sys
import cgi
import os
cgitb.enable()
print("Content-Type: application/json")
print()
fields = cgi.FieldStorage()
if "term" not in fields:
print("[]")
sys.exit()
f = urllib.request.urlopen('http://gtr.rcuk.ac.uk/search/project.json?term=%s' % fields['term'].value)
s = f.read()
data = json.loads(s.decode('utf-8'))
f.close()
organisations = dict()
# Loop over projects
for project in data['results']:
project = project['projectComposition']
orgid = project['leadResearchOrganisation']['id']
details = {
"title" : project['project']['title'],
"funder" : project['project']['fund']['funder']['name'],
"amount" : project['project']['fund']['valuePounds'],
"start" : project['project']['fund']['start'],
}
if 'status' in project['project']:
details["status"] = project['project']['status']
if 'end' in project['project']['fund']:
details["end"] = project['project']['fund']['end']
if 'abstractText' in project['project']:
details["abstract"] = project['project']['abstractText']
if orgid not in organisations:
organisations[orgid] = list()
organisations[orgid].append(details)
results = list()
for organisation in organisations.keys():
if os.path.isfile(organisation):
f = open(organisation)
establishment = json.load(f)
f.close()
else:
f = urllib.request.urlopen('http://gtr.rcuk.ac.uk/organisation/%s.json' % organisation)
s = f.read()
data = json.loads(s.decode('utf-8'))
f.close()
data = data['organisationOverview']['organisation']
postcode = data['address']['postCode']
f = urllib.request.urlopen('http://uk-postcodes.com/postcode/%s.json' % postcode.replace(" ", ""))
s = f.read()
postcodedata = json.loads(s.decode('utf-8'))
f.close()
try:
establishment = {
"orgname" : data['name'],
"lat" : postcodedata['geo']['lat'],
"lng" : postcodedata['geo']['lng']
}
except KeyError:
pass
f = open(organisation, "w")
json.dump(establishment, f)
f.close()
establishment["projects"] = organisations[organisation]
results.append(establishment)
print(json.dumps({"establishments":results}))