Skip to content

Commit c899d96

Browse files
author
ExpDev07
committed
new api
1 parent f651503 commit c899d96

File tree

14 files changed

+116
-151
lines changed

14 files changed

+116
-151
lines changed

.env.example

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,2 @@
11
# Port to serve app on.
2-
PORT = 5000
3-
4-
# Id of spreadsheet to collect data from (found in the URL).
5-
# https://docs.google.com/spreadsheets/d/1wQVypefm946ch4XDp37uZ-wartW4V7ILdg-qYiDXUHM/htmlview?usp=sharing&sle=true
6-
SPREADSHEET_ID = 1wQVypefm946ch4XDp37uZ-wartW4V7ILdg-qYiDXUHM
7-
8-
# A Google Cloud API key that has Google Sheets API enabled.
9-
GOOGLE_API_KEY = your_api_key
2+
PORT = 5000

Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ python-dotenv = "*"
1212
requests = "*"
1313
gunicorn = "*"
1414
flask-cors = "*"
15-
geopy = "*"
1615
cachetools = "*"
16+
python-dateutil = "*"
1717

1818
[requires]
1919
python_version = "3.8"

Pipfile.lock

Lines changed: 9 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from flask import Flask
22
from flask_cors import CORS
33
from app.settings import *
4-
from app.location import get_locations
54

65
# Create the flask application.
76
app = Flask(__name__)

app/data/__init__.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import requests
2+
import csv
3+
from cachetools import cached, TTLCache
4+
from app.utils import date as date_util
5+
6+
"""
7+
Base URL for fetching data.
8+
"""
9+
base_url = 'https://raw.githubusercontent.com/CSSEGISandData/2019-nCoV/master/time_series/time_series_2019-ncov-%s.csv';
10+
11+
@cached(cache=TTLCache(maxsize=1024, ttl=3600))
12+
def get_data(type):
13+
"""
14+
Retrieves the data for the provided type.
15+
"""
16+
17+
# Adhere to type naming standard.
18+
type = type.lower().capitalize();
19+
20+
# Request the data
21+
request = requests.get(base_url % type)
22+
text = request.text
23+
24+
# Parse the CSV.
25+
data = list(csv.DictReader(text.splitlines()))
26+
27+
# The normalized locations.
28+
locations = []
29+
30+
for item in data:
31+
32+
# Normalize the item and append to locations.
33+
locations.append({
34+
# General info.
35+
'country': item['Country/Region'],
36+
'province': item['Province/State'],
37+
38+
# Coordinates.
39+
'coordinates': {
40+
'lat': item['Lat'],
41+
'long': item['Long'],
42+
},
43+
44+
# History.
45+
'history': dict(filter(lambda element: date_util.is_date(element[0]), item.items())),
46+
47+
# TODO: Total.
48+
'total': 0
49+
})
50+
51+
# Return the final data.
52+
return locations
53+
54+
55+
56+
57+
58+
59+
60+

app/location.py

Lines changed: 0 additions & 91 deletions
This file was deleted.

app/routes/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
from . import latest
1+
from . import confirmed
2+
from . import deaths
3+
from . import recovered
4+
from . import latest

app/routes/confirmed.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from flask import jsonify
2+
from app import app
3+
from app.data import get_data
4+
5+
@app.route('/confirmed')
6+
def confirmed():
7+
return jsonify(get_data('confirmed'))

app/routes/deaths.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from flask import jsonify
2+
from app import app
3+
from app.data import get_data
4+
5+
@app.route('/deaths')
6+
def deaths():
7+
return jsonify(get_data('deaths'))

app/routes/latest.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
1-
import requests
2-
import functools
3-
from flask import jsonify
41
from app import app
5-
from app.location import get_locations
2+
from app.data import get_data
63

74
@app.route('/latest')
85
def latest():
9-
# Get all the locations.
10-
locations = list(map(lambda loc: loc.serialize(), get_locations()))
11-
12-
# Make some simple statistic calculations.
13-
confirmed = sum(map(lambda loc: loc['confirmed'], locations))
14-
deaths = sum(map(lambda loc: loc['deaths'], locations))
15-
recovered = sum(map(lambda loc: loc['recovered'], locations))
16-
17-
# Return the stats and data as json.
18-
return {
19-
# Stats
20-
'confirmed': confirmed,
21-
'deaths': deaths,
22-
'recovered': recovered,
23-
24-
# Data
25-
'locations': locations,
26-
}
6+
return { }

0 commit comments

Comments
 (0)