Spaces:
Build error
Build error
James McCool commited on
Commit ·
555ed46
1
Parent(s): 27154c4
Okay lets try this again
Browse files
global_func/contest_pricing_api.py
CHANGED
|
@@ -1,7 +1,12 @@
|
|
| 1 |
from datetime import datetime
|
| 2 |
|
| 3 |
import pandas as pd
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
SPORT_API_MAP = {
|
|
@@ -26,14 +31,18 @@ def _clean_contest_name(name: str) -> str:
|
|
| 26 |
return name.strip()
|
| 27 |
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def fetch_contests_for_selection(sport_var: str, type_var: str) -> list[dict]:
|
| 30 |
sport_code = SPORT_API_MAP.get(sport_var, sport_var)
|
| 31 |
-
|
| 32 |
-
f"https://www.draftkings.com/lobby/getcontests?sport={sport_code}",
|
| 33 |
-
impersonate="chrome",
|
| 34 |
-
timeout=20,
|
| 35 |
-
)
|
| 36 |
-
data = response.json()
|
| 37 |
contests = data.get("Contests", [])
|
| 38 |
|
| 39 |
filtered = []
|
|
@@ -68,23 +77,17 @@ def fetch_contests_for_selection(sport_var: str, type_var: str) -> list[dict]:
|
|
| 68 |
|
| 69 |
|
| 70 |
def fetch_pricing_for_contest(contest_id: int | str) -> pd.DataFrame:
|
| 71 |
-
|
| 72 |
-
f"https://api.draftkings.com/contests/v1/contests/{contest_id}?format=json"
|
| 73 |
-
impersonate="chrome",
|
| 74 |
-
timeout=20,
|
| 75 |
)
|
| 76 |
-
contest_data = contest_resp.json()
|
| 77 |
contest_detail = contest_data.get("contestDetail", {})
|
| 78 |
draft_group_id = contest_detail.get("draftGroupId")
|
| 79 |
if draft_group_id is None:
|
| 80 |
raise ValueError("No draftGroupId returned for selected contest.")
|
| 81 |
|
| 82 |
-
|
| 83 |
-
f"https://api.draftkings.com/draftgroups/v1/draftgroups/{draft_group_id}/draftables"
|
| 84 |
-
|
| 85 |
-
timeout=20,
|
| 86 |
-
)
|
| 87 |
-
draftables_data = draftables_resp.json().get("draftables", [])
|
| 88 |
|
| 89 |
rows = []
|
| 90 |
for draftable in draftables_data:
|
|
|
|
| 1 |
from datetime import datetime
|
| 2 |
|
| 3 |
import pandas as pd
|
| 4 |
+
try:
|
| 5 |
+
from curl_cffi import requests as http_requests
|
| 6 |
+
USE_IMPERSONATE = True
|
| 7 |
+
except Exception:
|
| 8 |
+
import requests as http_requests
|
| 9 |
+
USE_IMPERSONATE = False
|
| 10 |
|
| 11 |
|
| 12 |
SPORT_API_MAP = {
|
|
|
|
| 31 |
return name.strip()
|
| 32 |
|
| 33 |
|
| 34 |
+
def _http_get_json(url: str, timeout: int = 20) -> dict:
|
| 35 |
+
if USE_IMPERSONATE:
|
| 36 |
+
response = http_requests.get(url, impersonate="chrome", timeout=timeout)
|
| 37 |
+
else:
|
| 38 |
+
response = http_requests.get(url, timeout=timeout)
|
| 39 |
+
response.raise_for_status()
|
| 40 |
+
return response.json()
|
| 41 |
+
|
| 42 |
+
|
| 43 |
def fetch_contests_for_selection(sport_var: str, type_var: str) -> list[dict]:
|
| 44 |
sport_code = SPORT_API_MAP.get(sport_var, sport_var)
|
| 45 |
+
data = _http_get_json(f"https://www.draftkings.com/lobby/getcontests?sport={sport_code}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
contests = data.get("Contests", [])
|
| 47 |
|
| 48 |
filtered = []
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
def fetch_pricing_for_contest(contest_id: int | str) -> pd.DataFrame:
|
| 80 |
+
contest_data = _http_get_json(
|
| 81 |
+
f"https://api.draftkings.com/contests/v1/contests/{contest_id}?format=json"
|
|
|
|
|
|
|
| 82 |
)
|
|
|
|
| 83 |
contest_detail = contest_data.get("contestDetail", {})
|
| 84 |
draft_group_id = contest_detail.get("draftGroupId")
|
| 85 |
if draft_group_id is None:
|
| 86 |
raise ValueError("No draftGroupId returned for selected contest.")
|
| 87 |
|
| 88 |
+
draftables_data = _http_get_json(
|
| 89 |
+
f"https://api.draftkings.com/draftgroups/v1/draftgroups/{draft_group_id}/draftables"
|
| 90 |
+
).get("draftables", [])
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
rows = []
|
| 93 |
for draftable in draftables_data:
|