forked from ykeuter/ffl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
153 lines (137 loc) · 5.05 KB
/
manage.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
from ffl import app, db, models, espn, nfl, shark
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
import csv
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
@manager.command
def delete_data():
db.session.execute(models.playerPosition.delete())
models.EspnProjections.query.delete()
models.SharkProjections.query.delete()
models.NflPlayer.query.delete()
models.Position.query.delete()
models.NflGame.query.delete()
models.FieldTeam.query.delete()
models.FieldPlayer.query.delete()
db.session.commit()
print("Deleted all data.")
@manager.command
def shark_proj(period=app.config['SHARK_SEGMENT'],
scoring=app.config['SHARK_SCORING']):
shark.update_projections(int(period), int(scoring))
@manager.command
def shark_check():
shark.check_sanity()
@manager.command
def espn_proj(league_id=app.config['ESPN_LEAGUE_ID']):
espn.update_projections(int(league_id))
@manager.command
def update_boxscores(year=None, week=None):
if year is None:
models.NflGame.query.delete()
db.session.commit()
nfl.load_boxscores()
elif week is None:
models.NflGame.query.filter_by(season_value=int(year)).delete()
db.session.commit()
nfl.load_boxscores_per_year(int(year))
else:
models.NflGame.query.filter_by(
season_value=int(year), week_order=int(week)).delete()
db.session.commit()
nfl.load_boxscores_per_year(int(year), int(week))
@manager.command
def update_players():
dummy = models.FieldPlayer(espn_id=-1,
shark_id=-1,
name="dummy")
db.session.add(dummy)
players = models.FieldPlayer.query.all()
with open(app.config['PLAYERS_FILE']) as f:
r = csv.reader(f)
next(r)
for row in r:
sh = next((p for p in players if p.shark_id == int(row[1])),
None)
es = next((p for p in players if p.espn_id == int(row[0])),
None)
if sh:
if sh is es:
continue
dummy.shark_projections = sh.shark_projections
db.session.delete(sh)
if es:
dummy.espn_projections = es.espn_projections
db.session.delete(es)
db.session.commit()
p = models.FieldPlayer(espn_id=int(row[0]),
shark_id=int(row[1]),
name=row[2],
shark_projections=dummy.shark_projections,
espn_projections=dummy.espn_projections)
db.session.add(p)
db.session.commit()
db.session.delete(dummy)
db.session.commit()
@manager.command
def load_data():
delete_data()
with open(app.config['PLAYERS_FILE']) as f:
r = csv.reader(f)
next(r)
for row in r:
db.session.add(models.FieldPlayer(espn_id=int(row[0]),
shark_id=int(row[1]),
name=row[2]))
db.session.commit()
with open(app.config['TEAMS_FILE']) as f:
r = csv.reader(f)
next(r)
for row in r:
db.session.add(models.FieldTeam(espn_id=int(row[2]),
espn_code=row[1],
name=row[0],
shark_code=row[3]))
db.session.commit()
# BYE_STRING = "BYE"
# with open(app.config['SCHEDULE_FILE']) as f:
# r = csv.reader(f)
# r.next()
# for row in r:
# home = next(x for x in teams if x.espn_code == row[0])
# home.bye_week = row.index(BYE_STRING)
# for i in xrange(1, len(row)):
# away = next((x for x in teams if x.espn_code == row[i]), None)
# if away != None: db.session.add(models.NflGame(home, away, i))
# db.session.commit()
with open(app.config['POSITIONS_FILE']) as f:
r = csv.reader(f)
next(r)
positions = [models.Position(espn_code=row[0],
name=row[1], order=row[2]) for row in r]
for p in positions:
db.session.add(p)
db.session.commit()
# DEF_STRING = "D"
# FA_STRING = "FA"
# with open(app.config['PROJECTIONS_FILE']) as f:
# r = csv.reader(f)
# r.next()
# for row in r:
# if len(row) == 0:
# break
# if row[2] == FA_STRING:
# t = None
# else:
# t = next((x for x in teams if x.fs_name == row[2]), None)
# if row[3] == DEF_STRING:
# t.projected_defense_points = row[14]
# else:
# db.session.add(models.NflPlayer(row[1], t, [x for x in positions if x.code ==
# row[3]], row[14]))
# db.session.commit()
print("Loaded all reference data.")
if __name__ == '__main__':
manager.run()