forked from rboulton/ukhospitals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
executable file
·139 lines (132 loc) · 4.33 KB
/
service.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
136
137
138
139
#!/usr/bin/env python
import wsgiwapi
import search
@wsgiwapi.jsonreturning
@wsgiwapi.allow_GETHEAD
@wsgiwapi.param("loc", 1, 1, None, [''],
"The location to search for hospitals near.")
@wsgiwapi.param("maxhits", 1, 1, '[0-9]+', ['10'],
"The maximum number of hospitals to return.")
@wsgiwapi.param("need_aande", 1, 1, '[01]', ['0'],
"If 1, only return hospitals with an accident and emergency department.")
def find_hospital(request):
loc = request.params['loc'][0]
maxhits = int(request.params['maxhits'][0])
need_aande = bool(int(request.params['need_aande'][0]))
res = {}
if loc == '':
res['hospitals'] = search.all('hospitals.db', need_aande=need_aande)[:maxhits]
else:
res['query_location'] = loc
res['query_latlong'], res['hospitals'] = \
search.search('hospitals.db', loc, maxhits=maxhits,
need_aande=need_aande)
return res
def docs(request):
tmpl = '''
<html>
<head>
<title>Hospital Locator, API documentation</title>
</head>
<body>
<h1>Hospital Locator, API documentation</h1>
<h2>Find hospitals</h2>
<p>
<em>Base url</em>:
<ul>
<li>
<code>http://tartarus.org/richard/hospitals/find</code>
</li>
</ul>
</p>
<p>
<em>Parameters</em>:
<ul>
<li>
<code>loc</code>: The location to centre the search on. May be an
address or a postcode. If omitted, all hospitals will be
returned (subject to the <code>maxhits</code> parameter).
</li>
<li>
<code>maxhits</code>: The maximum number of matching centres to
return. If omitted, defaults to 10.
</li>
</ul>
</p>
<p>
<em>Results</em>:
Returns a JSON object, with the following attributes:
<ul>
<li>
<q><code>query_location</code><q>: The location that was searched for
(ie, the loc parameter supplied to the search). Missing if no
location was specified.
</li><li>
<q><code>query_latlong</code></q>: The latitude and longitude
determined for the location (in decimal, separated by a space).
Missing if no location was specified.
</li><li>
<q><code>hospitals</code></q>: A list of hospitals, in order of
distance from the query location (unless no location specified, in
which case the order is undefined). Each hospitals is a JSON object,
with the following attributes:
<ul><li>
<q><code>name</code></q>: The name of the hospital.
</li><li>
<q><code>addresss</code></q>: The address of the hospital (usually
very vague).
</li><li>
<q><code>postcode</code></q>: The postcode of the hospital.
</li><li>
<q><code>longitude</code></q>: The longitude of the hospital.
</li><li>
<q><code>latitude</code></q>: The latitude of the hospital.
</li><li>
<q><code>dist_miles</code></q>: Distance from the query location in
miles: omitted if no location was specified.
</li><li>
<q><code>hasaande</code></q>: true if the hospital has accident and
emergency services. false otherwise.
</li><li>
<q><code>telephone</code></q>: a telephone number for the hospital.
Not always present, and may not be just one number.
</li></ul>
Other fields may be present, but aren't for all hospitals.
</li>
</ul>
</p>
</body>
</html>
'''.strip()
return wsgiwapi.Response(tmpl, content_type='text/html')
def frontpage(request):
tmpl = '''
<html>
<head>
<title>Hospital Locator</title>
</head>
<body>
<ul>
<li>
<a href="find">Find hospitals API</a>
</li>
<li>
<a href="docs">API documentation</a>
</li></ul>
</body>
</html>
'''.strip()
return wsgiwapi.Response(tmpl, content_type='text/html')
def main():
app = wsgiwapi.make_application({
'find': find_hospital,
'docs': docs,
'': frontpage,
})
server = wsgiwapi.make_server(app, ('0.0.0.0', 10011))
try:
server.start()
except KeyboardInterrupt:
server.stop()
if __name__ == '__main__':
main()