-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
156 lines (120 loc) · 5.68 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from flask import Flask, redirect, render_template, request
import sqlite3
import csv
import requests
import os
app = Flask(__name__, static_url_path='/static')
@app.route("/")
def index():
return render_template("index.html")
@app.route("/contact")
def contact():
return render_template("contact.html")
@app.route("/polls", methods=["GET", "POST"])
def polls():
if request.method == "POST":
connection = sqlite3.connect('countries.db') # connects to db
db = connection.cursor() # creates the cursor for db connection
country = request.form.get("country")
rating = request.form.get("rating")
tools = request.form.get("tools")
try:
cTools = db.execute("SELECT learning_tools FROM stats WHERE name=(?)", (country,)).fetchone()[0]
if tools not in cTools:
nTools = cTools + ', ' + tools
else:
nTools = tools
except:
nTools = tools
try:
cRating = db.execute("SELECT rating FROM stats WHERE name=(?)", (country,)).fetchone()[0]
nRating = int((int(cRating) + int(rating)) / 2)
db.execute("UPDATE stats SET rating=(?), learning_tools = (?) WHERE name=(?)", (nRating, nTools, country))
connection.commit()
return redirect(f"/country/{country}")
except:
nRating = rating
db.execute("UPDATE stats SET rating=(?), learning_tools = (?) WHERE name=(?)", (nRating, nTools, country))
connection.commit()
return redirect(f"/country/{country}")
else:
connection = sqlite3.connect('countries.db') # connects to db
db = connection.cursor() # creates the cursor for db connection
countries = db.execute("SELECT name FROM stats").fetchall()
return render_template("polls.html", countries=countries)
@app.route("/country/<cnt>", methods=["GET"])
def country(cnt):
if request.method == "GET":
connection = sqlite3.connect('countries.db') # connects to db
db = connection.cursor() # creates the cursor for db connection
status = db.execute("SELECT status FROM stats WHERE name LIKE ?", ('%'+cnt+'%',)).fetchall()[0]
cases = db.execute("SELECT cases FROM stats WHERE name LIKE ?", ('%'+cnt+'%',)).fetchall()[0]
timeUpd = db.execute("SELECT time_updated FROM stats WHERE name LIKE ?", ('%'+cnt+'%',)).fetchall()[0]
try:
tools = db.execute("SELECT learning_tools FROM stats WHERE name LIKE ?", ('%'+cnt+'%',)).fetchall()[0]
except:
tools = None
try:
rating = db.execute("SELECT rating FROM stats WHERE name LIKE ?", ('%'+cnt+'%',)).fetchall()[0]
except:
rating = None
connection.commit()
return render_template("country.html", cnt=cnt, status=status, cases=cases, timeUpd=timeUpd, tools=tools, rating=rating)
@app.route("/countries", methods=["GET", "POST"])
def countries():
connection = sqlite3.connect('countries.db') # connects to db
db = connection.cursor() # creates the cursor for db connection
if request.method == "GET":
countries = db.execute("SELECT name FROM stats").fetchall()
return render_template("countries.html", countries=countries)
else:
search = request.form.get("cName")
results = db.execute("SELECT name FROM stats WHERE name LIKE ?", ('%'+search+'%',)).fetchall()
connection.commit()
return render_template("countries.html", results=results)
@app.route("/update/cases", methods=["GET"])
def case_updating():
connection = sqlite3.connect('countries.db') # connects to db
db = connection.cursor() # creates the cursor for db connection
filename = "WHO-COVID-19-global-data.csv"
data_url = "https://covid19.who.int/WHO-COVID-19-global-data.csv"
data = requests.get(data_url, allow_redirects=True)
try:
os.remove(filename)
except FileNotFoundError:
pass
open(filename, 'wb').write(data.content)
with open(filename, newline='') as csvFile: # link for file https://covid19.who.int/info
reader = csv.reader(csvFile)
for row in reader:
lastDate = row[0]
countryName = row[2]
countryCases = row[5] # code for updating
db.execute("UPDATE stats SET cases=(?) WHERE name LIKE (?)", (countryCases, '%'+countryName+'%'))
db.execute("UPDATE stats SET time_updated=(?) WHERE name LIKE (?)", (lastDate, '%'+countryName+'%'))
connection.commit()
print(f"Inserting {countryCases}, {countryName}")
return redirect("/")
@app.route("/update/status", methods=["GET"])
def status_updating():
connection = sqlite3.connect('countries.db') # connects to db
db = connection.cursor() # creates the cursor for db connection
filename = "covid_impact_education.csv"
data_url = "https://en.unesco.org/sites/default/files/covid_impact_education.csv"
data = requests.get(data_url, allow_redirects=True)
try:
os.remove(filename)
except FileNotFoundError:
pass
open(filename, 'wb').write(data.content)
with open(filename, newline='') as csvFile: # link for file https://en.unesco.org/covid19/educationresponse
reader = csv.reader(csvFile)
for row in reader:
countryName = row[2]
countryStatus = row[3] # code for updating
db.execute("UPDATE stats SET status=(?) WHERE name LIKE (?)", (countryStatus, '%'+countryName+'%'))
print(f"Updating {countryStatus}, {countryName}")
return redirect("/")
# available to run if double click the file
if __name__ == "__main__":
app.run(debug=True)