2025-08-04 19:54:56 +02:00
|
|
|
import datetime
|
2025-07-15 16:36:34 +02:00
|
|
|
import os
|
2025-08-04 19:54:56 +02:00
|
|
|
import time
|
|
|
|
|
|
2025-07-15 16:36:34 +02:00
|
|
|
from dotenv import load_dotenv
|
2025-08-02 22:37:17 +02:00
|
|
|
from auth.session import AnnySession
|
|
|
|
|
from booking.client import BookingClient
|
|
|
|
|
from utils.helpers import get_future_datetime
|
2025-08-04 19:54:56 +02:00
|
|
|
import pytz
|
2026-01-22 16:17:05 +01:00
|
|
|
from config.constants import RESOURCE_ID, TIMEZONE, SSO_PROVIDER
|
2025-07-15 16:36:34 +02:00
|
|
|
|
2025-08-02 22:37:17 +02:00
|
|
|
def main():
|
|
|
|
|
load_dotenv('.env', override=True)
|
|
|
|
|
username = os.getenv("USERNAME")
|
|
|
|
|
password = os.getenv("PASSWORD")
|
2025-07-15 16:36:34 +02:00
|
|
|
|
2026-01-22 16:17:05 +01:00
|
|
|
tz = pytz.timezone(TIMEZONE)
|
2025-08-04 19:54:56 +02:00
|
|
|
|
2025-08-02 22:37:17 +02:00
|
|
|
if not username or not password:
|
|
|
|
|
print("❌ Missing USERNAME or PASSWORD in .env")
|
|
|
|
|
return
|
2025-07-15 16:36:34 +02:00
|
|
|
|
2026-01-22 16:17:05 +01:00
|
|
|
session = AnnySession(username, password, provider_name=SSO_PROVIDER)
|
2025-08-02 22:37:17 +02:00
|
|
|
cookies = session.login()
|
2025-07-15 16:36:34 +02:00
|
|
|
|
2025-08-02 22:37:17 +02:00
|
|
|
if not cookies:
|
|
|
|
|
return
|
2025-07-15 16:36:34 +02:00
|
|
|
|
2025-08-02 22:37:17 +02:00
|
|
|
booking = BookingClient(cookies)
|
2025-08-04 19:54:56 +02:00
|
|
|
|
2026-01-22 16:17:05 +01:00
|
|
|
# Only wait for midnight if within 10 minutes, otherwise execute immediately
|
|
|
|
|
now = datetime.datetime.now(tz)
|
|
|
|
|
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:
|
|
|
|
|
print(f"⏳ Waiting {seconds_until_midnight:.0f} seconds until midnight...")
|
|
|
|
|
time.sleep(seconds_until_midnight)
|
|
|
|
|
elif seconds_until_midnight > max_wait_seconds:
|
|
|
|
|
print(f"⚡ More than 10 min until midnight, executing immediately...")
|
2025-08-04 19:54:56 +02:00
|
|
|
|
2025-08-09 18:28:04 +02:00
|
|
|
times = [
|
|
|
|
|
{
|
|
|
|
|
'start': '13:00:00',
|
|
|
|
|
'end': '18:00:00'
|
|
|
|
|
},
|
|
|
|
|
{
|
2025-08-22 09:31:42 +02:00
|
|
|
'start': '08:00:00',
|
2025-08-09 18:28:04 +02:00
|
|
|
'end': '12:00:00'
|
2025-08-22 09:31:42 +02:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'start': '19:00:00',
|
|
|
|
|
'end': '21:00:00'
|
|
|
|
|
},
|
2025-08-09 18:28:04 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
for time_ in times:
|
2026-01-22 16:17:05 +01:00
|
|
|
try:
|
|
|
|
|
start = get_future_datetime(hour=time_['start'])
|
|
|
|
|
end = get_future_datetime(hour=time_['end'])
|
2025-08-09 18:28:04 +02:00
|
|
|
|
2026-01-22 16:17:05 +01:00
|
|
|
if RESOURCE_ID:
|
|
|
|
|
resource_id = RESOURCE_ID
|
|
|
|
|
else:
|
|
|
|
|
resource_id = booking.find_available_resource(start, end)
|
2025-08-09 18:28:04 +02:00
|
|
|
|
2026-01-22 16:17:05 +01:00
|
|
|
if resource_id:
|
|
|
|
|
booking.reserve(resource_id, start, end)
|
|
|
|
|
else:
|
|
|
|
|
print("⚠️ No available slots found.")
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"❌ Error booking slot {time_['start']}-{time_['end']}: {e}")
|
2025-07-15 16:36:34 +02:00
|
|
|
|
2025-08-02 22:37:17 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|