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

Add contest data download functionality in app.py

Browse files

- Introduced a new function to download contest data from DraftKings, handling zip files and CSV extraction.
- Updated the user interface to allow selection between uploading a file or downloading contest data directly, enhancing user experience and flexibility.
- Improved error handling for download failures, ensuring users receive appropriate feedback.

Files changed (1) hide show
  1. app.py +52 -7
app.py CHANGED
@@ -7,6 +7,9 @@ from collections import Counter
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,6 +57,38 @@ def grab_contest_player_info(db, sport, type, contest_date, contest_name, contes
54
 
55
  return player_info, info_maps
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  db = init_conn()
58
 
59
  ## import global functions
@@ -116,19 +151,29 @@ with tab1:
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]}#/")
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
 
 
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
 
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
 
151
  else:
152
  pass
153
  with col2:
154
+ 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]}")
155
  if parse_type == 'Manual':
156
  if 'Contest_file_helper' in st.session_state:
157
  del st.session_state['Contest_file_helper']
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