Skip to content

Commit d475ac1

Browse files
authored
Merge pull request #184 from cuappdev/temp_merge
Merging updated capacity scraper to release
2 parents c128cb6 + 723e7a5 commit d475ac1

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

src/scrapers/capacities_scraper.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from src.models.capacity import Capacity
77
from src.utils.constants import (
88
C2C_URL,
9+
CRC_URL_NEW,
910
CAPACITY_MARKER_COUNTS,
1011
CAPACITY_MARKER_NAMES,
1112
CAPACITY_MARKER_UPDATED,
@@ -14,8 +15,8 @@
1415
)
1516
from src.utils.utils import get_facility_id, unix_time
1617

17-
18-
def fetch_capacities():
18+
# Legacy scraper from old webpage using CRC_URL
19+
def fetch_capacities_old():
1920
"""
2021
Fetch capacities for all facilities from Connect2Concepts.
2122
"""
@@ -49,6 +50,44 @@ def fetch_capacities():
4950
# Add to sheets
5051
add_single_capacity(count, facility_id, percent, updated)
5152

53+
# New scraper from new API using CRC_URL_NEW
54+
def fetch_capacities():
55+
"""Fetch capacities from the new JSON API endpoint."""
56+
try:
57+
headers = {
58+
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0"
59+
}
60+
61+
response = requests.get(CRC_URL_NEW, headers=headers)
62+
facilities = response.json()
63+
64+
for facility in facilities:
65+
try:
66+
facility_name = facility["LocationName"]
67+
68+
# Map API name to database name
69+
if facility_name not in CAPACITY_MARKER_NAMES:
70+
print(f"Warning: No name mapping for facility: {facility_name}")
71+
continue
72+
73+
db_name = CAPACITY_MARKER_NAMES[facility_name]
74+
facility_id = get_facility_id(db_name)
75+
76+
count = int(facility["LastCount"])
77+
updated_str = facility["LastUpdatedDateAndTime"]
78+
total_capacity = int(facility["TotalCapacity"])
79+
80+
percent = count / total_capacity if total_capacity > 0 else 0.0
81+
updated = datetime.strptime(updated_str.split(".")[0], "%Y-%m-%dT%H:%M:%S")
82+
83+
add_single_capacity(count, facility_id, percent, updated)
84+
85+
except Exception as e:
86+
print(f"Error processing facility {facility.get('LocationName', 'unknown')}: {str(e)}")
87+
88+
except Exception as e:
89+
print(f"Error fetching capacities: {str(e)}")
90+
raise
5291

5392
def add_single_capacity(count, facility_id, percent, updated):
5493
"""

src/utils/constants.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
# Base URL for Cornell Recreation Website
77
BASE_URL = "https://scl.cornell.edu/recreation/"
88

9-
# The path for capacities
9+
# The old path for capacities
1010
C2C_URL = "https://connect2concepts.com/connect2/?type=bar&key=355de24d-d0e4-4262-ae97-bc0c78b92839&loc_status=false"
11+
# The new path for capacities
12+
CRC_URL_NEW = "https://goboardapi.azurewebsites.net/api/FacilityCount/GetCountsByAccount?AccountAPIKey=355de24d-d0e4-4262-ae97-bc0c78b92839"
1113

1214
# The marker for counts in the HTML
1315
CAPACITY_MARKER_COUNTS = "Last Count: "

0 commit comments

Comments
 (0)