-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
123 lines (96 loc) · 4.41 KB
/
app.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
from flask import Flask, render_template, redirect, url_for, request, flash, session
import requests
import json
app = Flask(__name__)
app.secret_key = '|\x93\xf2A\t\xa6-\xd1\x11\xe2}\x19M\x8ad\xb2\x80q\xa3\x7f\xbb4\xe7n'
@app.route("/", methods=["POST", "GET"])
def index():
if request.method == "POST":
country_input = request.form["country"]
country_input_modified = request.form["country"].lower().replace(
" ", "-")
state_input = request.form["state"]
graph_data = {}
graph_data["Cases"] = []
growth_over_time_api = requests.get(
"https://api.covid19api.com/total/dayone/country/%s" % (country_input_modified)).json()
country_covid_api = requests.get(
"https://api.covid19api.com/summary").json()
state_covid_api = requests.get(
"https://api.covid19api.com/live/country/%s" % (country_input_modified)).json()
countryCovidInformation = None
stateCovidInformation = None
# exceptions
if(country_input == "United States"):
country_input = "United States of America"
for i in country_covid_api["Countries"]:
if(i['Country'] == country_input):
countryCovidInformation = i
for i in state_covid_api:
if(i['Province'] == state_input):
stateCovidInformation = i
if(countryCovidInformation):
for i in growth_over_time_api:
graph_data["Cases"].append({
'confirmed': i['Confirmed'],
'date': i['Date']
})
if(countryCovidInformation and stateCovidInformation):
return render_template("find.html", apiConnection=True, countrySearch=True, stateSearch=True, countryCovidInformation=countryCovidInformation, stateCovidInformation=stateCovidInformation, graph_data=graph_data)
elif(countryCovidInformation):
return render_template("find.html", apiConnection=True, countrySearch=True, countryCovidInformation=countryCovidInformation, graph_data=graph_data)
else:
return render_template('find.html', apiConnectionFailed=True)
return render_template("find.html")
@app.route("/about")
def about():
world_covid_api = requests.get(
"https://api.covid19api.com/world/total").json()
return render_template("about.html", worldCovidInformation=world_covid_api)
@app.route("/local", methods=["POST", "GET"])
def local():
if request.method == "POST":
country_input = request.form["country"]
country_input_modified = request.form["country"].lower().replace(
" ", "-")
state_input = request.form["state"]
graph_data = {}
graph_data["Cases"] = []
growth_over_time_api = requests.get(
"https://api.covid19api.com/total/dayone/country/%s" % (country_input_modified)).json()
country_covid_api = requests.get(
"https://api.covid19api.com/summary").json()
state_covid_api = requests.get(
"https://api.covid19api.com/live/country/%s" % (country_input_modified)).json()
countryCovidInformation = None
stateCovidInformation = None
# exceptions
if(country_input == "United States"):
country_input = "United States of America"
for i in country_covid_api["Countries"]:
if(i['Country'] == country_input):
countryCovidInformation = i
for i in state_covid_api:
if(i['Province'] == state_input):
stateCovidInformation = i
if(countryCovidInformation):
for i in growth_over_time_api:
graph_data["Cases"].append({
'confirmed': i['Confirmed'],
'date': i['Date']
})
session["countryCovidInformation"] = countryCovidInformation
session["stateCovidInformation"] = stateCovidInformation
if(countryCovidInformation and stateCovidInformation):
return render_template("local.html", graph_data=graph_data)
elif(countryCovidInformation):
return render_template("local.html", graph_data=graph_data)
else:
return render_template('local.html')
return render_template("local.html")
@app.route("/change_location")
def change_location():
session.clear()
return redirect(url_for("local"))
if __name__ == "__main__":
app.run(debug=True)