James McCool commited on
Commit
fbbf9c1
·
1 Parent(s): d17c4ea

Add functionality to grab and update player SR_IDs in app.py, integrating API calls for team rosters and enhancing data management in the NFL_SR_IDs worksheet.

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py CHANGED
@@ -31,6 +31,7 @@ gc = gspread.service_account_from_dict(credentials)
31
 
32
  NCAAF_model_url = st.secrets['NCAAF_model_url']
33
  pff_url = st.secrets['pff_url']
 
34
 
35
  headers = {
36
  'Authorization': 'Bearer 6984da1f-2c81-4140-8206-d018af38533f',
@@ -41,6 +42,63 @@ proj_source_options = ['Dwain', 'Aggregate', 'Freedman', 'Kickers', 'Defenses']
41
  proj_replace_options = ['dwain', 'aggregate', 'freedman', 'kickers', 'defenses']
42
  api_replace_options = ['dwain', 'freedman', 'ian', 'waz', 'charch', 'kendall', 'sam', 'berry', 'james', 'james_ncaaf']
43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  tab1, tab2, tab3, tab4, tab5 = st.tabs(['Rankings Update', 'Projections Update', 'Game Model Update', 'Rest of Season Update', 'NCAAF Script'])
45
 
46
  with tab1:
@@ -110,6 +168,17 @@ with tab2:
110
  if response.status_code == 200:
111
  st.write("Uploading!")
112
  st.write(response.json())
 
 
 
 
 
 
 
 
 
 
 
113
 
114
  with tab3:
115
  st.info("We will add this functionality as soon as we have the endpoints to make it work!")
 
31
 
32
  NCAAF_model_url = st.secrets['NCAAF_model_url']
33
  pff_url = st.secrets['pff_url']
34
+ FL_api_hub_url = st.secrets['FL_master_api_hub']
35
 
36
  headers = {
37
  'Authorization': 'Bearer 6984da1f-2c81-4140-8206-d018af38533f',
 
42
  proj_replace_options = ['dwain', 'aggregate', 'freedman', 'kickers', 'defenses']
43
  api_replace_options = ['dwain', 'freedman', 'ian', 'waz', 'charch', 'kendall', 'sam', 'berry', 'james', 'james_ncaaf']
44
 
45
+ def grab_sr_ids(headers: dict) -> pd.DataFrame:
46
+ sr_injuries = "https://api.sportradar.com/nfl/official/trial/v7/en/league/hierarchy.json"
47
+ response = requests.get(sr_injuries, headers=headers)
48
+ data = response.json()
49
+
50
+ print(data)
51
+
52
+ team_ids = []
53
+ player_fa_ids = []
54
+ player_sr_ids = []
55
+ player_first_names = []
56
+ player_prefer_first_name = []
57
+ player_last_name = []
58
+ player_name_suffix = []
59
+ player_position = []
60
+ player_team = []
61
+
62
+ row_data = []
63
+
64
+ for conference in data['conferences']:
65
+ for division in conference['divisions']:
66
+ for team in division['teams']:
67
+ team_ids.append(team['id'])
68
+
69
+ print(team_ids)
70
+
71
+ for team_id in team_ids:
72
+
73
+ sr_injuries = f"https://api.sportradar.com/nfl/official/trial/v7/en/teams/{team_id}/full_roster.json"
74
+ response = requests.get(sr_injuries, headers=headers)
75
+ data = response.json()
76
+
77
+ team_name = data['alias']
78
+
79
+ st.write(team_name + ' - ' + team_id + ' being processed')
80
+ try:
81
+ for player in data['players']:
82
+ player_fa_ids.append(999999)
83
+ player_sr_ids.append(player['id'])
84
+ player_first_names.append(player['first_name'])
85
+ player_prefer_first_name.append('-')
86
+ player_last_name.append(player['last_name'])
87
+ player_name_suffix.append('-')
88
+ player_position.append(player['position'])
89
+ player_team.append(team_name)
90
+
91
+ row_data.append([
92
+ 999999, player['id'], player['first_name'], '-', player['last_name'], '-', player['position'], team_name
93
+ ])
94
+ except:
95
+ st.write(f"Error processing team: {team_name} - {team_id}")
96
+ continue
97
+
98
+ st.write(pd.DataFrame(row_data, columns=['FA_ID', 'SR_ID', 'First_Name', 'Prefer_First_Name', 'Last_Name', 'Name_Suffix', 'Position', 'Team']).head(100))
99
+
100
+ return pd.DataFrame(row_data, columns=['FA_ID', 'SR_ID', 'First_Name', 'Prefer_First_Name', 'Last_Name', 'Name_Suffix', 'Position', 'Team'])
101
+
102
  tab1, tab2, tab3, tab4, tab5 = st.tabs(['Rankings Update', 'Projections Update', 'Game Model Update', 'Rest of Season Update', 'NCAAF Script'])
103
 
104
  with tab1:
 
168
  if response.status_code == 200:
169
  st.write("Uploading!")
170
  st.write(response.json())
171
+
172
+ with col4:
173
+ st.info("Update SR_ID list")
174
+ if st.button("Update SR_ID list", key='sr_id_list'):
175
+ player_df = grab_sr_ids(headers)
176
+
177
+ sh = gc.open_by_url(FL_api_hub_url)
178
+ worksheet = sh.worksheet('NFL_SR_IDs')
179
+ worksheet.batch_clear(['A:H'])
180
+ worksheet.update([player_df.columns.values.tolist()] + player_df.values.tolist())
181
+ st.write("Uploaded SR_ID list")
182
 
183
  with tab3:
184
  st.info("We will add this functionality as soon as we have the endpoints to make it work!")