Skip to content

Commit bb370db

Browse files
authored
Merge pull request #214 from cuappdev/nicole/capacity-scraper
fix log formatting + rollback error
2 parents cb254f2 + 8f2ba4d commit bb370db

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/scrapers/capacities_scraper.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import requests
22
import time
3+
import logging
34
from bs4 import BeautifulSoup
45
from collections import namedtuple
56
from datetime import datetime
@@ -71,7 +72,7 @@ def fetch_capacities():
7172

7273
# Map API name to database name
7374
if facility_name not in CAPACITY_MARKER_NAMES:
74-
print(f"Warning: No name mapping for facility: {facility_name}")
75+
logging.info(f"Warning: No name mapping for facility: {facility_name}")
7576
continue
7677

7778
db_name = CAPACITY_MARKER_NAMES[facility_name]
@@ -100,24 +101,33 @@ def fetch_capacities():
100101
facility = Facility.query.filter_by(id=facility_id).first()
101102

102103
if not facility or not facility.hours:
103-
print(f"Warning: No hours found for facility ID {facility_id}")
104+
logging.info(f"Warning: No hours found for facility ID {facility_id}")
104105
continue
105106

106107
current_time = int(time.time())
107-
108+
108109
is_open = any(hour.start_time <= current_time <= hour.end_time for hour in facility.hours)
110+
111+
logging.info("nicole: before send notif")
109112

110113
if is_open:
111114
topic_enum = gym_mapping[db_name]
112115
check_and_send_capacity_reminders(topic_enum.name, db_name, percent, last_percent)
113-
116+
117+
logging.info("nicole: after send notif")
118+
119+
logging.info("nicole: before add single cap")
120+
114121
add_single_capacity(count, facility_id, percent, updated)
122+
123+
logging.info("nicole: after add single cap")
115124

116125
except Exception as e:
117-
print(f"Error processing facility {facility.get('LocationName', 'unknown')}: {str(e)}")
118-
126+
logging.exception(f"Error processing facility {facility.get('LocationName', 'unknown')}: {str(e)}")
127+
db_session.rollback()
128+
119129
except Exception as e:
120-
print(f"Error fetching capacities: {str(e)}")
130+
logging.error(f"Error fetching capacities: {str(e)}")
121131
raise
122132

123133

@@ -177,10 +187,10 @@ def update_hourly_capacity(curDay, curHour):
177187
)
178188

179189
if hourly_average_capacity is not None:
180-
print("updating average")
190+
logging.info("updating average")
181191
hourly_average_capacity.update_hourly_average(capacity.percent)
182192
else:
183-
print("No hourly capacity, creating new entry")
193+
logging.info("No hourly capacity, creating new entry")
184194
hourly_average_capacity = HourlyAverageCapacity(
185195
facility_id=capacity.facility_id,
186196
average_percent=capacity.percent,
@@ -193,7 +203,7 @@ def update_hourly_capacity(curDay, curHour):
193203
db_session.commit()
194204

195205
except Exception as e:
196-
print(f"Error updating hourly average: {e}")
206+
logging.error(f"Error updating hourly average: {e}")
197207

198208

199209
def check_and_send_capacity_reminders(facility_name, readable_name, current_percent, last_percent):
@@ -214,5 +224,5 @@ def check_and_send_capacity_reminders(facility_name, readable_name, current_perc
214224

215225
for threshold in crossed_thresholds:
216226
topic_name = f"{facility_name}_{current_day_name}_{threshold}"
217-
print(f"Sending message to devices subscribed to {topic_name}")
227+
logging.info(f"Sending message to devices subscribed to {topic_name}")
218228
send_capacity_reminder(topic_name, readable_name, threshold)

0 commit comments

Comments
 (0)