James McCool commited on
Commit
5070c04
·
1 Parent(s): 48aa05f

Enhance download functionality for contest data in app.py

Browse files

- Added a browser selection option for downloading contest data, allowing users to choose between Chrome, Firefox, and Edge for cookie retrieval.
- Updated the cookie retrieval logic to accommodate the selected browser, improving the reliability of the download process.
- Enhanced error handling to provide clearer feedback when cookie access fails or when the downloaded file format is incorrect, ensuring a better user experience.

Files changed (1) hide show
  1. app.py +37 -23
app.py CHANGED
@@ -132,36 +132,50 @@ with tab1:
132
  download_col, upload_col = st.columns(2)
133
  with download_col:
134
  st.markdown("### Download Option")
 
135
  if st.button("Download from DraftKings"):
136
  try:
 
137
  # Get the download URL
138
  download_url = f"https://www.draftkings.com/contest/exportfullstandingscsv/{contest_id_map[contest_name_var]}"
139
 
140
- # Get cookies from the user's browser
141
- cookies = browser_cookie3.load(domain_name='.draftkings.com')
142
- cookie_dict = {cookie.name: cookie.value for cookie in cookies}
143
-
144
- # Attempt to download using requests with browser cookies
145
- headers = {
146
- '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'
147
- }
148
- response = requests.get(download_url, cookies=cookie_dict, headers=headers)
149
-
150
- if response.status_code == 200:
151
- # Check if it's a zip file
152
- if 'application/zip' in response.headers.get('content-type', ''):
153
- # Extract CSV from zip
154
- with zipfile.ZipFile(BytesIO(response.content)) as zip_ref:
155
- # Get the first CSV file in the zip
156
- csv_filename = [f for f in zip_ref.namelist() if f.endswith('.csv')][0]
157
- with zip_ref.open(csv_filename) as csv_file:
158
- st.session_state['Contest_file'] = pd.read_csv(csv_file)
159
- st.success("Successfully downloaded and loaded contest data!")
 
 
 
 
 
 
 
 
 
160
  else:
161
- st.error("Downloaded file is not in the expected format. Please use manual upload instead.")
162
- else:
163
- st.error(f"Failed to download (Status code: {response.status_code}). You may need to log into DraftKings in your browser first.")
 
 
164
  st.info("Please use the manual upload option instead.")
 
165
  except Exception as e:
166
  st.error(f"Error during download: {str(e)}")
167
  st.info("Please use the manual upload option instead.")
 
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.")