James McCool
commited on
Commit
·
48aa05f
1
Parent(s):
1cbe27c
Implement download option for contest data in app.py
Browse files- Added functionality to download contest data directly from DraftKings using user browser cookies, enhancing user experience by simplifying data retrieval.
- Introduced a manual upload option for contest files, allowing users to upload CSV or Excel files if needed.
- Updated the user interface to include separate columns for download and upload options, improving layout and accessibility.
- app.py +54 -7
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -7,6 +7,10 @@ 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 |
|
|
@@ -116,19 +120,62 @@ 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 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
st.
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
from io import BytesIO
|
| 12 |
+
import zipfile
|
| 13 |
+
import browser_cookie3
|
| 14 |
|
| 15 |
def init_conn():
|
| 16 |
|
|
|
|
| 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 |
+
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.")
|
| 168 |
+
|
| 169 |
+
with upload_col:
|
| 170 |
+
st.markdown("### Manual Upload Option")
|
| 171 |
+
st.session_state['Contest_upload'] = st.file_uploader("Upload Contest File (CSV or Excel)", type=['csv', 'xlsx', 'xls'])
|
| 172 |
+
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)
|
| 173 |
+
if st.session_state['Contest_upload'] is not None:
|
| 174 |
+
try:
|
| 175 |
+
st.session_state['Contest_file'] = pd.read_csv(st.session_state['Contest_upload'])
|
| 176 |
+
st.success("Successfully loaded contest data!")
|
| 177 |
+
except:
|
| 178 |
+
st.warning('Please upload a valid Contest CSV')
|
| 179 |
else:
|
| 180 |
pass
|
| 181 |
|
requirements.txt
CHANGED
|
@@ -6,4 +6,5 @@ rapidfuzz
|
|
| 6 |
pulp
|
| 7 |
docker
|
| 8 |
scipy
|
| 9 |
-
pymongo
|
|
|
|
|
|
| 6 |
pulp
|
| 7 |
docker
|
| 8 |
scipy
|
| 9 |
+
pymongo
|
| 10 |
+
browser-cookie3
|