-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
176 lines (131 loc) · 4.86 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
from unidecode import unidecode
from flask import (
Flask, flash, g, redirect, render_template, request, session, url_for, jsonify, current_app, send_from_directory
)
from flask.logging import create_logger
from superfpl.api import Api
from datetime import date
import logging
def create_app(test_config=None):
# create and configure the app
app = Flask(__name__, instance_relative_config=True, static_folder='static')
app.config.from_mapping(
SECRET_KEY='dev',
TEMPLATES_AUTO_RELOAD = True,
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
REDIS_HOST='localhost',
REDIS_PORT=6379,
REDIS_DB=0
)
if test_config is None:
# load the instance config, if it exists, when not testing
app.config.from_pyfile('config.py', silent=True)
else:
# load the test config if passed in
app.config.from_mapping(test_config)
# Configure logging:
logging.basicConfig(
filename='logs/' + date.today().strftime('%d-%m-%Y') + '.log',
level=logging.WARNING,
format='%(asctime)s %(levelname)s %(name)s %(threadName)s : %(message)s'
)
# ensure the instance folder exists
try:
os.makedirs(app.instance_path)
except OSError:
pass
return app
app = create_app() # Flask
logger = create_logger(app)
api = Api(logger)
@app.route('/', methods=['GET'])
def home():
players = api.get_players()
players = sorted(players, key=lambda k: unidecode(k['name']))
valid_players = []
for player in players:
if player['minutes'] > 0:
valid_players.append(player)
return render_template('fpl/home.html', title='Super FPL - Player Comparison Tool', players=valid_players)
@app.route('/robots.txt')
@app.route('/sitemap.xml')
def static_from_root():
return send_from_directory(app.static_folder, request.path[1:])
@app.route('/player_stats', methods=['GET'])
def get_player_stats():
# Get the players (make sure we have players in our mongodb):
api.get_players()
# get our teams and then format them:
teams = api.get_teams()
teams_formatted = {}
for team in teams:
teams_formatted[team['id']] = team
value_range = api.get_value_range()
return render_template('fpl/player_stats.html', title='Super FPL - Player Stats Database', teams=teams_formatted, value_range=value_range)
@app.route('/ajax/players', methods=['GET'])
def get_ajax_players():
players = api.get_players()
return jsonify(players)
@app.route('/ajax/player_form', methods=['GET'])
def get_player_form():
# Get the player IDs to get form for. Should be an array of IDs
players = request.args.getlist('players[]', None)
if players is None:
return jsonify({"data": []})
result = {}
for player in players:
form = api.get_player_form(player)
result[player] = form
return jsonify({"data": result})
@app.route('/ajax/player_stats', methods=['GET'])
def get_ajax_player_stats():
positions = get_parsed_positions(request.args)
min_price = request.args.get('minPrice', 0)
max_price = request.args.get('maxPrice', 15)
min_minutes_played = request.args.get('minsPlayed', 0)
max_ownership = request.args.get('maxOwnership', 100)
players = api.get_players(
min_price,
max_price,
min_minutes_played,
positions,
max_ownership)
if players is None:
return jsonify({"data": []})
players = sorted(players, key=lambda k: unidecode(k['name']))
fixtures = api.get_fixtures()
team_fixtures = {}
for fixture in fixtures:
home_team_id = fixture['team_h']
away_team_id = fixture['team_a']
if home_team_id not in team_fixtures:
team_fixtures[home_team_id] = []
if away_team_id not in team_fixtures:
team_fixtures[away_team_id] = []
team_fixtures[home_team_id].append(fixture)
team_fixtures[away_team_id].append(fixture)
# get our teams and then format them:
teams = api.get_teams()
teams_formatted = {}
for team in teams:
code = team['code']
team['fixtures'] = team_fixtures[team['id']]
teams_formatted[code] = team
for player in players:
player['team'] = teams_formatted[player['team_code']]
# Return in a format readable by DataTables
return jsonify({"data": players})
def get_parsed_positions(request_args):
positions = []
if request_args.get('positions[gkp]') is not None:
positions.append('gkp')
if request_args.get('positions[def]') is not None:
positions.append('def')
if request_args.get('positions[mid]') is not None:
positions.append('mid')
if request_args.get('positions[fwd]') is not None:
positions.append('fwd')
return positions
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)