Spaces:
Build error
Build error
File size: 17,765 Bytes
27154c4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | from pymongo import MongoClient
from pymongo.server_api import ServerApi
from curl_cffi import requests
from pandas import DataFrame
from pandas import concat as pd_concat
from numpy import where as np_where
from numpy import nan as np_nan
from datetime import datetime
from datetime import date
from datetime import timedelta
from pytz import timezone as pytz_timezone
import time
import random
uri = "mongodb+srv://multichem:Xr1q5wZdXPbxdUmJ@testcluster.lgwtp5i.mongodb.net/?retryWrites=true&w=majority&appName=TestCluster"
# Try to connect with error handling
try:
# First attempt: Using SRV format
client = MongoClient(uri, server_api=ServerApi('1'))
# Test the connection
client.admin.command('ping')
print("Pinged your deployment. You successfully connected to MongoDB!")
except Exception as e:
print(f"SRV connection failed: {e}")
try:
# Second attempt: Try direct connection format
direct_uri = uri.replace('mongodb+srv://', 'mongodb://')
client = MongoClient(direct_uri, server_api=ServerApi('1'))
# Test the connection
client.admin.command('ping')
print("Pinged your deployment using direct connection!")
except Exception as e:
print(f"Direct connection failed: {e}")
raise
def grab_contest_ids():
db = client['Contest_Information']
for sport in ['MLB', 'NBA', 'NHL', 'NFL', 'GOLF', 'MMA', 'CFB', 'LOL', 'CS', 'COD', 'SOC', 'F1', 'NAS', 'UFL', 'TEN', 'WNBA']:
# for sport in ['MLB', 'NFL']:
for contest_type_var in ['Classic', 'Showdown Captain Mode']:
if sport != 'WNBA':
response = requests.get(f'https://www.draftkings.com/lobby/getcontests?sport={sport}', impersonate="chrome")
else:
response = requests.get(f'https://www.draftkings.com/lobby/getcontests?sport=NBA', impersonate="chrome")
data = response.json()
contest_import = None
player_import = None
payout_import = None
if contest_type_var == 'Classic':
contest_collection = db[f'{sport}_reg_contest_info']
player_collection = db[f'{sport}_reg_player_info']
payout_collection = db[f'{sport}_reg_payout_info']
cursor = contest_collection.find()
try:
contest_import = DataFrame(list(cursor)).drop('_id', axis=1)
except Exception as e:
print(f"Classic Contest Importing Error: {e}")
cursor = player_collection.find()
try:
player_import = DataFrame(list(cursor)).drop('_id', axis=1)
except Exception as e:
print(f"Classic Player Importing Error: {e}")
cursor = payout_collection.find()
try:
payout_import = DataFrame(list(cursor)).drop('_id', axis=1)
except Exception as e:
print(f"Classic Payout Importing Error: {e}")
instances = 25
elif contest_type_var == 'Showdown Captain Mode':
contest_collection = db[f'{sport}_sd_contest_info']
player_collection = db[f'{sport}_showdown_player_info']
payout_collection = db[f'{sport}_showdown_payout_info']
cursor = contest_collection.find()
try:
contest_import = DataFrame(list(cursor)).drop('_id', axis=1)
except Exception as e:
print(f"Showdown Contest Importing Error: {e}")
cursor = player_collection.find()
try:
player_import = DataFrame(list(cursor)).drop('_id', axis=1)
except Exception as e:
print(f"Showdown Player Importing Error: {e}")
cursor = payout_collection.find()
try:
payout_import = DataFrame(list(cursor)).drop('_id', axis=1)
except Exception as e:
print(f"Showdown Payout Importing Error: {e}")
instances = 25
contest_overall = []
payouts_overall = DataFrame(columns=['Date', 'Contest Date', 'Contest ID', 'Entry Fee', 'minPosition', 'maxPosition', 'value'])
player_overall = DataFrame(columns=['Date', 'Contest Date', 'Contest ID', 'draftableId', 'First Name', 'Last Name', 'Display Name', 'Position', 'Team', 'Opp', 'Salary', 'Avg FPTS', 'Sport'])
try:
if sport != 'WNBA':
if sport == 'TEN':
classic_contests = [contest for contest in data['Contests']
if contest.get('gameType') == 'Single Match' or contest.get('gameType') == 'Short Slate' and '-Player' not in contest.get('n') and '50-50' not in contest.get('n')]
else:
classic_contests = [contest for contest in data['Contests']
if contest.get('gameType') == 'Classic' and '-Player' not in contest.get('n') and '50-50' not in contest.get('n')]
else:
classic_contests = [contest for contest in data['Contests']
if contest.get('gameType') == 'WNBA' and '-Player' not in contest.get('n') and '50-50' not in contest.get('n')]
# Sort classic contests by 'po' element in descending order (highest first)
classic_contests.sort(key=lambda x: x.get('po', 0), reverse=True)
# Filter out contests with "-Player" afterwards
classic_contests = [contest for contest in classic_contests if '-Player' not in contest.get('n') and '50-50' not in contest.get('n') and 'Winner Take All' not in contest.get('n')]
except Exception as e:
print(f"Classic Contest Error: {e}")
if not classic_contests:
print('No Classic Contests Found')
try:
if sport != 'WNBA':
showdown_contests = [contest for contest in data['Contests']
if contest.get('gameType') == 'Showdown Captain Mode' or contest.get('gameType') == 'Showdown' and '-Player' not in contest.get('n') and '50-50' not in contest.get('n')]
else:
showdown_contests = [contest for contest in data['Contests']
if 'WNBA' in contest.get('n', '') and '-Player' not in contest.get('n') and '50-50' not in contest.get('n') and contest.get('gameType') == 'Showdown Captain Mode']
# Sort showdown contests by 'po' element in descending order (highest first)
showdown_contests.sort(key=lambda x: x.get('po', 0), reverse=True)
# Filter out contests with "-Player" afterwards
showdown_contests = [contest for contest in showdown_contests if '-Player' not in contest.get('n') and '50-50' not in contest.get('n') and 'Winner Take All' not in contest.get('n')]
except Exception as e:
print(f"Showdown Contest Error: {e}")
if not showdown_contests:
print('No Showdown Contests Found')
for contest_num in range(0,instances):
try:
contest_info = []
if contest_type_var == 'Classic':
# Then get the nth contest (if it exists)
if contest_num < len(classic_contests):
contests = classic_contests[contest_num]
else:
break # No more contests to process
elif contest_type_var == 'Showdown Captain Mode':
# Then get the nth contest (if it exists)
if contest_num < len(showdown_contests):
contests = showdown_contests[contest_num]
else:
break # No more contests to process
# Get current time in EST
est = pytz_timezone('US/Eastern')
current_time = datetime.now(est)
# If before 8pm EST, use today, otherwise use tomorrow
if current_time.hour < 20:
current_date = str(current_time.date()).replace('-', '')
else:
tomorrow = current_time + timedelta(days=1)
current_date = str(tomorrow.date()).replace('-', '')
contest_name = contests.get('n')
contest_type = contests.get('gameType')
contest_id = contests.get('id')
contest_date = str((datetime.fromtimestamp(int(contests.get('sd')[6:-2]) / 1000)).strftime('%Y-%m-%d')).replace('-', '')
contest_url = 'https://dh5nxc6yx3kwy.cloudfront.net/contests/'+str(sport.lower())+'/'+str(contest_date)+'/'+str(contest_id)+'/data/'
for data_type in [current_date, contest_date, contest_name, contest_type, contest_id, contest_url, sport]:
contest_info.append(data_type)
contest_overall.append(contest_info)
time.sleep(.5 + random.random())
contest_specs_url = 'https://api.draftkings.com/contests/v1/contests/'+str(contest_id)+'?format=json'
response = requests.get(contest_specs_url, impersonate="chrome")
contest_specs_data = response.json()
contests = contest_specs_data['contestDetail']
draftGroup = contests.get('draftGroupId')
entry_fee = contests.get('entryFee')
payouts_data = contests.get('payoutSummary')
payouts = [
{
'Date': current_date,
'Contest Date': contest_date,
'Contest ID': contest_id,
'Entry Fee': entry_fee,
'minPosition': payout['minPosition'],
'maxPosition': payout['maxPosition'],
'value': payout.get('payoutDescriptions', [{}])[0].get('value', 0) if payout.get('payoutDescriptions') else 0
}
for payout in payouts_data
]
payout_info = DataFrame(payouts)
payouts_overall = pd_concat([payouts_overall, payout_info], ignore_index=True)
time.sleep(.5 + random.random())
DK_URL = "https://api.draftkings.com/draftgroups/v1/draftgroups/"+str(draftGroup)+"/draftables"
# Make a request to the API
response = requests.get(DK_URL, impersonate="chrome")
draftables_data = response.json()
# Extracting the required fields
draftables = draftables_data['draftables']
try:
draftables_data = [
{
'Date': current_date,
'Contest Date': contest_date,
'Contest ID': contest_id,
'draftableId': draftable.get('draftableId'),
'First Name': draftable.get('firstName'),
'Last Name': draftable.get('lastName'),
'Display Name': draftable.get('displayName'),
'Position': draftable.get('position'),
'Team': draftable.get('teamAbbreviation'),
'Opp': np_where(draftable.get('teamAbbreviation') == draftable['competition']['nameDisplay'][0]['value'], draftable['competition']['nameDisplay'][2]['value'], draftable['competition']['nameDisplay'][0]['value']).item(),
'Salary': draftable.get('salary'),
'Avg FPTS': draftable.get('draftStatAttributes', [{}])[0].get('value', 0),
'Sport': sport,
}
for draftable in draftables
]
except:
draftables_data = [
{
'Date': current_date,
'Contest Date': contest_date,
'Contest ID': contest_id,
'draftableId': draftable.get('draftableId'),
'First Name': draftable.get('firstName'),
'Last Name': draftable.get('lastName'),
'Display Name': draftable.get('displayName'),
'Position': draftable.get('position'),
'Team': draftable.get('teamAbbreviation'),
'Opp': np_nan,
'Salary': draftable.get('salary'),
'Avg FPTS': draftable.get('draftStatAttributes', [{}])[0].get('value', 0),
'Sport': sport,
}
for draftable in draftables
]
player_info = DataFrame(draftables_data)
player_overall = pd_concat([player_overall, player_info], ignore_index=True)
except Exception as e:
print(f"Error: {e}")
pass
payout_info_frame = DataFrame(payouts_overall, columns=['Date', 'Contest Date', 'Contest ID', 'Entry Fee', 'minPosition', 'maxPosition', 'value'])
contest_info_frame = DataFrame(contest_overall, columns=['Date', 'Contest Date', 'Contest Name', 'Contest Type', 'Contest ID', 'URL', 'Sport'])
print(contest_info_frame)
try:
contest_export = pd_concat([contest_import, contest_info_frame], ignore_index=True)
except:
contest_export = contest_info_frame
if len(contest_export) > 0:
contest_export = contest_export.drop_duplicates(subset=['Contest Date', 'Contest ID'])
contest_export = contest_export.reset_index(drop=True)
chunk_size = 10000
contest_collection.drop()
for i in range(0, len(contest_export), chunk_size):
for _ in range(5):
try:
df_chunk = contest_export.iloc[i:i + chunk_size]
contest_collection.insert_many(df_chunk.to_dict('records'), ordered=False)
break
except Exception as e:
print(f"Retry due to error: {e}")
time.sleep(1)
print(player_overall)
try:
player_export = pd_concat([player_import, player_overall], ignore_index=True)
except:
player_export = player_overall
if len(player_export) > 0:
player_export = player_export.drop_duplicates(subset=['Contest Date', 'draftableId', 'Display Name'])
player_export = player_export.reset_index(drop=True)
chunk_size = 10000
player_collection.drop()
for i in range(0, len(player_export), chunk_size):
for _ in range(5):
try:
df_chunk = player_export.iloc[i:i + chunk_size]
player_collection.insert_many(df_chunk.to_dict('records'), ordered=False)
break
except Exception as e:
print(f"Retry due to error: {e}")
time.sleep(1)
print(payout_info_frame)
try:
payout_export = pd_concat([payout_import, payout_info_frame], ignore_index=True)
except:
payout_export = payout_info_frame
if len(payout_export) > 0:
payout_export = payout_export.drop_duplicates(subset=['Contest Date', 'Contest ID', 'Entry Fee', 'minPosition', 'maxPosition'])
payout_export = payout_export.reset_index(drop=True)
chunk_size = 10000
payout_collection.drop()
for i in range(0, len(payout_export), chunk_size):
for _ in range(5):
try:
df_chunk = payout_export.iloc[i:i + chunk_size]
payout_collection.insert_many(df_chunk.to_dict('records'), ordered=False)
break
except Exception as e:
print(f"Retry due to error: {e}")
time.sleep(1)
grab_contest_ids() |