anny-booking-automation/main.py

79 lines
2.7 KiB
Python
Raw Permalink Normal View History

import datetime
2025-07-15 16:36:34 +02:00
import os
import time
2025-07-15 16:36:34 +02:00
from dotenv import load_dotenv
from auth.session import AnnySession
from booking.client import BookingClient
from utils.helpers import get_future_datetime
import pytz
2026-02-07 13:50:59 +01:00
from config.constants import TIMEZONE, SSO_PROVIDER, BOOKING_TIMES, RESOURCE_IDS
2025-07-15 16:36:34 +02:00
2026-02-06 17:42:40 +01:00
def main():
2026-02-06 17:42:40 +01:00
load_dotenv(".env", override=True)
username = os.getenv("USERNAME")
password = os.getenv("PASSWORD")
2025-07-15 16:36:34 +02:00
tz = pytz.timezone(TIMEZONE)
if not username or not password:
print("❌ Missing USERNAME or PASSWORD in .env")
return
2025-07-15 16:36:34 +02:00
session = AnnySession(username, password, provider_name=SSO_PROVIDER)
cookies = session.login()
2025-07-15 16:36:34 +02:00
if not cookies:
return
2025-07-15 16:36:34 +02:00
booking = BookingClient(cookies)
# Only wait for midnight if within 10 minutes, otherwise execute immediately
now = datetime.datetime.now(tz)
2026-02-06 17:42:40 +01:00
midnight = (now + datetime.timedelta(days=1)).replace(
hour=0, minute=0, second=0, microsecond=0
)
seconds_until_midnight = (midnight - now).total_seconds()
max_wait_seconds = 10 * 60 # 10 minutes
if 0 < seconds_until_midnight <= max_wait_seconds:
2026-02-07 13:50:59 +01:00
print(f"⏳ Waiting {seconds_until_midnight:.0f} seconds until midnight...")
time.sleep(seconds_until_midnight)
elif seconds_until_midnight > max_wait_seconds:
2026-02-06 17:42:40 +01:00
print("⚡ More than 10 min until midnight, executing immediately...")
for time_ in BOOKING_TIMES:
try:
2026-02-06 17:42:40 +01:00
start = get_future_datetime(hour=time_["start"])
end = get_future_datetime(hour=time_["end"])
2026-02-06 20:05:16 +01:00
# maybe add if reservation fails try next id and if everything fails do ^
for seat_id in RESOURCE_IDS:
resource_id = seat_id
reservation_success = booking.reserve(resource_id, start, end)
if reservation_success:
2026-02-10 14:03:18 +01:00
print("✅ Reservation success for %d" % resource_id)
# return True
else:
print("❌ Reservation failure for %d" % resource_id)
2026-02-06 20:05:16 +01:00
2026-02-07 13:50:59 +01:00
# if RESOURCE_ID:
# resource_id = RESOURCE_ID
# reservation_success = booking.reserve(resource_id, start, end)
# if reservation_success:
# return True
2026-02-06 20:05:16 +01:00
resource_id = booking.find_available_resource(start, end)
reservation_success = booking.reserve(resource_id, start, end)
if reservation_success:
return True
else:
2026-02-06 20:05:16 +01:00
print("⚠️ No available slots found. ( or other error )")
except Exception as e:
print(f"❌ Error booking slot {time_['start']}-{time_['end']}: {e}")
2025-07-15 16:36:34 +02:00
2026-02-06 17:42:40 +01:00
if __name__ == "__main__":
main()