Skip to content

Commit c02d2aa

Browse files
author
Vasily Fotin
committed
Route: /formatted/
Formatted by location version of the "all" route
1 parent f22bd25 commit c02d2aa

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

app/data/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"""
1010
base_url = 'https://raw.githubusercontent.com/CSSEGISandData/2019-nCoV/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-%s.csv';
1111

12-
@cached(cache=TTLCache(maxsize=1024, ttl=3600))
12+
@cached(cache=TTLCache(maxsize=1024, ttl=900))
1313
def get_data(category):
1414
"""
1515
Retrieves the data for the provided type. The data is cached for 1 hour.

app/routes/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from . import confirmed
2-
from . import deaths
3-
from . import recovered
4-
from . import all
1+
# from . import confirmed
2+
# from . import deaths
3+
# from . import recovered
4+
# from . import all
5+
from . import formatted

app/routes/formatted.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from flask import jsonify
2+
from app import app
3+
from app.data import get_data
4+
from nested_lookup import get_occurrence_of_value
5+
from cachetools import cached, TTLCache
6+
7+
8+
@app.route('/formatted')
9+
@cached(cache=TTLCache(maxsize=1024, ttl=900))
10+
def full():
11+
# Get all the categories.
12+
confirmed = get_data('confirmed')
13+
deaths = get_data('deaths')
14+
recovered = get_data('recovered')
15+
16+
# prepare output dictionary
17+
output = []
18+
19+
# Formatting
20+
for locationConfirmed in confirmed["locations"]:
21+
deathsData = {}
22+
recoversData = {}
23+
24+
# find matching region for deaths & recovered
25+
targetCoordniates = locationConfirmed["coordinates"]
26+
27+
# search for location match: death
28+
for locationDeaths in deaths["locations"]:
29+
# check for matching coordinates
30+
if locationDeaths["coordinates"] == targetCoordniates:
31+
deathsData = locationDeaths
32+
33+
# search for location match: recovered
34+
for locationRecovers in recovered["locations"]:
35+
# check for matching coordinates
36+
if locationRecovers["coordinates"] == targetCoordniates:
37+
recoversData = locationRecovers
38+
39+
40+
# add the death statistics to the object
41+
locationOutput = {
42+
'location': locationConfirmed["country"],
43+
'coordinates': targetCoordniates,
44+
'confirmed': locationConfirmed,
45+
'deaths': deathsData,
46+
'recovered': recoversData
47+
}
48+
49+
# add current location to the output
50+
output.append(locationOutput)
51+
52+
return jsonify(output)

0 commit comments

Comments
 (0)