Fix dockerfile, switch to firefox

This commit is contained in:
Yigit Colakoglu 2024-10-01 14:05:37 +02:00
parent 10fb02c90d
commit 083ea29c21
3 changed files with 51 additions and 57 deletions

View File

@ -19,20 +19,13 @@ RUN apt-get update && \
build-essential \ build-essential \
libsqlite3-dev \ libsqlite3-dev \
sqlite3 \ sqlite3 \
firefox \ firefox-esr \
&& apt-get clean \ && apt-get clean \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
# Install the required Python packages # Install the required Python packages
RUN pip install --no-cache-dir -r requirements.txt 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 # Set environment variables for Chrome and ChromeDriver
ENV PATH=/app:$PATH ENV PATH=/app:$PATH
ENV DISPLAY=:99 ENV DISPLAY=:99

View File

@ -243,7 +243,6 @@ class TelegramBot:
start = datetime.now() start = datetime.now()
end = start + timedelta(days=1) end = start + timedelta(days=1)
slots = xclient.list_slots(start, end) slots = xclient.list_slots(start, end)
if slots: if slots:
@ -346,6 +345,7 @@ class TelegramBot:
self.bot.edit_message_reply_markup( self.bot.edit_message_reply_markup(
call.message.chat.id, call.message.message_id call.message.chat.id, call.message.message_id
) )
except Exception as e: except Exception as e:
self.bot.reply_to(call.message, f"Error: {str(e)}") self.bot.reply_to(call.message, f"Error: {str(e)}")
return return
@ -513,50 +513,54 @@ class TelegramBot:
for chat_id, slots in list(self.watchlist.items()): for chat_id, slots in list(self.watchlist.items()):
try: try:
xclient = self.get_xclient(chat_id) xclient = self.get_xclient(chat_id)
except Exception as e:
print(f"Error polling watchlist: {str(e)}")
available_slots = [] available_slots = []
for slot in slots: for slot in slots:
if slot.start < now: if slot.start < now:
# If the slot has expired # 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",
},
},
)
self.bot.send_message( self.bot.send_message(
chat_id, 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 available_slots.append(slot) # Mark it to remove from watchlist
except Exception as e: elif xclient.check_booking_availability(slot):
self.bot.send_message( # If the slot becomes available
chat_id, f"Error booking slot {slot.start_stamp}: {str(e)}" 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 # Remove the expired or booked slots from the watchlist
self.watchlist[chat_id] = [ self.watchlist[chat_id] = [

View File

@ -1,4 +1,5 @@
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from models import Booking from models import Booking
import json import json
from selenium.webdriver.common.by import By 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 from selenium.webdriver.support import expected_conditions as EC
import time import time
import requests import requests
import config
from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
class LoginFailedException(Exception): class LoginFailedException(Exception):
@ -33,13 +32,13 @@ class XClient:
) )
def fetch_access_token(self): def fetch_access_token(self):
# Set up the WebDriver (make sure to use the correct path for your WebDriver) options = webdriver.FirefoxOptions()
driver = webdriver.Firefox() options.add_argument("--headless")
driver = webdriver.Firefox(options=options)
print("Loading x.tudelft.nl")
driver.get("https://x.tudelft.nl") driver.get("https://x.tudelft.nl")
print("Done loading x.tudelft.nl")
button = WebDriverWait(driver, 30).until( button = WebDriverWait(driver, 30).until(
EC.element_to_be_clickable( EC.element_to_be_clickable(
(By.XPATH, "//span[contains(text(), 'TUDelft')]") (By.XPATH, "//span[contains(text(), 'TUDelft')]")
@ -67,18 +66,16 @@ class XClient:
password_input.submit() password_input.submit()
time.sleep(1) time.sleep(2)
cookie = driver.execute_script( cookie = driver.execute_script(
"return window.localStorage.getItem('delcom_auth');" "return window.localStorage.getItem('delcom_auth');"
) )
print(cookie)
if cookie is None: if cookie is None:
raise LoginFailedException("Logging in to X has failed") raise LoginFailedException("Logging in to X has failed")
delcom_auth = json.loads(cookie) delcom_auth = json.loads(cookie)
print(cookie, delcom_auth)
driver.quit() driver.quit()