Skip to content

Commit 56c5be6

Browse files
authored
Merge pull request #149 from ExpDev07/sources-endpoint
add sources endpoint and improve datasource middleware
2 parents 22e5920 + 594b56f commit 56c5be6

File tree

6 files changed

+83
-106
lines changed

6 files changed

+83
-106
lines changed

README.md

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ All requests must be made to the base url: ``https://coronavirus-tracker-api.her
2626

2727
### Picking data source
2828

29-
We provide multiple data-sources you can pick from, simply add the query parameter ``?source=your_source_of_choice`` to your requests. JHU will be used as a default if you don't provide one.
29+
We provide multiple data-sources you can pick from, simply add the query parameter ``?source=your_source_of_choice`` to your requests. JHU will be used as a default if you don't provide one. Dynamically retrieve available sources at ``/v2/sources``.
3030

3131
#### Available sources:
3232

@@ -142,58 +142,6 @@ Exclude timelines.
142142
GET /v2/locations?timelines=0
143143
```
144144

145-
### Getting US per county information.
146-
```http
147-
GET /v2/locations?source=csbs
148-
```
149-
```json
150-
{
151-
"latest": {
152-
"confirmed": 7596,
153-
"deaths": 43,
154-
"recovered": 0
155-
},
156-
"locations": [
157-
{
158-
"id": 0,
159-
"country": "US",
160-
"country_code": "US",
161-
"province": "New York",
162-
"state": "New York",
163-
"county": "New York",
164-
"last_updated": "2020-03-21T14:00:00Z",
165-
"coordinates": {
166-
"latitude": 40.71455,
167-
"longitude": -74.00714
168-
},
169-
"latest": {
170-
"confirmed": 6211,
171-
"deaths": 43,
172-
"recovered": 0
173-
}
174-
},
175-
{
176-
"id": 1,
177-
"country": "US",
178-
"country_code": "US",
179-
"province": "New York",
180-
"state": "New York",
181-
"county": "Westchester",
182-
"last_updated": "2020-03-21T14:00:00Z",
183-
"coordinates": {
184-
"latitude": 41.16319759,
185-
"longitude": -73.7560629
186-
},
187-
"latest": {
188-
"confirmed": 1385,
189-
"deaths": 0,
190-
"recovered": 0
191-
},
192-
}
193-
]
194-
}
195-
```
196-
197145
## Wrappers
198146

199147
These are the available API wrappers created by the community. They are not necessarily maintained by any of this project's authors or contributors.

app/routes/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from flask import Blueprint, redirect, request, current_app as app
1+
from flask import Blueprint, redirect, request, abort, current_app as app
22
from ..data import data_source
33

44
# Follow the import order to avoid circular dependency
55
api_v1 = Blueprint('api_v1', __name__, url_prefix='')
66
api_v2 = Blueprint('api_v2', __name__, url_prefix='/v2')
77

88
# API version 2.
9-
from .v2 import locations, latest
9+
from .v2 import locations, latest, sources
1010

1111
# API version 1.
1212
from .v1 import confirmed, deaths, recovered, all
@@ -23,8 +23,12 @@ def datasource():
2323
Attaches the datasource to the request.
2424
"""
2525
# Retrieve the datas ource from query param.
26-
source = request.args.get('source', type=str, default='jhu')
26+
source = data_source(request.args.get('source', type=str, default='jhu'))
27+
28+
# Abort with 404 if source cannot be found.
29+
if not source:
30+
return abort(404, description='The provided data-source was not found.')
2731

2832
# Attach source to request and return it.
29-
request.source = data_source(source)
33+
request.source = source
3034
pass

app/routes/v2/locations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def locations():
1717
try:
1818
locations = [j for j in locations if getattr(j, i) == args.get(i, type=str)]
1919
except AttributeError:
20-
print('TimelinedLocation object does not have attribute {}.'.format(i))
20+
print('Location does not have attribute {}.'.format(i))
2121

2222
# Serialize each location and return.
2323
return jsonify({

app/routes/v2/sources.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from flask import jsonify
2+
from ...data import data_sources
3+
from ...routes import api_v2 as api
4+
5+
@api.route('/sources')
6+
def sources():
7+
"""
8+
Retrieves a list of data-sources that are availble to use.
9+
"""
10+
return jsonify({
11+
'sources': list(data_sources.keys())
12+
})

app/services/location/csbs.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,41 @@ def get_locations():
3838
locations = []
3939

4040
for i, item in enumerate(data):
41+
# General info.
4142
state = item['State Name']
4243
county = item['County Name']
44+
45+
# Ensure country is specified.
4346
if county == "Unassigned" or county == "Unknown":
4447
continue
4548

46-
confirmed = int(item['Confirmed'] or 0)
47-
death = int(item['Death'] or 0)
48-
coordinates = Coordinates(float(item['Latitude']), float(item['Longitude']))
49+
# Coordinates.
50+
coordinates = Coordinates(
51+
item['Latitude'],
52+
item['Longitude']
53+
)
54+
55+
# Date string without "EDT" at end.
56+
last_update = ' '.join(item['Last Update'].split(' ')[0:2])
4957

50-
# Parse time to ISO format
51-
last_update = item['Last Update']
52-
date = last_update.split("-")
53-
year = int(date[0])
54-
month = int(date[1])
55-
date = date[2].split(" ")
56-
day = int(date[0])
57-
time = date[1].split(":")
58-
hour = int(time[0])
59-
minute = int(time[1])
60-
d = datetime(year=year, month=month, day=day, hour=hour, minute=minute)
61-
last_update = d.isoformat() + 'Z'
58+
# Append to locations.
59+
locations.append(CSBSLocation(
60+
# General info.
61+
i, state, county,
62+
63+
# Coordinates.
64+
Coordinates(
65+
item['Latitude'],
66+
item['Longitude']
67+
),
6268

63-
locations.append(CSBSLocation(i, state, county, coordinates, last_update, confirmed, death))
69+
# Last update (parse as ISO).
70+
datetime.strptime(last_update, '%Y/%m/%d %H:%M').isoformat() + 'Z',
71+
72+
# Statistics.
73+
int(item['Confirmed'] or 0),
74+
int(item['Death'] or 0)
75+
))
6476

77+
# Return the locations.
6578
return locations
Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
County Name,State Name,Confirmed,New,Death,Fatality Rate,Latitude,Longitude,Last Update
2-
New York,New York,4408,454,26,0.6%,40.71455,-74.00714,2020-03-20 13:58 EDT
3-
Westchester,New York,1091,293,0,0%,41.16319759,-73.7560629,2020-03-20 13:58 EDT
4-
Nassau,New York,754,382,4,0.5%,40.74165225,-73.58899619,2020-03-20 13:58 EDT
5-
Yakima,Washington,7,0,0,0%,46.60448,-120.50721,2020-03-20 13:58 EDT
6-
Thurston,Washington,6,0,0,0%,46.91980578,-122.8298691,2020-03-20 13:58 EDT
7-
Jefferson,Washington,4,0,0,0%,47.74810608,-123.6000095,2020-03-20 13:58 EDT
8-
Douglas,Kansas,1,0,0,0%,38.88462907,-95.29255463,2020-03-20 13:58 EDT
9-
Cherokee,Kansas,1,0,0,0%,37.16926692,-94.8462675759999,2020-03-20 13:58 EDT
10-
Jackson,Kansas,1,0,0,0%,39.4168027220001,-95.793674403,2020-03-20 13:58 EDT
11-
Twin Falls,Idaho,1,0,0,0%,42.55619,-114.4696,2020-03-20 13:58 EDT
12-
Kootenai,Idaho,1,0,0,0%,47.6775872760001,-116.697131928,2020-03-20 13:58 EDT
13-
Chittenden,Vermont,4,0,1,25%,44.45799511,-73.05404973,2020-03-20 13:58 EDT
14-
Bennington,Vermont,3,0,0,0%,42.87672,-73.19818,2020-03-20 13:58 EDT
15-
Windsor,Vermont,3,0,1,33.3%,43.48115,-72.38581,2020-03-20 13:58 EDT
16-
Washington,Vermont,1,0,0,0%,44.27344561,-72.61485925,2020-03-20 13:58 EDT
17-
Orange,Vermont,1,0,0,0%,44.14854,-72.40233,2020-03-20 13:58 EDT
18-
Addison,Vermont,1,0,0,0%,44.0280736,-73.13152876,2020-03-20 13:58 EDT
19-
Burleigh,North Dakota,11,0,0,0%,46.97801044,-100.4669442,2020-03-20 13:58 EDT
20-
Tucker,West Virginia,2,0,0,0%,39.1135508250001,-79.56492129,2020-03-20 13:58 EDT
21-
Mercer,West Virginia,1,0,0,0%,37.40556515,-81.11143231,2020-03-20 13:58 EDT
22-
Monongalia,West Virginia,1,0,0,0%,39.630233859,-80.0465546289999,2020-03-20 13:58 EDT
23-
Unassigned,New York,166,149,4,2.4%,42.165726,-74.948051,2020-03-20 13:58 EDT
24-
Unassigned,Washington,151,0,0,0%,47.400902,-121.490494,2020-03-20 13:58 EDT
25-
Unassigned,Colorado,57,0,0,0%,39.059811,-105.311104,2020-03-20 13:58 EDT
26-
Unknown,Pennsylvania,55,55,0,0%,40.590752,-77.209755,2020-03-20 13:58 EDT
27-
Unassigned,Pennsylvania,0,0,0,NaN%,40.590752,-77.209755,2020-03-20 13:58 EDT
28-
Franklin,Pennsylvania,1,1,0,0%,39.927495836,-77.721161869,2020-03-20 13:58 EDT
29-
Franklin,North Carolina,4,4,0,0%,36.0827448150001,-78.285600305,2020-03-20 13:58 EDT
30-
Lee,North Carolina,1,1,0,0%,35.475059921,-79.17154054,2020-03-20 13:58 EDT
31-
Clay,Minnesota,1,1,0,0%,46.892347886,-96.490737839,2020-03-20 13:58 EDT
32-
Yuma,Arizona,1,1,0,0%,32.768956524,-113.905830295,2020-03-20 13:58 EDT
33-
Dunklin,Missouri,1,1,0,0%,36.105848973,-90.16563,2020-03-20 13:58 EDT
2+
New York,New York,4408,454,26,0.6%,40.71455,-74.00714,2020/03/20 13:58 EDT
3+
Westchester,New York,1091,293,0,0%,41.16319759,-73.7560629,2020/03/20 13:58 EDT
4+
Nassau,New York,754,382,4,0.5%,40.74165225,-73.58899619,2020/03/20 13:58 EDT
5+
Yakima,Washington,7,0,0,0%,46.60448,-120.50721,2020/03/20 13:58 EDT
6+
Thurston,Washington,6,0,0,0%,46.91980578,-122.8298691,2020/03/20 13:58 EDT
7+
Jefferson,Washington,4,0,0,0%,47.74810608,-123.6000095,2020/03/20 13:58 EDT
8+
Douglas,Kansas,1,0,0,0%,38.88462907,-95.29255463,2020/03/20 13:58 EDT
9+
Cherokee,Kansas,1,0,0,0%,37.16926692,-94.8462675759999,2020/03/20 13:58 EDT
10+
Jackson,Kansas,1,0,0,0%,39.4168027220001,-95.793674403,2020/03/20 13:58 EDT
11+
Twin Falls,Idaho,1,0,0,0%,42.55619,-114.4696,2020/03/20 13:58 EDT
12+
Kootenai,Idaho,1,0,0,0%,47.6775872760001,-116.697131928,2020/03/20 13:58 EDT
13+
Chittenden,Vermont,4,0,1,25%,44.45799511,-73.05404973,2020/03/20 13:58 EDT
14+
Bennington,Vermont,3,0,0,0%,42.87672,-73.19818,2020/03/20 13:58 EDT
15+
Windsor,Vermont,3,0,1,33.3%,43.48115,-72.38581,2020/03/20 13:58 EDT
16+
Washington,Vermont,1,0,0,0%,44.27344561,-72.61485925,2020/03/20 13:58 EDT
17+
Orange,Vermont,1,0,0,0%,44.14854,-72.40233,2020/03/20 13:58 EDT
18+
Addison,Vermont,1,0,0,0%,44.0280736,-73.13152876,2020/03/20 13:58 EDT
19+
Burleigh,North Dakota,11,0,0,0%,46.97801044,-100.4669442,2020/03/20 13:58 EDT
20+
Tucker,West Virginia,2,0,0,0%,39.1135508250001,-79.56492129,2020/03/20 13:58 EDT
21+
Mercer,West Virginia,1,0,0,0%,37.40556515,-81.11143231,2020/03/20 13:58 EDT
22+
Monongalia,West Virginia,1,0,0,0%,39.630233859,-80.0465546289999,2020/03/20 13:58 EDT
23+
Unassigned,New York,166,149,4,2.4%,42.165726,-74.948051,2020/03/20 13:58 EDT
24+
Unassigned,Washington,151,0,0,0%,47.400902,-121.490494,2020/03/20 13:58 EDT
25+
Unassigned,Colorado,57,0,0,0%,39.059811,-105.311104,2020/03/20 13:58 EDT
26+
Unknown,Pennsylvania,55,55,0,0%,40.590752,-77.209755,2020/03/20 13:58 EDT
27+
Unassigned,Pennsylvania,0,0,0,NaN%,40.590752,-77.209755,2020/03/20 13:58 EDT
28+
Franklin,Pennsylvania,1,1,0,0%,39.927495836,-77.721161869,2020/03/20 13:58 EDT
29+
Franklin,North Carolina,4,4,0,0%,36.0827448150001,-78.285600305,2020/03/20 13:58 EDT
30+
Lee,North Carolina,1,1,0,0%,35.475059921,-79.17154054,2020/03/20 13:58 EDT
31+
Clay,Minnesota,1,1,0,0%,46.892347886,-96.490737839,2020/03/20 13:58 EDT
32+
Yuma,Arizona,1,1,0,0%,32.768956524,-113.905830295,2020/03/20 13:58 EDT
33+
Dunklin,Missouri,1,1,0,0%,36.105848973,-90.16563,2020/03/20 13:58 EDT

0 commit comments

Comments
 (0)