Skip to content
16 changes: 12 additions & 4 deletions src/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,9 @@ def mutate(self, info, fcm_token, days_of_week, gyms, capacity_percent):
for day in validated_workout_days:
topic_name = f"{gym}_{day}_{capacity_percent}"
try:
messaging.subscribe_to_topic(fcm_token, topic_name)
response = messaging.subscribe_to_topic(fcm_token, topic_name)
if response.success_count == 0:
raise Exception(response.errors[0].reason)
except Exception as error:
raise GraphQLError(f"Error subscribing to topic for {topic_name}: {error}")

Expand Down Expand Up @@ -916,7 +918,9 @@ def mutate(self, info, reminder_id, gyms, days_of_week, capacity_percent):

for topic in topics:
try:
messaging.unsubscribe_from_topic(reminder.fcm_token, topic)
response = messaging.unsubscribe_from_topic(reminder.fcm_token, topic)
if response.success_count == 0:
raise Exception(response.errors[0].reason)
except Exception as error:
raise GraphQLError(f"Error subscribing to topic: {error}")

Expand All @@ -925,7 +929,9 @@ def mutate(self, info, reminder_id, gyms, days_of_week, capacity_percent):

for topic in topics:
try:
messaging.subscribe_to_topic(reminder.fcm_token, topic)
response = messaging.subscribe_to_topic(reminder.fcm_token, topic)
if response.success_count == 0:
raise Exception(response.errors[0].reason)
except Exception as error:
raise GraphQLError(f"Error subscribing to topic: {error}")

Expand Down Expand Up @@ -954,7 +960,9 @@ def mutate(self, info, reminder_id):

for topic in topics:
try:
messaging.unsubscribe_from_topic(reminder.fcm_token, topic)
response = messaging.unsubscribe_from_topic(reminder.fcm_token, topic)
if response.success_count == 0:
raise Exception(response.errors[0].reason)
except Exception as error:
raise GraphQLError(f"Error unsubscribing from topic {topic}: {error}")

Expand Down
28 changes: 15 additions & 13 deletions src/scrapers/capacities_scraper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests
import time
import logging
from bs4 import BeautifulSoup
from collections import namedtuple
from datetime import datetime
Expand Down Expand Up @@ -71,7 +72,7 @@ def fetch_capacities():

# Map API name to database name
if facility_name not in CAPACITY_MARKER_NAMES:
print(f"Warning: No name mapping for facility: {facility_name}")
logging.info(f"Warning: No name mapping for facility: {facility_name}")
continue

db_name = CAPACITY_MARKER_NAMES[facility_name]
Expand Down Expand Up @@ -100,24 +101,25 @@ def fetch_capacities():
facility = Facility.query.filter_by(id=facility_id).first()

if not facility or not facility.hours:
print(f"Warning: No hours found for facility ID {facility_id}")
logging.info(f"Warning: No hours found for facility ID {facility_id}")
continue

current_time = int(time.time())

is_open = any(hour.start_time <= current_time <= hour.end_time for hour in facility.hours)

if is_open:
topic_enum = gym_mapping[db_name]
check_and_send_capacity_reminders(topic_enum.name, db_name, percent, last_percent)

add_single_capacity(count, facility_id, percent, updated)

except Exception as e:
print(f"Error processing facility {facility.get('LocationName', 'unknown')}: {str(e)}")

logging.exception(f"Error processing facility {facility.get('LocationName', 'unknown')}: {str(e)}")
# db_session.rollback()

except Exception as e:
print(f"Error fetching capacities: {str(e)}")
logging.error(f"Error fetching capacities: {str(e)}")
raise


Expand Down Expand Up @@ -177,10 +179,10 @@ def update_hourly_capacity(curDay, curHour):
)

if hourly_average_capacity is not None:
print("updating average")
logging.info("updating average")
hourly_average_capacity.update_hourly_average(capacity.percent)
else:
print("No hourly capacity, creating new entry")
logging.info("No hourly capacity, creating new entry")
hourly_average_capacity = HourlyAverageCapacity(
facility_id=capacity.facility_id,
average_percent=capacity.percent,
Expand All @@ -193,7 +195,7 @@ def update_hourly_capacity(curDay, curHour):
db_session.commit()

except Exception as e:
print(f"Error updating hourly average: {e}")
logging.error(f"Error updating hourly average: {e}")


def check_and_send_capacity_reminders(facility_name, readable_name, current_percent, last_percent):
Expand All @@ -214,5 +216,5 @@ def check_and_send_capacity_reminders(facility_name, readable_name, current_perc

for threshold in crossed_thresholds:
topic_name = f"{facility_name}_{current_day_name}_{threshold}"
print(f"Sending message to devices subscribed to {topic_name}")
logging.info(f"Sending message to devices subscribed to {topic_name}")
send_capacity_reminder(topic_name, readable_name, threshold)
Loading