Jon Solow commited on
Commit
8aedeba
·
1 Parent(s): ef5069d

Add live passing, rushing, and receiving stats

Browse files
Files changed (1) hide show
  1. src/stats.py +102 -1
src/stats.py CHANGED
@@ -1,5 +1,7 @@
1
  from dataclasses import dataclass
 
2
  import pandas as pd
 
3
  import streamlit as st
4
 
5
  from domain.playoffs import PLAYOFF_TEAM_DEF_PLAYER
@@ -187,8 +189,107 @@ def assemble_nflverse_stats() -> dict[int, dict[str, dict[str, float]]]:
187
 
188
 
189
  def get_live_stats() -> dict[int, dict[str, dict[str, float]]]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  stat_map: dict[int, dict[str, dict[str, float]]] = {w: {} for w in NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK.values()}
191
- # TODO - implement live stats
 
 
 
 
 
 
 
 
 
 
 
192
  return stat_map
193
 
194
 
 
1
  from dataclasses import dataclass
2
+ import json
3
  import pandas as pd
4
+ import requests
5
  import streamlit as st
6
 
7
  from domain.playoffs import PLAYOFF_TEAM_DEF_PLAYER
 
189
 
190
 
191
  def get_live_stats() -> dict[int, dict[str, dict[str, float]]]:
192
+ return get_yahoo_stats()
193
+
194
+
195
+ YAHOO_TO_STAT_MAP = {
196
+ "PASSING": {
197
+ "PASSING_YARDS": PASS_YD.key,
198
+ "PASSING_TOUCHDOWNS": PASS_TD.key,
199
+ "PASSING_INTERCEPTIONS": PASS_INT.key,
200
+ "FUMBLES_LOST": FUM_LOST.key,
201
+ },
202
+ "RUSHING": {
203
+ "RUSHING_TOUCHDOWNS": RUSH_TD.key,
204
+ "FUMBLES_LOST": FUM_LOST.key,
205
+ "RUSHING_YARDS": RUSH_YD.key,
206
+ },
207
+ "RECEIVING": {
208
+ "RECEPTIONS": RECEPTION.key,
209
+ "RECEIVING_YARDS": REC_YD.key,
210
+ "RECEIVING_TOUCHDOWNS": REC_TD.key,
211
+ "FUMBLES_LOST": FUM_LOST.key,
212
+ },
213
+ }
214
+
215
+
216
+ # HACK find permanent source of this map
217
+ YAHOO_PLAYER_ID_MAP = {
218
+ "30123": "00-0033873", # Patrick Mahomes
219
+ "28654": "00-0031687", # Raheem Mostert
220
+ "29399": "00-0033040", # Tyreek
221
+ "31093": "00-0034798", # Durham Smythe
222
+ "31199": "00-0034794", # Jason Sanders
223
+ "30142": "00-0033885", # David Njoku
224
+ "29792": "00-0032726", # Fairbairn
225
+ "34207": "00-0037197", # Pacheco
226
+ "26686": "00-0030506", # Kelce
227
+ "30346": "00-0033303", # Butker
228
+ }
229
+
230
+ # happens to be the same
231
+ YAHOO_WEEK_MAP = NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK
232
+
233
+
234
+ def add_yahoo_stat_type_to_stat_map(
235
+ stats_object, yahoo_stat_type: str, stat_map: dict[int, dict[str, dict[str, float]]]
236
+ ):
237
+ assert yahoo_stat_type in YAHOO_TO_STAT_MAP
238
+
239
+ for raw_week, week_dict in stats_object.items():
240
+ week = YAHOO_WEEK_MAP[int(raw_week)]
241
+ if week not in stat_map:
242
+ stat_map[week] = {}
243
+
244
+ week_leaders = week_dict["POSTSEASON"][""][f"{yahoo_stat_type}_YARDS"]["leagues"][0]["leagueWeeks"][0][
245
+ "leaders"
246
+ ]
247
+
248
+ for player in week_leaders:
249
+ raw_player_id = player["player"]["playerId"].split(".")[-1]
250
+ player_id = YAHOO_PLAYER_ID_MAP.get(raw_player_id, raw_player_id)
251
+ if player_id not in stat_map[week]:
252
+ stat_map[week][player_id] = {}
253
+ stats = player["stats"]
254
+ for stat in stats:
255
+ if stat_key := YAHOO_TO_STAT_MAP[yahoo_stat_type].get(stat["statId"]):
256
+ stat_map[week][player_id][stat_key] = float(stat["value"])
257
+ # else:
258
+ # # remove after mapping all intended
259
+ # stat_map[week][player_id][stat["statId"]] = stat["value"]
260
+
261
+
262
+ def get_yahoo_stat_json_obj():
263
+ url = "https://sports.yahoo.com/nfl/stats/weekly/?selectedTable=0"
264
+ request = requests.get(url)
265
+ request_content_str = request.text
266
+
267
+ start_str = """root.App.main = """
268
+ end_str = """;\n}(this));"""
269
+
270
+ start_slice_pos = request_content_str.find(start_str) + len(start_str)
271
+ first_slice = request_content_str[start_slice_pos:]
272
+ end_slice_pos = first_slice.find(end_str)
273
+ dom_str = first_slice[:end_slice_pos]
274
+ dom_json = json.loads(dom_str)
275
+ return dom_json
276
+
277
+
278
+ def get_yahoo_stats() -> dict[int, dict[str, dict[str, float]]]:
279
+ dom_json = get_yahoo_stat_json_obj()
280
  stat_map: dict[int, dict[str, dict[str, float]]] = {w: {} for w in NFLVERSE_STAT_WEEK_TO_PLAYOFF_WEEK.values()}
281
+
282
+ stats_json = dom_json["context"]["dispatcher"]["stores"]["GraphStatsStore"]
283
+
284
+ add_yahoo_stat_type_to_stat_map(stats_json["weeklyStatsFootballPassing"]["nfl"]["200"]["2023"], "PASSING", stat_map)
285
+ add_yahoo_stat_type_to_stat_map(stats_json["weeklyStatsFootballRushing"]["nfl"]["200"]["2023"], "RUSHING", stat_map)
286
+ add_yahoo_stat_type_to_stat_map(
287
+ stats_json["weeklyStatsFootballReceiving"]["nfl"]["200"]["2023"], "RECEIVING", stat_map
288
+ )
289
+ kicking_stats = stats_json["weeklyStatsFootballKicking"]["nfl"]["200"]["2023"]
290
+ return_stats = stats_json["weeklyStatsFootballReturns"]["nfl"]["200"]["2023"]
291
+ defense_stats = stats_json["weeklyStatsFootballDefense"]["nfl"]["200"]["2023"]
292
+
293
  return stat_map
294
 
295