-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
1598 lines (1358 loc) · 82.5 KB
/
scraper.py
File metadata and controls
1598 lines (1358 loc) · 82.5 KB
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import json
import logging
import os
import re
import time
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Dict, List, Optional, Callable
import pandas as pd
from playwright.async_api import async_playwright, Page, TimeoutError as PlaywrightTimeoutError
import pytz
import requests
from supabase import create_client, Client
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
logging.basicConfig(
level=logging.DEBUG, # Changed to DEBUG for detailed logging
format="%(asctime)s [%(levelname)s] %(message)s",
)
DEFAULT_URL = "https://recreation.ubc.ca/tennis/court-booking/"
DATA_PATH = Path(__file__).parent / "court_data.json"
async def _click_and_wait(page: Page, locator_str: str, wait_for: Optional[str] = None, timeout: int = 10_000) -> None:
"""Click an element and optionally wait for a selector."""
await page.click(locator_str, timeout=timeout)
if wait_for:
await page.wait_for_selector(wait_for, timeout=timeout)
async def _ensure_page_size_20(page: Page) -> bool:
"""Ensure the page size is set to 20 results. Returns True if successful."""
try:
# Check if page size is already 20 by looking at the select element
select_elements = await page.query_selector_all("select")
for select_el in select_elements:
try:
current_value = await select_el.evaluate("el => el.value")
if current_value == "20":
logging.debug("Page size is already set to 20")
return True
except Exception:
continue
# Page size is not 20, change it
logging.info("Setting page size to 20 results...")
select_elements = await page.query_selector_all("select, [role='combobox']")
for idx, select_el in enumerate(select_elements):
try:
options = await select_el.query_selector_all("option")
for opt in options:
opt_text = (await opt.inner_text()).strip()
opt_value = await opt.get_attribute("value") or ""
if opt_text == "20" or opt_value == "20":
tag_name = await select_el.evaluate("el => el.tagName.toLowerCase()")
if tag_name == "select":
try:
await select_el.evaluate("el => { el.value = '20'; el.dispatchEvent(new Event('change', { bubbles: true })); }")
await page.wait_for_timeout(2_000)
logging.info("✅ Set page size to 20 results")
return True
except Exception:
continue
except Exception:
continue
logging.warning("Could not set page size to 20")
return False
except Exception as e:
logging.warning("Error setting page size to 20: %s", e)
return False
async def _wait_for_court_list_loaded(page: Page, expected_court_count: int = 10, max_wait: int = 30_000, check_interval: int = 1_000) -> bool:
"""Wait briefly for the court list page to load, then proceed.
Just waits 2-3 seconds and does a quick check that we have some courts.
Returns True to proceed regardless.
"""
logging.info("Waiting briefly for court list to load (2-3 seconds)...")
# Wait 2-3 seconds for page to settle
await page.wait_for_timeout(2_500)
# Quick check that we're on the right page and have some courts
try:
current_url = page.url
if "perfectmind" in current_url.lower() and "facility" in current_url.lower():
# We're on a facility/schedule page, not the list
logging.debug("Still on facility page, but proceeding anyway...")
else:
# Check if we have at least some courts
court_elements = await page.query_selector_all("text=/Court\\s+\\d+/i")
court_count = len(court_elements)
if court_count > 0:
logging.info("✅ Found %d courts on page, proceeding...", court_count)
else:
logging.debug("No courts found yet, but proceeding anyway...")
except Exception as exc:
logging.debug("Error checking court list: %s, but proceeding anyway...", exc)
# Always return True - we'll proceed and handle errors as we go
return True
async def _verify_court_on_page(page: Page, court_name: str) -> bool:
"""Verify that the specified court name appears on the current page.
Checks multiple locations: page title, headings, body text, etc.
"""
try:
# Normalize court name for matching (handle "Court 01" vs "Court 1" etc)
court_num_match = re.search(r'Court\s+0?(\d+)', court_name, re.IGNORECASE)
if court_num_match:
court_num = int(court_num_match.group(1))
# Try multiple formats
court_patterns = [
f"Court\\s+{court_num:02d}", # Court 01
f"Court\\s+{court_num}", # Court 1
f"Court\\s+0{court_num}", # Court 01 (alternative)
]
else:
court_patterns = [re.escape(court_name)]
# Check page title
try:
title = await page.title()
for pattern in court_patterns:
if re.search(pattern, title, re.IGNORECASE):
logging.debug("Found court name in page title: %s", title)
return True
except Exception:
pass
# Check all headings (h1, h2, h3, etc.)
try:
headings = await page.query_selector_all("h1, h2, h3, h4, h5, h6")
for heading in headings:
try:
heading_text = await heading.inner_text()
for pattern in court_patterns:
if re.search(pattern, heading_text, re.IGNORECASE):
logging.debug("Found court name in heading: %s", heading_text)
return True
except Exception:
continue
except Exception:
pass
# Check page body text (look for court name near "Court" or facility-related text)
try:
body_text = await page.inner_text("body")
for pattern in court_patterns:
# Look for the pattern, ideally near words like "Court", "Facility", "Schedule"
if re.search(pattern, body_text, re.IGNORECASE):
# Additional check: make sure it's not just in a list of all courts
# If we see many court numbers, we might still be on the list page
all_courts = re.findall(r'Court\s+\d+', body_text, re.IGNORECASE)
if len(all_courts) <= 2: # Should only see 1-2 mentions of court names
logging.debug("Found court name in page body")
return True
except Exception:
pass
# Check URL parameters (sometimes court info is in the URL)
try:
current_url = page.url
for pattern in court_patterns:
if re.search(pattern, current_url, re.IGNORECASE):
logging.debug("Found court name in URL: %s", current_url[:100])
return True
except Exception:
pass
return False
except Exception as exc:
logging.debug("Error verifying court on page: %s", exc)
return False
async def _wait_for_schedule_page(page: Page, court_name: str, max_wait: int = 30_000, check_interval: int = 1_000) -> bool:
"""Wait for the schedule page to load and VERIFY we're on the correct court's page.
This function is very patient and verifies:
1. We're on a schedule/booking page (not the court list)
2. The page actually shows the correct court name
3. The page has loaded calendar/schedule elements
Returns True only if ALL verifications pass, False otherwise.
"""
start_time = time.time() * 1000 # Convert to milliseconds
elapsed = 0
last_log_time = 0
logging.info("Waiting for schedule page for %s (max wait: %d seconds)...", court_name, max_wait // 1000)
while elapsed < max_wait:
try:
# Step 1: Check that we're NOT on the court list page
court_list_indicators = await page.query_selector_all("text=/Court\\s+\\d+/i")
if len(court_list_indicators) > 5:
# Still on the list page, wait more
if elapsed - last_log_time > 10_000: # Log every 10 seconds
logging.debug("Still on court list page, waiting... (%d courts found)", len(court_list_indicators))
last_log_time = elapsed
await page.wait_for_timeout(check_interval)
elapsed = (time.time() * 1000) - start_time
continue
# Step 2: Check if we're on a schedule/booking page
schedule_indicators = [
"table", # Calendar table
".calendar",
"[class*='calendar']",
"[class*='schedule']",
"[class*='time-slot']",
"td[onclick]",
"div[onclick]",
"[id*='calendar']",
"[id*='schedule']",
]
found_schedule = False
for indicator in schedule_indicators:
try:
elements = await page.query_selector_all(indicator)
if elements and len(elements) > 0:
found_schedule = True
break
except Exception:
continue
if not found_schedule:
# Not on schedule page yet
if elapsed - last_log_time > 10_000:
logging.debug("Schedule elements not found yet, waiting...")
last_log_time = elapsed
await page.wait_for_timeout(check_interval)
elapsed = (time.time() * 1000) - start_time
continue
# Step 3: CRITICAL - Verify we're on the CORRECT court's page
court_verified = await _verify_court_on_page(page, court_name)
if not court_verified:
# We're on a schedule page, but not the right court!
logging.warning("On a schedule page, but court verification failed for %s. Checking again...", court_name)
if elapsed - last_log_time > 10_000:
# Try to get more info about what court we're actually on
try:
page_text = await page.inner_text("body")
found_courts = re.findall(r'Court\s+\d+', page_text, re.IGNORECASE)
if found_courts:
logging.warning("Found these courts on page: %s (expected: %s)", found_courts[:5], court_name)
except Exception:
pass
last_log_time = elapsed
await page.wait_for_timeout(check_interval)
elapsed = (time.time() * 1000) - start_time
continue
# Step 4: Additional confirmation - check URL
current_url = page.url
url_ok = "perfectmind" in current_url.lower() or "book" in current_url.lower() or "schedule" in current_url.lower() or "facility" in current_url.lower()
if court_verified and found_schedule and url_ok:
logging.info("✅ VERIFIED on correct schedule page for %s (URL: %s)", court_name, current_url[:100])
return True
# If we have schedule but court not verified, keep waiting
if elapsed - last_log_time > 10_000:
logging.debug("Schedule found but verification incomplete, continuing to wait...")
last_log_time = elapsed
await page.wait_for_timeout(check_interval)
elapsed = (time.time() * 1000) - start_time
except Exception as exc:
logging.debug("Error checking schedule page: %s", exc)
await page.wait_for_timeout(check_interval)
elapsed = (time.time() * 1000) - start_time
logging.warning("❌ Timeout waiting for schedule page for %s (waited %d seconds)", court_name, max_wait // 1000)
return False
async def _scrape_court_schedule(page: Page, court_name: str) -> List[Dict]:
"""Scrape available slots from a court's weekly schedule view.
IMPORTANT: This function assumes we're already on the correct court's schedule page.
It will verify this before scraping.
"""
entries: List[Dict] = []
try:
# CRITICAL: Verify we're on the correct court's page before scraping
logging.info("Verifying we're on the correct page for %s before scraping...", court_name)
court_verified = await _verify_court_on_page(page, court_name)
if not court_verified:
logging.error("❌ VERIFICATION FAILED: Not on %s's page! Attempting to identify which court we're actually on...", court_name)
# Try to identify which court we're actually on
try:
page_text = await page.inner_text("body")
found_courts = re.findall(r'Court\s+\d+', page_text, re.IGNORECASE)
if found_courts:
unique_courts = list(set(found_courts))
logging.error("Found these courts on the page: %s (expected: %s)", unique_courts, court_name)
else:
logging.error("No court names found on page")
except Exception as exc:
logging.error("Could not identify court on page: %s", exc)
return entries # Return empty - don't scrape wrong court data
logging.info("✅ Verified on %s's page, proceeding with scraping...", court_name)
# Wait for the schedule table to load - use #scheduler as the main container
logging.info("Waiting for schedule table (#scheduler) to load...")
try:
await page.wait_for_selector("#scheduler", timeout=30_000)
await page.wait_for_timeout(1_000) # Give it a moment to fully render
except Exception as exc:
logging.error("No #scheduler container found on page: %s", exc)
return entries
# Use #scheduler as the main container (not just the table)
scheduler_container = page.locator("#scheduler")
# Get current time in Vancouver (Pacific timezone)
vancouver_tz = pytz.timezone('America/Vancouver')
now_vancouver = datetime.now(vancouver_tz)
today = now_vancouver.date()
# Calculate 72 hours from now
max_datetime = now_vancouver + timedelta(hours=72)
logging.debug("Current time in Vancouver: %s, Max time (72h): %s", now_vancouver, max_datetime)
# Generate time slots from 8:00 AM to 10:00 PM (hourly)
time_slots = []
for hour in range(8, 23): # 8 AM to 10 PM (22:00 = 10 PM)
# Convert to 12-hour format
start_period = "AM" if hour < 12 else "PM"
start_hour_12 = hour if hour <= 12 else hour - 12
if start_hour_12 == 0:
start_hour_12 = 12
time_slot_str = f"{start_hour_12}:00 {start_period}"
time_slots.append(time_slot_str)
logging.info("Checking slots for time ranges: %s", time_slots)
# Locate ALL rows in the scheduler container
rows = scheduler_container.locator("tbody tr")
row_count = await rows.count()
logging.info("Found %d rows in scheduler container", row_count)
# Debug: Check for gridcells with "Bookable" text
bookable_gridcells = scheduler_container.locator("[role='gridcell']:has-text('Bookable')")
bookable_count = await bookable_gridcells.count()
logging.info("Found %d gridcells with 'Bookable' text", bookable_count)
if bookable_count > 0:
logging.info("Debugging first 5 bookable gridcells:")
for i in range(min(5, bookable_count)):
try:
gc = bookable_gridcells.nth(i)
gc_text = await gc.inner_text()
gc_title = await gc.get_attribute("title") or ""
# Get the parent row to see structure
parent_row = await gc.evaluate_handle("el => el.closest('tr')")
row_text = ""
if parent_row:
try:
row_elem = parent_row.as_element() if hasattr(parent_row, 'as_element') else parent_row
if row_elem:
row_text = await row_elem.inner_text()
except Exception:
pass
logging.info(" Bookable gridcell %d: title='%s', text='%s', row text: %s",
i, gc_title[:80], gc_text[:100], row_text[:150] if row_text else "empty")
except Exception as exc:
logging.debug(" Bookable gridcell %d: error - %s", i, exc)
# Also check all gridcells to see what titles they have
all_gridcells = scheduler_container.locator("[role='gridcell']")
gc_count = await all_gridcells.count()
logging.info("Checking titles of first 20 gridcells:")
titles_found = set()
for i in range(min(20, gc_count)):
try:
gc = all_gridcells.nth(i)
gc_title = await gc.get_attribute("title") or ""
if gc_title:
titles_found.add(gc_title)
except Exception:
pass
logging.info(" Unique titles found: %s", list(titles_found)[:10])
# Also check what's in row 3 (the one with 92 cells)
if row_count > 3:
logging.info("Debugging row 3 (the one with many cells):")
row3 = rows.nth(3)
row3_cells = row3.locator("td, th")
row3_cell_count = await row3_cells.count()
logging.info(" Row 3 has %d cells", row3_cell_count)
# Check first 10 cells
for j in range(min(10, row3_cell_count)):
try:
cell = row3_cells.nth(j)
cell_text = await cell.inner_text()
cell_html = await cell.evaluate("el => el.outerHTML.substring(0, 200)")
logging.info(" Cell %d: text='%s', HTML: %s", j, cell_text[:80], cell_html)
except Exception as exc:
logging.debug(" Cell %d: error - %s", j, exc)
# Debug: Print first few rows to see structure
if row_count > 0:
logging.info("Debugging first 10 rows:")
for i in range(min(10, row_count)):
try:
row = rows.nth(i)
row_text = await row.inner_text()
cells = row.locator("td, th")
cell_count = await cells.count()
cell_texts = []
for j in range(min(5, cell_count)): # First 5 cells
try:
cell_text = await cells.nth(j).inner_text()
cell_texts.append(f"cell[{j}]='{cell_text[:50]}'")
except Exception:
cell_texts.append(f"cell[{j}]=error")
logging.info(" Row %d: %d cells, cells: %s", i, cell_count, ", ".join(cell_texts))
except Exception as exc:
logging.debug(" Row %d: error - %s", i, exc)
# Find all gridcells that contain a span with a title attribute
# The span has title="03:00 PM-04:00 PM" and text "Bookable 24hrs in advance"
all_gridcells = scheduler_container.locator("[role='gridcell']")
gc_count = await all_gridcells.count()
logging.info("Found %d gridcells in #scheduler", gc_count)
for i in range(gc_count):
try:
gc = all_gridcells.nth(i)
# Look for span elements with title attribute inside this gridcell
title_spans_locator = gc.locator("span[title]")
span_count = await title_spans_locator.count()
# Debug: Log first few gridcells
if i < 3 and span_count > 0:
logging.debug("Gridcell %d: found %d spans with title", i, span_count)
for span_idx in range(span_count):
try:
span = title_spans_locator.nth(span_idx)
span_title = await span.get_attribute("title") or ""
span_text = (await span.inner_text()).strip().lower()
# Check if this span has a time title and bookable text
if not span_title or ("AM" not in span_title and "PM" not in span_title):
continue
# Check if it's bookable
is_bookable = False
status = "Open"
if "bookable 24hrs in advance" in span_text or "bookable 24 hours in advance" in span_text:
is_bookable = True
status = "Bookable in 24h"
elif "book now" in span_text or ("book" in span_text and len(span_text) < 50):
is_bookable = True
status = "Open"
if not is_bookable:
continue
# Extract time from title (e.g., "03:00 PM-04:00 PM" -> "3:00 PM")
time_match = re.search(r'(\d{1,2}):00\s*(AM|PM)', span_title)
if not time_match:
continue
time_slot_str = f"{time_match.group(1)}:00 {time_match.group(2)}"
logging.debug("Found bookable slot: time=%s, title='%s'", time_slot_str, span_title)
# Determine day offset from the gridcell's left position
day_offset = 0
try:
left_pos = await gc.evaluate("el => parseFloat(window.getComputedStyle(el).left)")
# First column (today) is at ~2px, each day is ~208px
if left_pos <= 10:
day_offset = 0 # Today
else:
day_offset = int(round((left_pos - 2) / 208))
logging.debug(" Calculated day offset: %d (left position: %f px)", day_offset, left_pos)
except Exception as exc:
logging.warning(" Could not get left position: %s, defaulting to today (offset 0)", exc)
day_offset = 0
slot_date = today + timedelta(days=day_offset)
date_str = slot_date.strftime("%Y-%m-%d")
# Construct full datetime string
datetime_str = f"{date_str} {time_slot_str}"
# Parse the datetime and check if it's within 72 hours
try:
# Parse time (e.g., "3:00 PM" -> hour=15, minute=0)
time_parts = time_slot_str.split()
time_hour_min = time_parts[0].split(':')
hour = int(time_hour_min[0])
minute = int(time_hour_min[1]) if len(time_hour_min) > 1 else 0
period = time_parts[1] if len(time_parts) > 1 else "AM"
# Convert to 24-hour format
if period == "PM" and hour != 12:
hour += 12
elif period == "AM" and hour == 12:
hour = 0
# Create datetime in Vancouver timezone
slot_datetime = vancouver_tz.localize(
datetime(slot_date.year, slot_date.month, slot_date.day, hour, minute)
)
# Filter: only include slots within 72 hours
if slot_datetime > max_datetime:
logging.debug(" Skipping slot %s (beyond 72 hours)", datetime_str)
continue
# Also skip slots in the past
if slot_datetime < now_vancouver:
logging.debug(" Skipping slot %s (in the past)", datetime_str)
continue
except Exception as exc:
logging.warning(" Error parsing datetime for %s: %s, including anyway", datetime_str, exc)
# Get link
href = page.url
try:
link_el = await gc.query_selector("a[href]")
if link_el:
href = await link_el.get_attribute("href")
else:
onclick = await gc.get_attribute("onclick")
if onclick:
url_match = re.search(r'["\']([^"\']*perfectmind[^"\']*)["\']', onclick)
if url_match:
href = url_match.group(1)
except Exception:
pass
entries.append({
"court": court_name,
"time": datetime_str,
"status": status,
"link": href,
"raw_text": await span.inner_text(),
})
logging.debug("Found slot: %s on %s (day offset %d) - %s",
time_slot_str, date_str, day_offset, status)
except Exception as exc:
logging.debug("Error processing span %d in gridcell %d: %s", span_idx, i, exc)
continue
except Exception as exc:
logging.debug("Error processing gridcell %d: %s", i, exc)
continue
logging.info("Found %d slots for %s", len(entries), court_name)
except Exception as exc:
logging.exception("Error scraping schedule for %s: %s", court_name, exc)
return entries
async def scrape_courts(
*,
headless: bool = True,
base_url: str = DEFAULT_URL,
court_names: Optional[List[str]] = None,
days: int = 7,
progress_callback: Optional[Callable] = None,
) -> List[Dict]:
"""Scrape available slots for each court."""
results: List[Dict] = []
async with async_playwright() as p:
browser = await p.chromium.launch(headless=headless)
page = await browser.new_page()
try:
# Step 1: Navigate to the booking page
logging.info("Navigating to %s", base_url)
await page.goto(base_url, wait_until="domcontentloaded", timeout=30_000)
await page.wait_for_timeout(2000)
# Step 2: Click "Book a Court" button
logging.info("Clicking 'Book a Court' button")
try:
# Try multiple possible selectors for the button
book_button_selectors = [
"text='Book a Court'",
"a:has-text('Book a Court')",
"button:has-text('Book a Court')",
"[href*='book']:has-text('Book')",
]
clicked = False
for selector in book_button_selectors:
try:
await page.click(selector, timeout=10_000)
clicked = True
logging.info("Clicked 'Book a Court' using selector: %s", selector)
break
except Exception:
continue
if not clicked:
raise RuntimeError("Could not find 'Book a Court' button")
# Wait for the court list to fully load with all courts and Choose buttons
# Use flexible count - we'll discover the actual count (10 courts visible, Court 9 not available)
court_list_loaded = await _wait_for_court_list_loaded(page, expected_court_count=10, max_wait=30_000)
if not court_list_loaded:
logging.error("Failed to load court list properly, but continuing...")
# Still try to proceed, but log the issue
await page.wait_for_timeout(2_000)
except Exception as exc:
logging.error("Failed to click 'Book a Court': %s", exc)
raise
# Step 3: Change page size to show 20 results instead of paginating
if not court_names:
logging.info("Changing page size to show 20 results per page...")
try:
# Wait for the page to be fully loaded first
await page.wait_for_timeout(2_000)
try:
await page.wait_for_load_state("networkidle", timeout=5_000)
except Exception:
pass
# First, find the select/dropdown element that contains the page size options
# Look for select elements or comboboxes - try multiple times
select_elements = []
for attempt in range(3):
select_elements = await page.query_selector_all("select, [role='combobox'], [class*='select'], [class*='dropdown'], [aria-label*='per page'], [aria-label*='results']")
if len(select_elements) > 0:
break
await page.wait_for_timeout(1_000)
logging.info("Found %d potential select/dropdown elements", len(select_elements))
page_size_changed = False
# Try to find and click the "20" option
# Method 1: Direct role-based selector
try:
option_20 = page.get_by_role("option", name="20")
count = await option_20.count()
if count > 0:
# First, we might need to open the dropdown
# Look for the select element or button that opens it
select_parent = await option_20.first.evaluate_handle("el => el.closest('select, [role=\"combobox\"]')")
if select_parent:
# Click the parent to open dropdown
await select_parent.as_element().click()
await page.wait_for_timeout(500)
await option_20.first.click()
logging.info("✅ Changed page size to 20 results (method 1)")
page_size_changed = True
except Exception as e1:
logging.debug("Method 1 failed: %s", e1)
# Method 2: Find select element and look for option with text "20"
if not page_size_changed:
try:
for idx, select_el in enumerate(select_elements):
try:
# Check if this select has a "20" option
options = await select_el.query_selector_all("option")
logging.info("Select element %d has %d options", idx, len(options))
for opt_idx, opt in enumerate(options):
opt_text = (await opt.inner_text()).strip()
opt_value = await opt.get_attribute("value") or ""
logging.info(" Option %d: text='%s', value='%s'", opt_idx, opt_text, opt_value)
if opt_text == "20" or opt_value == "20":
logging.info("Found '20' option in select element %d", idx)
# Check if it's a real <select> element
tag_name = await select_el.evaluate("el => el.tagName.toLowerCase()")
logging.info("Select element %d tag: %s", idx, tag_name)
if tag_name == "select":
# Use locator's select_option method which is more reliable
try:
select_locator = page.locator(f"select").nth(idx)
await select_locator.select_option(value="20", timeout=10_000)
logging.info("✅ Changed page size to 20 results (method 2 - select_option)")
page_size_changed = True
break
except Exception as select_err:
logging.debug("select_option failed: %s", select_err)
# Fallback: try setting value directly via JavaScript
try:
await select_el.evaluate("el => { el.value = '20'; el.dispatchEvent(new Event('change', { bubbles: true })); }")
await page.wait_for_timeout(1_000)
logging.info("✅ Changed page size to 20 results (method 2 - JavaScript)")
page_size_changed = True
break
except Exception as js_err:
logging.debug("JavaScript method also failed: %s", js_err)
continue
else:
# Custom dropdown - use click method
try:
await select_el.click(timeout=5_000)
await page.wait_for_timeout(500)
await opt.click(timeout=5_000)
logging.info("✅ Changed page size to 20 results (method 2 - custom dropdown)")
page_size_changed = True
break
except Exception as click_err:
logging.debug("Custom dropdown click failed: %s", click_err)
continue
if page_size_changed:
break
except Exception as e:
logging.debug("Error checking select element %d: %s", idx, e)
continue
except Exception as e2:
logging.debug("Method 2 failed: %s", e2)
# Method 3: Use locator to find option
if not page_size_changed:
try:
# Try finding by text content
all_options = await page.query_selector_all("option")
for opt in all_options:
opt_text = (await opt.inner_text()).strip()
if opt_text == "20":
# Get parent select and click it first
parent_select = await opt.evaluate_handle("el => el.parentElement")
if parent_select:
await parent_select.as_element().click()
await page.wait_for_timeout(500)
await opt.click()
logging.info("✅ Changed page size to 20 results (method 3)")
page_size_changed = True
break
except Exception as e3:
logging.debug("Method 3 failed: %s", e3)
if page_size_changed:
# Wait for page to reload with more results
await page.wait_for_timeout(3_000)
try:
await page.wait_for_load_state("networkidle", timeout=10_000)
except Exception:
pass
# Additional wait for courts to appear
await page.wait_for_timeout(3_000)
# Verify we can see more courts now
court_check = await page.query_selector_all("text=/Court\\s+\\d+/i")
logging.info("After page size change: Found %d court elements", len(court_check))
else:
logging.warning("Could not change page size, continuing with default (10 results)...")
except Exception as e:
logging.warning("Error changing page size: %s, continuing with default...", e)
# Now discover all courts on the single page (should include all courts now)
logging.info("Discovering all courts on page...")
# Wait a bit more and try multiple times to find courts
all_courts = []
max_attempts = 3
for attempt in range(max_attempts):
court_elements = await page.query_selector_all("text=/Court\\s+\\d+/i")
logging.info("Attempt %d/%d: Found %d court elements", attempt + 1, max_attempts, len(court_elements))
if len(court_elements) > 0:
for el in court_elements:
try:
label = (await el.inner_text()).strip()
# Extract just "Court X" format (handle both "Court 1" and "Court 01")
match = re.search(r'Court\s+0?(\d+)', label, re.IGNORECASE)
if match:
# Normalize to "Court X" format (remove leading zero)
court_num = str(int(match.group(1))) # Remove leading zeros
court_label = f"Court {court_num:>02}" # Format as "Court 01", "Court 02", etc.
if court_label not in all_courts:
all_courts.append(court_label)
except Exception:
continue
if len(all_courts) >= 10: # We should have at least 10 courts
break
if attempt < max_attempts - 1:
await page.wait_for_timeout(2_000)
court_names = sorted(all_courts, key=lambda x: int(re.search(r'\d+', x).group()))
logging.info("Discovered %d total courts: %s", len(court_names), court_names)
if not court_names:
raise RuntimeError("No courts found on booking page")
# Step 4: Find all court rows with their Choose buttons
# Build a mapping of court names to their Choose buttons
court_choose_map = {}
# Find all elements containing "Court X" text
all_court_elements = await page.query_selector_all("text=/Court\\s+\\d+/i")
for court_el in all_court_elements:
try:
court_text = (await court_el.inner_text()).strip()
match = re.search(r'Court\s+0?(\d+)', court_text, re.IGNORECASE)
if not match:
continue
# Normalize court label format
court_num = str(int(match.group(1))) # Remove leading zeros
court_label = f"Court {court_num:>02}" # Format as "Court 01", "Court 02", etc.
# Find the Choose button in the same row/container
try:
# Get the parent container (row, div, etc.)
parent_handle = await court_el.evaluate_handle("el => el.closest('tr, div.row, li, [class*=\"row\"], [class*=\"court\"]')")
if parent_handle:
parent_elem = parent_handle.as_element() if hasattr(parent_handle, 'as_element') else None
if parent_elem:
# Look for Choose button in the parent - try multiple selectors
choose_buttons = []
for selector in ["text=/Choose/i", "button:has-text('Choose')", "a:has-text('Choose')", "[class*='choose']", "[class*='select']"]:
try:
buttons = await parent_elem.query_selector_all(selector)
if buttons:
choose_buttons.extend(buttons)
except Exception:
continue
if choose_buttons:
court_choose_map[court_label] = choose_buttons[0]
continue
except Exception:
pass
# If not found in parent, try to find nearby Choose button
# Look for Choose buttons and match by proximity/index
if court_label not in court_choose_map:
all_choose_buttons = []
for selector in ["text=/Choose/i", "button:has-text('Choose')", "a:has-text('Choose')"]:
try:
buttons = await page.query_selector_all(selector)
if buttons:
all_choose_buttons.extend(buttons)
except Exception:
continue
# Match by index (assuming courts and buttons are in same order)
court_index = court_names.index(court_label) if court_label in court_names else -1
if court_index >= 0 and court_index < len(all_choose_buttons):
court_choose_map[court_label] = all_choose_buttons[court_index]
except Exception as exc:
logging.warning("Error mapping court to Choose button: %s", exc)
continue
# Step 5: Loop through each court and scrape its schedule
# Track which courts have been processed
processed_courts = set()
for court_name in court_names:
try:
# Skip if already processed
if court_name in processed_courts:
logging.info("Skipping %s (already processed)", court_name)
continue
current_progress = len(processed_courts) + 1
total_courts = len(court_names)
logging.info("Processing %s (%d/%d courts)", court_name, current_progress, total_courts)
# Call progress callback if provided
if progress_callback:
try:
progress_callback(
current=current_progress,
total=total_courts,
message=f"Grabbing {court_name} timings...",
court_name=court_name
)
except Exception:
pass # Don't fail scraping if callback fails
# Ensure page size is 20 before processing Court 12 or 13
if court_name in ["Court 12", "Court 13"]:
logging.info("Ensuring page size is 20 for %s...", court_name)
await _ensure_page_size_20(page)
await page.wait_for_timeout(2_000) # Wait for page to reload
try:
await page.wait_for_load_state("networkidle", timeout=5_000)
except Exception:
pass
# Re-find the Choose button for this court (elements may be stale after navigation)
choose_button = None
try:
# Brief wait for page to be ready
await page.wait_for_timeout(500)
# Check if we're on the court list page
court_list_check = await page.query_selector_all("text=/Court\\s+\\d+/i")
if not court_list_check or len(court_list_check) < 3:
logging.warning("Not on court list page, attempting to navigate back...")
# Try to get back to court list
try:
await page.goto(base_url, wait_until="domcontentloaded", timeout=30_000)
await page.wait_for_timeout(2_000)
await page.click("text='Book a Court'", timeout=10_000)
await page.wait_for_timeout(2_000)
# Wait a bit for court list to load, but don't wait for all buttons
court_elements = await page.query_selector_all("text=/Court\\s+\\d+/i")
if len(court_elements) < 5:
logging.error("Court list not loading after navigation back")
continue
except Exception as nav_exc:
logging.error("Failed to navigate back to court list: %s", nav_exc)
continue
# Use the exact pattern from Playwright codegen:
# page.get_by_role("listitem").filter(has_text="Choose Court 01 Read more").get_by_label("#: linkText + ' ' + Name #").click()
logging.debug("Looking for Choose button for %s using Playwright role-based selector...", court_name)
# Extract court number from court_name (e.g., "Court 01" -> "01")
court_num_match = re.search(r'Court\s+0?(\d+)', court_name, re.IGNORECASE)
if not court_num_match:
logging.warning("Could not extract court number from %s", court_name)
processed_courts.add(court_name)
continue
court_num = court_num_match.group(1)
# Format as "01", "02", etc. (with leading zero)
court_num_formatted = f"{int(court_num):02d}"
# Find the Choose button using regular methods
if not choose_button:
# Try to find the listitem with the court text and then the button
max_button_wait = 10_000
button_check_interval = 500
button_start_time = time.time() * 1000
button_elapsed = 0
while button_elapsed < max_button_wait and not choose_button:
try:
# Method 1: Find listitem with role="listitem" containing the court text
# Pattern: "Choose Court 01 Read more"
listitems = await page.query_selector_all("li[role='listitem'], [role='listitem']")
for li in listitems:
try:
li_text = await li.inner_text()
# Look for listitem containing "Choose Court XX" or "Court XX Read more"
if f"Court {court_num_formatted}" in li_text and ("Choose" in li_text or "Read more" in li_text):
# Found the listitem for this court, now find the button inside it
# Look for button with aria-label pattern or pm-confirm-button class
button = await li.query_selector("a[aria-label*='linkText'], a[aria-label*='Name'], a.pm-confirm-button, a[onclick*='onChooseClick']")
if button:
btn_text = (await button.inner_text()).strip().lower()
onclick_attr = await button.get_attribute("onclick") or ""
aria_label = await button.get_attribute("aria-label") or ""
# Verify it's a Choose button
if "choose" in btn_text or "onChooseClick" in onclick_attr or "linkText" in aria_label:
choose_button = button
logging.info("✅ Found Choose button for %s using listitem method", court_name)
break
except Exception:
continue
if choose_button:
break
# Method 2: Direct selector using text pattern
try:
# Find element containing "Choose Court XX" or "Court XX Read more"
court_listitem = await page.query_selector(f"text=/Choose.*Court\\s+{court_num_formatted}|Court\\s+{court_num_formatted}.*Read more/i")
if court_listitem:
# Find the Choose button within this listitem or its parent
button = await court_listitem.query_selector("a.pm-confirm-button, a[onclick*='onChooseClick'], a[aria-label*='linkText']")
if not button: