-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathapp.py
126 lines (102 loc) · 3.81 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
from model import magic
from flask import Flask, request, Response
import sqlite3
import json
import threading
from constants import CURRENT_YEAR, DATABASE_PATH
import datetime
app = Flask(__name__)
database_path = DATABASE_PATH
@app.route('/')
def index():
response = Response({})
response.headers['Access-Control-Allow-Origin'] = '*'
return response
@app.route('/refresh')
def start_new_prediction():
t = threading.Thread(target=magic)
t.daemon = True
t.start()
response = Response(json.dumps("Process started."))
response.headers['Access-Control-Allow-Origin'] = '*'
return response
@app.route('/rankings')
def rankings():
conn = sqlite3.connect(database_path)
cur = conn.cursor()
cur.execute('SELECT * FROM prediction_rankings')
rankings_raw = cur.fetchall()
columns = [x[0] for x in cur.description]
rankings = []
for ranking in rankings_raw:
ranking_on_date = {}
for column, data in zip(columns[1:], ranking[1:]):
ranking_on_date[column] = data
rankings.append(ranking_on_date)
response = Response(json.dumps(rankings))
response.headers['Access-Control-Allow-Origin'] = '*'
return response
@app.route('/summary')
def summary():
conn = sqlite3.connect(database_path)
cur = conn.cursor()
cur.execute('SELECT * FROM summary')
summary = cur.fetchall()[0]
columns = [x[0] for x in cur.description]
summary_dict = {}
for column, data in zip(columns, summary):
summary_dict[column] = data
response = Response(json.dumps(summary_dict))
response.headers['Access-Control-Allow-Origin'] = '*'
return response
@app.route('/predictions')
def predictions():
conn = sqlite3.connect(database_path)
cur = conn.cursor()
query = 'SELECT * FROM prediction_results'
req_params_raw = request.data
if req_params_raw:
req_params = json.loads(req_params_raw)
query_type = 'AND' if 'against' in req_params else 'OR'
teams = ["'" + team + "'" for team in req_params['teams']]
teams = ",".join(teams)
query += ' WHERE HomeTeam IN ({}) {} AwayTeam IN ({})'.format(teams, query_type, teams)
cur.execute(query)
predictions_raw = cur.fetchall()
columns = [x[0] for x in cur.description]
predictions = []
for prediction in predictions_raw:
prediction_match = {}
for column, data in zip(columns[1:], prediction[1:]):
prediction_match[column] = data
predictions.append(prediction_match)
response = Response(json.dumps(predictions))
response.headers['Access-Control-Allow-Origin'] = '*'
return response
@app.route('/previous_results')
def previous_results():
conn = sqlite3.connect(database_path)
cur = conn.cursor()
season_start = datetime.datetime(CURRENT_YEAR, 7, 1).date().strftime('%Y-%m-%d')
query = 'SELECT * FROM previous_results WHERE Date > "{}"'.format(season_start)
req_params_raw = request.data
if req_params_raw:
req_params = json.loads(req_params_raw)
query_type = 'AND' if 'against' in req_params else 'OR'
teams = ["'" + team + "'" for team in req_params['teams']]
teams = ",".join(teams)
query += ' AND (HomeTeam IN ({}) {} AwayTeam IN ({}))'.format(teams, query_type, teams)
cur.execute(query)
previous_results_raw = cur.fetchall()
columns = [x[0] for x in cur.description]
previous_results = []
for result in previous_results_raw:
match_result = {}
for column, data in zip(columns[1:], result[1:]):
match_result[column] = data
previous_results.append(match_result)
response = Response(json.dumps(previous_results))
response.headers['Access-Control-Allow-Origin'] = '*'
return response
if __name__ == '__main__':
app.run()