-
Notifications
You must be signed in to change notification settings - Fork 0
/
assemble_data.py
262 lines (196 loc) · 9.13 KB
/
assemble_data.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# -*- coding: utf-8 -*-
#import csv, json, requests, time
import csv, json, numpy, xmltodict, time
from datetime import datetime
from operator import itemgetter
from yahooapi import YahooAPI
keyfile = 'secrets.txt'
tokenfile = 'tokenfile.txt'
SEED_LEAGUE_KEY = '331.l.1098504' # current Kimball leauge, build list of leagues based on this # 2014
SEED_LEAGUE_KEY = '348.l.1044567' # 2015
OUTPUT_CSV_PATH = 'C:\\Users\\Peter\\Dropbox\\ff\\data\\'
#OUTPUT_CSV_PATH = 'data/'
api = YahooAPI(keyfile, tokenfile)
url = 'http://fantasysports.yahooapis.com/fantasy/v2/users;use_login=1/games;game_keys=nfl/leagues'
users_uri = 'http://fantasysports.yahooapis.com/fantasy/v2/users;use_login=1/games'
teams_uri = 'http://fantasysports.yahooapis.com/fantasy/v2/users;use_login=1/teams'
nfl_games = 'http://fantasysports.yahooapis.com/fantasy/v2/game/nfl'
numpy.set_printoptions(precision=4)
def write_csv_file(filename, year, players):
filepath = OUTPUT_CSV_PATH + year + '-' + filename + '.csv'
print str(datetime.now()) + ' Writing file {0}'.format(filepath)
with open(filepath, 'w+') as f:
writer = csv.writer(f, quotechar='"', quoting=csv.QUOTE_MINIMAL, lineterminator = '\n')
writer.writerow([
'YEAR',
'FILENAME',
'PLAYER_KEY',
'PLAYER_NAME',
'SEASON_TOTAL',
'CALCULATED_SEASON_TOTAL',
'MEAN',
'MEDIAN',
'STD_DEVIATION',
'CV',
'MEAN_RANK',
'CV_RANK',
'PERFORMANCE_SCORE',
'PERFORMANCE_RANK'
] + ['WK{0}'.format(i) for i in range(1, len(players[0]['scores'])+1)])
for player in players:
writer.writerow([
year,
filename,
player['player_key'],
player['player_name'],
player['season_total'],
player['calculated_season_total'],
player['mean'],
player['median'],
player['std_deviation'],
player['coefficient_of_variation'],
player['mean_rank'],
player['cv_rank'],
player['performance_score'],
player['performance_rank'],
] + player['scores'])
def get_players(league_key, position = None):
uri = 'http://fantasysports.yahooapis.com/fantasy/v2/league/' + league_key + '/players;sort=PTS;sort_type=season'
# uri = 'http://fantasysports.yahooapis.com/fantasy/v2/league/' + league_key + '/players;sort=PTS;sort_type=season;count=5'
# ;start=26 would get the next set
# so we need to loop through 4 times to get the top 100 of anything
startPos = ['0', '25', '50', '75']
#startPos = ['75']
if position:
uri += ';position=' + position
rtn = []
for pos in startPos:
r = api.request(uri + ';start=' + pos)
print str(datetime.now()) + ' ' + uri + ';start=' + pos
if r.status_code == 200:
result = xmltodict.parse(r.text)
# print r.text
players = []
if result['fantasy_content']['league']['players']:
players = result['fantasy_content']['league']['players']['player']
if not isinstance(players, list): players = [players]
end_week = int(result['fantasy_content']['league']['current_week'])
print end_week
if end_week < 16:
end_week = end_week - 1
limit_debug = False
x = 1
for player in players:
if (x <= 1 and limit_debug == True) or limit_debug == False:
print str(datetime.now()) + ' Looking up scores for ' + player['player_key'] + ' ' + player['name']['full']
pdict = {
'player_key': player['player_key'],
'player_name': player['name']['full'],
'season_total': get_player_overall_stats(league_key, player['player_key'])
}
# print player['player_key']
# print player['name']['full']
# # for each player, assemble a score for each week
scores = []
for i in range(1, end_week+1):
#print 'Looking up week ' + str(i) + ' score for ' + player['player_key'] + ' ' + player['name']['full']
player_stats = get_player_stats(league_key, player['player_key'], i)
if player_stats:
scores.append(player_stats)
else:
scores.append(None)
scores_list = [float(item) for item in scores]
a = numpy.array(scores_list)
pdict['scores'] = scores
pdict['calculated_season_total'] = sum(float(item) for item in scores)
#pdict['averageold'] = sum(float(item) for item in scores) / len(scores)
pdict['mean'] = numpy.around(numpy.mean(a), decimals=4)
pdict['median'] = numpy.median(a)
pdict['std_deviation'] = numpy.std(a)
pdict['coefficient_of_variation'] = pdict['std_deviation'] / pdict['mean'] # sd / mean # lower is better, less risk/volatility
#pdict['performance_score'] = pdict['coefficient_of_variation'] * pdict['mean']
rtn.append(pdict)
x += 1
else:
print 'Error: ' + str(r.status_code)
print r.text
return rtn
def get_player_overall_stats(league_key, player_key):
uri = 'http://fantasysports.yahooapis.com/fantasy/v2/league/' + league_key + '/players;player_keys=' + player_key + '/stats'
r = api.request(uri)
if r.status_code == 200:
result = xmltodict.parse(r.text)
#print r.text
weekly_score = result['fantasy_content']['league']['players']['player']['player_points']['total']
return weekly_score
else:
print 'Error: ' + str(r.status_code)
print r.text
return None
def get_player_overall_stats2(league_key, player_key):
uri = 'http://fantasysports.yahooapis.com/fantasy/v2/league/' + league_key + '/players;player_keys=' + player_key + '/stats'
r = api.request(uri)
if r.status_code == 200:
result = xmltodict.parse(r.text)
print r.text
weekly_score = result['fantasy_content']['league']['players']['player']['player_points']['total']
return weekly_score
else:
print 'Error: ' + str(r.status_code)
print r.text
return None
def get_player_stats(league_key, player_key, week):
uri = 'http://fantasysports.yahooapis.com/fantasy/v2/league/' + league_key + '/players;player_keys=' + player_key + '/stats;type=week;week=' + str(week)
retries = 5
while(retries >=0):
r = api.request(uri)
if r.status_code == 200:
result = xmltodict.parse(r.text)
#print r.text
weekly_score = result['fantasy_content']['league']['players']['player']['player_points']['total']
return weekly_score
else:
print 'Error: ' + str(r.status_code)
print r.text
print 'Retrying...'
retries = retries - 1
time.sleep(2)
return -99
'''
For a given year (league ID), go through each position and assemble top players' weekly average scores
'''
positions = ['QB', 'RB', 'WR', 'TE', 'K', 'DEF']
positions = ['QB', 'RB']
for position in positions:
players = get_players(SEED_LEAGUE_KEY, position)
# sort list by mean, then add rank value (highest is better)
players = sorted(players, key=itemgetter('mean'), reverse=True)
i = 1
for player in players:
player['mean_rank'] = i
i += 1
# sort list by cv, then add rank value (lowest value is better)
players = sorted(players, key=itemgetter('coefficient_of_variation'))
i = 1
for player in players:
player['cv_rank'] = i if player['coefficient_of_variation'] > 0 else 100
i +=1
# calculate average rank, place in performance value
for player in players:
player['performance_score'] = ((4 *player['mean_rank']) + player['cv_rank']) / 2
# sort by overall performance
players = sorted(players, key=itemgetter('performance_score'))
i = 1
for player in players:
player['performance_rank'] = i
i +=1
for player in players:
print player
# write csv
# print '{1} Writing CSV file to {0}'.format(OUTPUT_CSV_NAME, str(datetime.now()))
write_csv_file(position, '2015', players)
# aaron rodgers 331.p.7200
#foo = get_player_overall_stats2('331.l.1098504', '331.p.7200')
# player stats http://fantasysports.yahooapis.com/fantasy/v2/player/223.p.5479/stats
# http://fantasysports.yahooapis.com/fantasy/v2/league/223.l.431/players;player_keys=223.p.5479/stats
# /fantasy/v2/player/{player_key}/stats;type=week;week={week}