forked from ExpDev07/coronavirus-tracker-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
66 lines (47 loc) · 1.58 KB
/
__init__.py
File metadata and controls
66 lines (47 loc) · 1.58 KB
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
import requests
import csv
from cachetools import cached, TTLCache
from app.utils import date as date_util
"""
Base URL for fetching data.
"""
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';
@cached(cache=TTLCache(maxsize=1024, ttl=3600))
def get_data(category):
"""
Retrieves the data for the provided type.
"""
# Adhere to category naming standard.
category = category.lower().capitalize();
# Request the data
request = requests.get(base_url % category)
text = request.text
# Parse the CSV.
data = list(csv.DictReader(text.splitlines()))
# The normalized locations.
locations = []
for item in data:
# Filter out all the dates.
history = dict(filter(lambda element: date_util.is_date(element[0]), item.items()))
# Normalize the item and append to locations.
locations.append({
# General info.
'country': item['Country/Region'],
'province': item['Province/State'],
# Coordinates.
'coordinates': {
'lat': item['Lat'],
'long': item['Long'],
},
# History.
'history': history,
# Latest statistic.
'latest': int(list(history.values())[-1]),
})
# Latest total.
latest = sum(map(lambda location: location['latest'], locations))
# Return the final data.
return {
'locations': locations,
'latest': latest
}