Skip to content

Commit 5cd787a

Browse files
committed
chore: remove time dependency
1 parent 2616608 commit 5cd787a

3 files changed

Lines changed: 30 additions & 26 deletions

File tree

examples/direct/ball/raw_data_example.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import json
1111
import os
1212
import sys
13-
from datetime import datetime, timedelta, timezone
13+
from datetime import datetime, timezone
1414

1515
import httpx
1616

@@ -23,8 +23,6 @@
2323
# -----------------------------------------------------------------------------
2424
CLIENT_ID = os.environ.get("CLIENT_ID")
2525
CLUB_ID = os.environ.get("CLUB_ID")
26-
SESSION_DAYS = 30 # sessions from last N days
27-
2826
# -----------------------------------------------------------------------------
2927
# GraphQL
3028
# -----------------------------------------------------------------------------
@@ -120,6 +118,9 @@ async def download_recording(
120118
"""Download one recording's raw JSON to out_dir. Returns True if saved, False if skipped."""
121119
url = recording.get("url")
122120
if not url:
121+
ball = recording.get("ball") or {}
122+
serial = ball.get("serialNumber", "?")
123+
print(f" Skip {recording['id']} (Ball {serial}): no URL")
123124
return False
124125
if url.startswith("/"):
125126
url = f"{API_BASE_URL.rstrip('/')}{url}"
@@ -135,7 +136,8 @@ async def download_recording(
135136
print(f" Skip {recording['id']} (Ball {serial}): {e.response.status_code}")
136137
return False
137138
except httpx.RequestError as e:
138-
print(f" Skip {recording['id']} (Ball {serial}): {e}")
139+
reason = str(e).strip() or type(e).__name__
140+
print(f" Skip {recording['id']} (Ball {serial}): {reason}")
139141
return False
140142

141143
if _record_count(raw) == 0:
@@ -160,9 +162,10 @@ async def main() -> None:
160162
)
161163

162164
now = datetime.now(timezone.utc)
165+
# No time filter: get all sessions (use a wide range for the required query params)
163166
list_vars = {
164167
"clubIdEq": CLUB_ID,
165-
"startTimeGteq": (now - timedelta(days=SESSION_DAYS)).isoformat(),
168+
"startTimeGteq": datetime(2000, 1, 1, tzinfo=timezone.utc).isoformat(),
166169
"endTimeLteq": now.isoformat(),
167170
}
168171

@@ -172,7 +175,7 @@ async def main() -> None:
172175
if not sessions:
173176
print("No sessions found.")
174177
return
175-
print(f"Found {len(sessions)} session(s) in last {SESSION_DAYS} days.")
178+
print(f"Found {len(sessions)} session(s).")
176179

177180
chosen = _choose_session(sessions)
178181
if not chosen:
@@ -196,7 +199,7 @@ async def main() -> None:
196199
print(f"Downloading {len(recordings_with_url)} recording(s) to {out_dir}/")
197200

198201
headers = {"Authorization": f"Bearer {auth._get_authentication_token()}"}
199-
async with httpx.AsyncClient(headers=headers) as http_client:
202+
async with httpx.AsyncClient(headers=headers, timeout=60.0) as http_client:
200203
ok = sum(
201204
await asyncio.gather(
202205
*[

examples/pydantic/ball/raw_data_example.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,20 @@
1010
import json
1111
import os
1212
import sys
13-
from datetime import datetime, timedelta, timezone
14-
1513
import httpx
1614

1715
from playerdatapy.constants import API_BASE_URL
1816
from playerdatapy.gqlauth import AuthenticationType
1917
from playerdatapy.playerdata_api import PlayerDataAPI
2018

21-
from queries.club_sessions_filtered_by_time_range import (
22-
club_sessions_filtered_by_time_range,
23-
)
24-
from queries.session_ball_data import session_ball_data
19+
from examples.pydantic.queries.club_sessions import club_sessions
20+
from examples.pydantic.queries.session_ball_data import session_ball_data
2521

2622
# -----------------------------------------------------------------------------
2723
# Config (env or override below)
2824
# -----------------------------------------------------------------------------
2925
CLIENT_ID = os.environ.get("CLIENT_ID")
3026
CLUB_ID = os.environ.get("CLUB_ID")
31-
SESSION_DAYS = 30 # sessions from last N days
3227

3328

3429
def _record_count(data: list | dict) -> int:
@@ -84,6 +79,9 @@ async def download_recording(
8479
"""Download one recording's raw JSON to out_dir. Returns True if saved, False if skipped."""
8580
url = recording.get("url")
8681
if not url:
82+
ball = recording.get("ball") or {}
83+
serial = ball.get("serialNumber", "?")
84+
print(f" Skip {recording['id']} (Ball {serial}): no URL")
8785
return False
8886
if url.startswith("/"):
8987
url = f"{API_BASE_URL.rstrip('/')}{url}"
@@ -99,7 +97,8 @@ async def download_recording(
9997
print(f" Skip {recording['id']} (Ball {serial}): {e.response.status_code}")
10098
return False
10199
except httpx.RequestError as e:
102-
print(f" Skip {recording['id']} (Ball {serial}): {e}")
100+
reason = str(e).strip() or type(e).__name__
101+
print(f" Skip {recording['id']} (Ball {serial}): {reason}")
103102
return False
104103

105104
if _record_count(raw) == 0:
@@ -120,23 +119,16 @@ async def main() -> None:
120119
authentication_type=AuthenticationType.AUTHORISATION_CODE_FLOW_PCKE,
121120
)
122121

123-
now = datetime.now(timezone.utc)
124-
start = now - timedelta(days=SESSION_DAYS)
125-
126122
sessions_response = await api.run_queries(
127-
"ClubSessionsFilteredByTimeRangeQuery",
128-
club_sessions_filtered_by_time_range(
129-
club_id=CLUB_ID,
130-
start_time_gteq=start,
131-
end_time_lteq=now,
132-
),
123+
"ClubSessionsQuery",
124+
club_sessions(club_id=CLUB_ID),
133125
)
134126
sessions = sessions_response.get("sessions") or []
135127

136128
if not sessions:
137129
print("No sessions found.")
138130
return
139-
print(f"Found {len(sessions)} session(s) in last {SESSION_DAYS} days.")
131+
print(f"Found {len(sessions)} session(s).")
140132

141133
chosen = _choose_session(sessions)
142134
if not chosen:
@@ -165,7 +157,7 @@ async def main() -> None:
165157
print(f"Downloading {len(recordings_with_url)} recording(s) to {out_dir}/")
166158

167159
headers = {"Authorization": f"Bearer {api._get_authentication_token()}"}
168-
async with httpx.AsyncClient(headers=headers) as http_client:
160+
async with httpx.AsyncClient(headers=headers, timeout=60.0) as http_client:
169161
ok = sum(
170162
await asyncio.gather(
171163
*[
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from playerdatapy.custom_queries import Query
2+
from playerdatapy.input_types import SessionsSessionFilter
3+
from playerdatapy.custom_fields import SessionInterface
4+
5+
6+
def club_sessions(club_id: str) -> SessionInterface:
7+
return Query.sessions(filter=SessionsSessionFilter(clubIdEq=club_id)).fields(
8+
SessionInterface.id, SessionInterface.start_time, SessionInterface.end_time
9+
)

0 commit comments

Comments
 (0)