-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathespn.py
179 lines (141 loc) · 5.54 KB
/
espn.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
"""
ESPN league wrapper
# TODO - handle draft auction values
"""
import datetime
import json
import os
import espn_api.baseball
import pandas
class Espn(espn_api.baseball.League):
def __init__(self, league_data_directory):
self.league_data_directory = league_data_directory
with open(os.path.join(self.league_data_directory, "config.json"), 'r') as f:
config = json.load(f)
super().__init__(**config)
self.possible_positions = ["C", "1B", "2B", "3B", "SS", "LF", "CF", "RF", "DH", "SP", "RP"]
def save_eligibilities(self):
"""Get all player positional eligibilities and save to CSV"""
data_date = datetime.date.today()
self.save_rosters(data_date)
self._process_free_agents(data_date)
columns = ['espn_name', 'espn_id', 'pro_team', 'espn_injury_status'] + self.possible_positions
players = pandas.concat([
self.rostered_players[columns],
self.free_agent_players[columns]
])
players.to_csv(os.path.join(
self.league_data_directory,
"espn_eligibilities.csv"
), columns=columns, encoding='utf8', index=False)
players.to_csv(os.path.join(
self.league_data_directory,
"historical",
f"espn_eligibilities_{data_date:%Y-%m-%d}.csv"
), columns=columns, encoding='utf8', index=False)
def save_rosters(self, data_date=datetime.date.today()):
"""Get all rostered players and save rosters to CSV"""
rostered_players = []
for team in self.teams:
for x in team.roster:
player = self._process_player(x, self.possible_positions)
player['fantasy_team_id'] = team.team_id
rostered_players.append(player)
self.rostered_players = pandas.DataFrame(rostered_players)
self.rostered_players.to_csv(os.path.join(
self.league_data_directory,
"rosters.csv"
), encoding='utf8', index=False)
self.rostered_players.to_csv(os.path.join(
self.league_data_directory,
"historical",
f"rosters_{data_date:%Y-%m-%d}.csv"
), encoding='utf8', index=False)
def _process_free_agents(self, data_date=datetime.date.today(), size=500):
"""Get all free agents"""
free_agent_players = []
for x in self.free_agents(size):
player = self._process_player(x, self.possible_positions)
free_agent_players.append(player)
self.free_agent_players = pandas.DataFrame(free_agent_players)
@staticmethod
def _process_player(player, possible_positions):
"""Convert from player class to player dict"""
player_dict = {
'espn_id': player.playerId,
'espn_name': player.name,
# 'espn_value': player_json['draftAuctionValue'],
'pro_team': player.proTeam,
'espn_injury_status': player.injuryStatus
}
for pos in possible_positions:
if pos in player.eligibleSlots:
player_dict[pos] = 1
else:
player_dict[pos] = 0
return player_dict
def save_scores(self):
"""
Saves all boxscores for current season
This is based on my code in https://github.com/cwendt94/espn-api/blob/19613d73c6476a78b3ccffd4e0e045c6e457cb62/espn_api/baseball/box_score.py
"""
scores = []
for i in range(1, self.currentMatchupPeriod):
weekly_box_scores = self.box_scores(i)
for box_score in weekly_box_scores:
if box_score.away_team:
# we only want competitive matchups, not byes
for as_home in (True, False):
scores.append(self._process_box_score(
box_score,
self.year,
i,
as_home=as_home)
)
scores = pandas.DataFrame(scores)
scores.to_csv(os.path.join(
self.league_data_directory,
"scores",
f"scores_{self.year}.csv"
), encoding='utf8', index=False)
@staticmethod
def _process_box_score(box_score, year, week, as_home=True):
"""Retrieve box scores and convert to dict"""
stats_mapping = [
('AB', 'AB'),
('H', 'H'),
('R', 'R'),
('HR', 'HR'),
('TB', 'TB'),
('RBI', 'RBI'),
('BB', 'B_BB'),
('SB', 'SB'),
('OBP', 'OBP'),
('IP', 'OUTS'),
('pH', 'P_H'),
('ER', 'ER'),
('pBB', 'P_BB'),
('W', 'W'),
('SV', 'SV'),
('ERA', 'ERA'),
('WHIP', 'WHIP'),
('K9', 'K/9')
]
if as_home:
row = {
'team_id': box_score.home_team.team_id,
'opponent_team_id': box_score.away_team.team_id
}
stats = box_score.home_stats
else:
row = {
'team_id': box_score.away_team.team_id,
'opponent_team_id': box_score.home_team.team_id
}
stats = box_score.away_stats
row['year'] = year
row['matchup_period'] = week
for stat in stats_mapping:
row[stat[0]] = stats[stat[1]]['value']
row['IP'] = str(int(row['IP'] / 3)) + '.' + str(int(row['IP'] % 3))
return row