Refactor SSO authentication to support multiple providers and enhance error handling
This commit is contained in:
parent
862518a45f
commit
c4cef0d9b5
10 changed files with 233 additions and 66 deletions
43
main.py
43
main.py
|
|
@ -7,20 +7,20 @@ from auth.session import AnnySession
|
|||
from booking.client import BookingClient
|
||||
from utils.helpers import get_future_datetime
|
||||
import pytz
|
||||
from config.constants import RESSOURCE_ID
|
||||
from config.constants import RESOURCE_ID, TIMEZONE, SSO_PROVIDER
|
||||
|
||||
def main():
|
||||
load_dotenv('.env', override=True)
|
||||
username = os.getenv("USERNAME")
|
||||
password = os.getenv("PASSWORD")
|
||||
|
||||
start_time = datetime.datetime.now(pytz.timezone('Europe/Berlin'))
|
||||
tz = pytz.timezone(TIMEZONE)
|
||||
|
||||
if not username or not password:
|
||||
print("❌ Missing USERNAME or PASSWORD in .env")
|
||||
return
|
||||
|
||||
session = AnnySession(username, password)
|
||||
session = AnnySession(username, password, provider_name=SSO_PROVIDER)
|
||||
cookies = session.login()
|
||||
|
||||
if not cookies:
|
||||
|
|
@ -28,8 +28,17 @@ def main():
|
|||
|
||||
booking = BookingClient(cookies)
|
||||
|
||||
while start_time.day == datetime.datetime.now(pytz.timezone('Europe/Berlin')).day:
|
||||
time.sleep(1)
|
||||
# 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...")
|
||||
|
||||
times = [
|
||||
{
|
||||
|
|
@ -47,19 +56,21 @@ def main():
|
|||
]
|
||||
|
||||
for time_ in times:
|
||||
try:
|
||||
start = get_future_datetime(hour=time_['start'])
|
||||
end = get_future_datetime(hour=time_['end'])
|
||||
|
||||
start = get_future_datetime(hour=time_['start'])
|
||||
end = get_future_datetime(hour=time_['end'])
|
||||
if RESOURCE_ID:
|
||||
resource_id = RESOURCE_ID
|
||||
else:
|
||||
resource_id = booking.find_available_resource(start, end)
|
||||
|
||||
if RESSOURCE_ID:
|
||||
resource_id = RESSOURCE_ID
|
||||
else:
|
||||
resource_id = booking.find_available_resource(start, end)
|
||||
|
||||
if resource_id:
|
||||
booking.reserve(resource_id, start, end)
|
||||
else:
|
||||
print("⚠️ No available slots found.")
|
||||
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}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue