James McCool commited on
Commit
8794602
·
1 Parent(s): 2f88441

Refactor contest data handling in app.py

Browse files

- Removed the download functionality for contest data from DraftKings, streamlining the user interface.
- Simplified the file upload process by directly allowing users to upload contest files without the download option.
- Enhanced error handling for file uploads to improve user feedback when invalid files are uploaded.

Files changed (1) hide show
  1. app.py +6 -51
app.py CHANGED
@@ -7,9 +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
- import zipfile
12
- import io
13
 
14
  def init_conn():
15
 
@@ -57,38 +54,6 @@ def grab_contest_player_info(db, sport, type, contest_date, contest_name, contes
57
 
58
  return player_info, info_maps
59
 
60
- def download_draftkings_contest(contest_id):
61
- try:
62
- # Construct the URL
63
- url = f"https://www.draftkings.com/contest/exportfullstandingscsv/{contest_id}"
64
-
65
- # Download the file
66
- response = requests.get(url)
67
- response.raise_for_status() # Raise an exception for bad status codes
68
-
69
- # Create a BytesIO object from the content
70
- zip_file = io.BytesIO(response.content)
71
-
72
- # Open the zip file
73
- with zipfile.ZipFile(zip_file) as z:
74
- # Get the first CSV file in the zip (there should only be one)
75
- csv_filename = z.namelist()[0]
76
- # Read the CSV file into a pandas DataFrame
77
- with z.open(csv_filename) as f:
78
- df = pd.read_csv(f)
79
-
80
- return df
81
-
82
- except requests.exceptions.RequestException as e:
83
- st.error(f"Error downloading contest data: {str(e)}")
84
- return None
85
- except zipfile.BadZipFile:
86
- st.error("The downloaded file is not a valid zip file")
87
- return None
88
- except Exception as e:
89
- st.error(f"An error occurred: {str(e)}")
90
- return None
91
-
92
  db = init_conn()
93
 
94
  ## import global functions
@@ -158,22 +123,12 @@ with tab1:
158
  if 'Contest_file' in st.session_state:
159
  del st.session_state['Contest_file']
160
  if 'Contest_file' not in st.session_state:
161
- download_option = st.radio("Choose input method:", ["Upload File", "Download from DraftKings"], key='download_option')
162
-
163
- if download_option == "Upload File":
164
- st.session_state['Contest_upload'] = st.file_uploader("Upload Contest File (CSV or Excel)", type=['csv', 'xlsx', 'xls'])
165
- 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)
166
- try:
167
- st.session_state['Contest_file'] = pd.read_csv(st.session_state['Contest_upload'])
168
- except:
169
- st.warning('Please upload a Contest CSV')
170
- else:
171
- if st.button("Download Contest Data"):
172
- with st.spinner("Downloading contest data..."):
173
- 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)
174
- st.session_state['Contest_file'] = download_draftkings_contest(contest_id_map[contest_name_var])
175
- if st.session_state['Contest_file'] is not None:
176
- st.success("Contest data downloaded successfully!")
177
  else:
178
  pass
179
 
 
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
 
 
54
 
55
  return player_info, info_maps
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  db = init_conn()
58
 
59
  ## import global functions
 
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