From 083ea29c21814310a438263aa4bb7f62ddef39bf Mon Sep 17 00:00:00 2001 From: Yigit Colakoglu Date: Tue, 1 Oct 2024 14:05:37 +0200 Subject: [PATCH] Fix dockerfile, switch to firefox --- Dockerfile | 9 +----- tgbot.py | 84 ++++++++++++++++++++++++++++-------------------------- xclient.py | 15 ++++------ 3 files changed, 51 insertions(+), 57 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9168ec0..e4ccfd4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -19,20 +19,13 @@ RUN apt-get update && \ build-essential \ libsqlite3-dev \ sqlite3 \ - firefox \ + firefox-esr \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* # Install the required Python packages RUN pip install --no-cache-dir -r requirements.txt -# Set up Chrome WebDriver for Selenium -RUN wget -q -O /app/chromedriver.zip https://storage.googleapis.com/chrome-for-testing-public/129.0.6668.70/linux64/chromedriver-linux64.zip && \ - unzip /app/chromedriver.zip && \ - mv /app/chromedriver-linux64/chromedriver /app && \ - chmod +x /app/chromedriver && \ - rm -rf /app/chromedriver.zip /app/chromedriver-linux64 - # Set environment variables for Chrome and ChromeDriver ENV PATH=/app:$PATH ENV DISPLAY=:99 diff --git a/tgbot.py b/tgbot.py index 2c0ba3b..0de72c8 100644 --- a/tgbot.py +++ b/tgbot.py @@ -243,7 +243,6 @@ class TelegramBot: start = datetime.now() end = start + timedelta(days=1) - slots = xclient.list_slots(start, end) if slots: @@ -346,6 +345,7 @@ class TelegramBot: self.bot.edit_message_reply_markup( call.message.chat.id, call.message.message_id ) + except Exception as e: self.bot.reply_to(call.message, f"Error: {str(e)}") return @@ -513,50 +513,54 @@ class TelegramBot: for chat_id, slots in list(self.watchlist.items()): try: xclient = self.get_xclient(chat_id) - except Exception as e: - print(f"Error polling watchlist: {str(e)}") - available_slots = [] - for slot in slots: - if slot.start < now: - # If the slot has expired - self.bot.send_message( - chat_id, - f"Slot {slot.start_stamp} has expired and is no longer available.", - ) - available_slots.append(slot) # Mark it to remove from watchlist - elif xclient.check_booking_availability(slot): - # If the slot becomes available - try: - xclient.make_booking(slot) - if (token := self.get_calendar_token(chat_id)) is not None: - self.calendar.create_event( - token, - { - "summary": "Gym Booking", - "start": { - "dateTime": slot.start.strftime( - "%Y-%m-%dT%H:%M:%S.%fZ" - ), - "timeZone": "Europe/Amsterdam", - }, - "end": { - "dateTime": slot.end.strftime( - "%Y-%m-%dT%H:%M:%S.%fZ" - ), - "timeZone": "Europe/Amsterdam", - }, - }, - ) + available_slots = [] + for slot in slots: + if slot.start < now: + # If the slot has expired self.bot.send_message( chat_id, - f"Slot {slot.start_stamp} is now available and has been booked for you.", + f"Slot {slot.start_stamp} has expired and is no longer available.", ) available_slots.append(slot) # Mark it to remove from watchlist - except Exception as e: - self.bot.send_message( - chat_id, f"Error booking slot {slot.start_stamp}: {str(e)}" - ) + elif xclient.check_booking_availability(slot): + # If the slot becomes available + try: + xclient.make_booking(slot) + if (token := self.get_calendar_token(chat_id)) is not None: + self.calendar.create_event( + token, + { + "summary": "Gym Booking", + "start": { + "dateTime": slot.start.strftime( + "%Y-%m-%dT%H:%M:%S.%fZ" + ), + "timeZone": "Europe/Amsterdam", + }, + "end": { + "dateTime": slot.end.strftime( + "%Y-%m-%dT%H:%M:%S.%fZ" + ), + "timeZone": "Europe/Amsterdam", + }, + }, + ) + self.bot.send_message( + chat_id, + f"Slot {slot.start_stamp} is now available and has been booked for you.", + ) + available_slots.append( + slot + ) # Mark it to remove from watchlist + except Exception as e: + self.bot.send_message( + chat_id, + f"Error booking slot {slot.start_stamp}: {str(e)}", + ) + + except Exception as e: + print(f"Error polling watchlist: {str(e)}") # Remove the expired or booked slots from the watchlist self.watchlist[chat_id] = [ diff --git a/xclient.py b/xclient.py index d198a16..4040871 100644 --- a/xclient.py +++ b/xclient.py @@ -1,4 +1,5 @@ from selenium import webdriver +from selenium.webdriver.firefox.options import Options from models import Booking import json from selenium.webdriver.common.by import By @@ -6,9 +7,7 @@ from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time import requests -import config from selenium.webdriver.chrome.service import Service -from webdriver_manager.chrome import ChromeDriverManager class LoginFailedException(Exception): @@ -33,13 +32,13 @@ class XClient: ) def fetch_access_token(self): - # Set up the WebDriver (make sure to use the correct path for your WebDriver) - driver = webdriver.Firefox() + options = webdriver.FirefoxOptions() + options.add_argument("--headless") + + driver = webdriver.Firefox(options=options) - print("Loading x.tudelft.nl") driver.get("https://x.tudelft.nl") - print("Done loading x.tudelft.nl") button = WebDriverWait(driver, 30).until( EC.element_to_be_clickable( (By.XPATH, "//span[contains(text(), 'TUDelft')]") @@ -67,18 +66,16 @@ class XClient: password_input.submit() - time.sleep(1) + time.sleep(2) cookie = driver.execute_script( "return window.localStorage.getItem('delcom_auth');" ) - print(cookie) if cookie is None: raise LoginFailedException("Logging in to X has failed") delcom_auth = json.loads(cookie) - print(cookie, delcom_auth) driver.quit()