James McCool commited on
Commit
6a6220a
·
1 Parent(s): 5070c04

Refactor contest data handling in app.py

Browse files

- Removed the download option for contest data, streamlining the user interface and focusing on manual upload functionality.
- Updated the information message to include a direct download link for contest results, enhancing user accessibility to data.
- Simplified the manual upload process by directly integrating file upload handling into the main logic, improving overall user experience.

Files changed (1) hide show
  1. app.py +7 -68
app.py CHANGED
@@ -7,10 +7,6 @@ from collections import Counter
7
  from pymongo.mongo_client import MongoClient
8
  from pymongo.server_api import ServerApi
9
  from datetime import datetime
10
- import requests
11
- from io import BytesIO
12
- import zipfile
13
- import browser_cookie3
14
 
15
  def init_conn():
16
 
@@ -120,76 +116,19 @@ with tab1:
120
  else:
121
  pass
122
  with col2:
123
- st.info(f"If you are manually loading and do not have the results CSV for the contest you selected, you can find it here: https://www.draftkings.com/contest/gamecenter/{contest_id_map[contest_name_var]}#/")
124
-
125
  if parse_type == 'Manual':
126
  if 'Contest_file_helper' in st.session_state:
127
  del st.session_state['Contest_file_helper']
128
  if 'Contest_file' in st.session_state:
129
  del st.session_state['Contest_file']
130
  if 'Contest_file' not in st.session_state:
131
- # Add download option
132
- download_col, upload_col = st.columns(2)
133
- with download_col:
134
- st.markdown("### Download Option")
135
- browser_select = st.selectbox("Select your browser", ['Chrome', 'Firefox', 'Edge'])
136
- if st.button("Download from DraftKings"):
137
- try:
138
-
139
- # Get the download URL
140
- download_url = f"https://www.draftkings.com/contest/exportfullstandingscsv/{contest_id_map[contest_name_var]}"
141
-
142
- # Get cookies from the user's selected browser
143
- try:
144
- if browser_select == 'Chrome':
145
- cookies = browser_cookie3.chrome(domain_name='.draftkings.com')
146
- elif browser_select == 'Firefox':
147
- cookies = browser_cookie3.firefox(domain_name='.draftkings.com')
148
- else: # Edge
149
- cookies = browser_cookie3.edge(domain_name='.draftkings.com')
150
-
151
- cookie_dict = {cookie.name: cookie.value for cookie in cookies}
152
-
153
- # Attempt to download using requests with browser cookies
154
- headers = {
155
- 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
156
- }
157
- response = requests.get(download_url, cookies=cookie_dict, headers=headers)
158
-
159
- if response.status_code == 200:
160
- # Check if it's a zip file
161
- if 'application/zip' in response.headers.get('content-type', ''):
162
- # Extract CSV from zip
163
- with zipfile.ZipFile(BytesIO(response.content)) as zip_ref:
164
- # Get the first CSV file in the zip
165
- csv_filename = [f for f in zip_ref.namelist() if f.endswith('.csv')][0]
166
- with zip_ref.open(csv_filename) as csv_file:
167
- st.session_state['Contest_file'] = pd.read_csv(csv_file)
168
- st.success("Successfully downloaded and loaded contest data!")
169
- else:
170
- st.error("Downloaded file is not in the expected format. Please use manual upload instead.")
171
- else:
172
- st.error(f"Failed to download (Status code: {response.status_code}). You may need to log into DraftKings in your browser first.")
173
- st.info("Please use the manual upload option instead.")
174
-
175
- except browser_cookie3.BrowserCookieError as e:
176
- st.error(f"Could not access cookies from {browser_select}. Make sure you're logged into DraftKings in {browser_select}.")
177
- st.info("Please use the manual upload option instead.")
178
-
179
- except Exception as e:
180
- st.error(f"Error during download: {str(e)}")
181
- st.info("Please use the manual upload option instead.")
182
-
183
- with upload_col:
184
- st.markdown("### Manual Upload Option")
185
- st.session_state['Contest_upload'] = st.file_uploader("Upload Contest File (CSV or Excel)", type=['csv', 'xlsx', 'xls'])
186
- st.session_state['player_info'], st.session_state['info_maps'] = grab_contest_player_info(db, sport_select, type_var, date_select, contest_name_var, contest_id_map)
187
- if st.session_state['Contest_upload'] is not None:
188
- try:
189
- st.session_state['Contest_file'] = pd.read_csv(st.session_state['Contest_upload'])
190
- st.success("Successfully loaded contest data!")
191
- except:
192
- st.warning('Please upload a valid Contest CSV')
193
  else:
194
  pass
195
 
 
7
  from pymongo.mongo_client import MongoClient
8
  from pymongo.server_api import ServerApi
9
  from datetime import datetime
 
 
 
 
10
 
11
  def init_conn():
12
 
 
116
  else:
117
  pass
118
  with col2:
119
+ st.info(f"If you are manually loading and do not have the results CSV for the contest you selected, you can find it here: https://www.draftkings.com/contest/gamecenter/{contest_id_map[contest_name_var]}#/, or you can initiate a download with this link: https://www.draftkings.com/contest/exportfullstandingscsv/{contest_id_map[contest_name_var]}")
 
120
  if parse_type == 'Manual':
121
  if 'Contest_file_helper' in st.session_state:
122
  del st.session_state['Contest_file_helper']
123
  if 'Contest_file' in st.session_state:
124
  del st.session_state['Contest_file']
125
  if 'Contest_file' not in st.session_state:
126
+ st.session_state['Contest_upload'] = st.file_uploader("Upload Contest File (CSV or Excel)", type=['csv', 'xlsx', 'xls'])
127
+ st.session_state['player_info'], st.session_state['info_maps'] = grab_contest_player_info(db, sport_select, type_var, date_select, contest_name_var, contest_id_map)
128
+ try:
129
+ st.session_state['Contest_file'] = pd.read_csv(st.session_state['Contest_upload'])
130
+ except:
131
+ st.warning('Please upload a Contest CSV')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  else:
133
  pass
134