major refactor (untested)

This commit is contained in:
Aron Petau 2025-12-27 11:59:04 +01:00
parent a29412b4da
commit a77a0c0393
22 changed files with 1037 additions and 24 deletions

1
handlers/__init__.py Normal file
View file

@ -0,0 +1 @@
# This file makes the handlers directory a Python package.

42
handlers/base_handler.py Normal file
View file

@ -0,0 +1,42 @@
from abc import ABC, abstractmethod
from playwright.async_api import Page
import logging
logger = logging.getLogger(__name__)
class BaseHandler(ABC):
def __init__(self, context):
self.context = context
@abstractmethod
async def apply(self, listing: dict, result: dict) -> dict:
"""Abstract method to handle the application process for a specific company."""
pass
async def handle_cookies(self, page: Page):
"""Handle cookie banners if present."""
try:
cookie_btn = await page.query_selector('button:has-text("Akzeptieren"), button:has-text("Alle akzeptieren")')
if cookie_btn and await cookie_btn.is_visible():
await cookie_btn.click()
logger.info("[BaseHandler] Dismissed cookie banner")
await asyncio.sleep(1)
except Exception as e:
logger.warning(f"[BaseHandler] Failed to handle cookies: {e}")
async def handle_consent(self, page: Page):
"""Handle consent manager banners if present."""
try:
consent_selectors = [
'#cmpbntyestxt', '.cmpboxbtnyes', 'a.cmpboxbtn.cmpboxbtnyes',
'#cmpwelcomebtnyes', '.cmptxt_btn_yes'
]
for sel in consent_selectors:
consent_btn = await page.query_selector(sel)
if consent_btn and await consent_btn.is_visible():
await consent_btn.click()
logger.info("[BaseHandler] Dismissed consent manager")
await asyncio.sleep(1)
break
except Exception as e:
logger.warning(f"[BaseHandler] Failed to handle consent manager: {e}")

View file

@ -0,0 +1,59 @@
from .base_handler import BaseHandler
import logging
import asyncio
logger = logging.getLogger(__name__)
class DegewoHandler(BaseHandler):
async def apply(self, listing: dict, result: dict) -> dict:
page = await self.context.new_page()
try:
logger.info(f"[DEGEWO] Opening page: {listing['link']}")
await page.goto(listing["link"], wait_until="networkidle")
logger.info("[DEGEWO] Page loaded")
await asyncio.sleep(2)
# Handle cookies and consent
await self.handle_cookies(page)
await self.handle_consent(page)
# Look for application button
logger.info("[DEGEWO] Looking for application button...")
selectors = [
'a[href*="bewerben"]',
'button:has-text("Bewerben")'
]
apply_btn = None
for sel in selectors:
all_btns = await page.query_selector_all(sel)
logger.info(f"[DEGEWO] Selector '{sel}' found {len(all_btns)} matches")
for btn in all_btns:
try:
if await btn.is_visible():
apply_btn = btn
logger.info(f"[DEGEWO] Found visible button with selector '{sel}'")
break
except Exception as e:
logger.warning(f"[DEGEWO] Error checking button visibility: {e}")
if apply_btn:
break
if apply_btn:
logger.info("[DEGEWO] Found application button, scrolling into view...")
await apply_btn.scroll_into_view_if_needed()
await asyncio.sleep(0.5)
logger.info("[DEGEWO] Clicking button...")
await apply_btn.click()
await asyncio.sleep(2)
result["success"] = True
result["message"] = "Application submitted successfully."
else:
result["message"] = "No application button found."
except Exception as e:
result["message"] = f"Error during application: {e}"
logger.error(f"[DEGEWO] Application error: {e}")
finally:
await page.close()
return result

View file

@ -0,0 +1,59 @@
from .base_handler import BaseHandler
import logging
import asyncio
logger = logging.getLogger(__name__)
class GesobauHandler(BaseHandler):
async def apply(self, listing: dict, result: dict) -> dict:
page = await self.context.new_page()
try:
logger.info(f"[GESOBAU] Opening page: {listing['link']}")
await page.goto(listing["link"], wait_until="networkidle")
logger.info("[GESOBAU] Page loaded")
await asyncio.sleep(2)
# Handle cookies and consent
await self.handle_cookies(page)
await self.handle_consent(page)
# Look for application button
logger.info("[GESOBAU] Looking for application button...")
selectors = [
'a[href*="bewerben"]',
'button:has-text("Bewerben")'
]
apply_btn = None
for sel in selectors:
all_btns = await page.query_selector_all(sel)
logger.info(f"[GESOBAU] Selector '{sel}' found {len(all_btns)} matches")
for btn in all_btns:
try:
if await btn.is_visible():
apply_btn = btn
logger.info(f"[GESOBAU] Found visible button with selector '{sel}'")
break
except Exception as e:
logger.warning(f"[GESOBAU] Error checking button visibility: {e}")
if apply_btn:
break
if apply_btn:
logger.info("[GESOBAU] Found application button, scrolling into view...")
await apply_btn.scroll_into_view_if_needed()
await asyncio.sleep(0.5)
logger.info("[GESOBAU] Clicking button...")
await apply_btn.click()
await asyncio.sleep(2)
result["success"] = True
result["message"] = "Application submitted successfully."
else:
result["message"] = "No application button found."
except Exception as e:
result["message"] = f"Error during application: {e}"
logger.error(f"[GESOBAU] Application error: {e}")
finally:
await page.close()
return result

View file

@ -0,0 +1,59 @@
from .base_handler import BaseHandler
import logging
import asyncio
logger = logging.getLogger(__name__)
class GewobagHandler(BaseHandler):
async def apply(self, listing: dict, result: dict) -> dict:
page = await self.context.new_page()
try:
logger.info(f"[GEWOBAG] Opening page: {listing['link']}")
await page.goto(listing["link"], wait_until="networkidle")
logger.info("[GEWOBAG] Page loaded")
await asyncio.sleep(2)
# Handle cookies and consent
await self.handle_cookies(page)
await self.handle_consent(page)
# Look for application button
logger.info("[GEWOBAG] Looking for application button...")
selectors = [
'a[href*="bewerben"]',
'button:has-text("Bewerben")'
]
apply_btn = None
for sel in selectors:
all_btns = await page.query_selector_all(sel)
logger.info(f"[GEWOBAG] Selector '{sel}' found {len(all_btns)} matches")
for btn in all_btns:
try:
if await btn.is_visible():
apply_btn = btn
logger.info(f"[GEWOBAG] Found visible button with selector '{sel}'")
break
except Exception as e:
logger.warning(f"[GEWOBAG] Error checking button visibility: {e}")
if apply_btn:
break
if apply_btn:
logger.info("[GEWOBAG] Found application button, scrolling into view...")
await apply_btn.scroll_into_view_if_needed()
await asyncio.sleep(0.5)
logger.info("[GEWOBAG] Clicking button...")
await apply_btn.click()
await asyncio.sleep(2)
result["success"] = True
result["message"] = "Application submitted successfully."
else:
result["message"] = "No application button found."
except Exception as e:
result["message"] = f"Error during application: {e}"
logger.error(f"[GEWOBAG] Application error: {e}")
finally:
await page.close()
return result

View file

@ -0,0 +1,62 @@
from .base_handler import BaseHandler
import logging
import asyncio
logger = logging.getLogger(__name__)
class HowogeHandler(BaseHandler):
async def apply(self, listing: dict, result: dict) -> dict:
page = await self.context.new_page()
try:
logger.info(f"[HOWOGE] Opening page: {listing['link']}")
await page.goto(listing["link"], wait_until="networkidle")
logger.info("[HOWOGE] Page loaded")
await asyncio.sleep(2)
# Handle cookies and consent
await self.handle_cookies(page)
await self.handle_consent(page)
# Look for "Besichtigung vereinbaren" button
logger.info("[HOWOGE] Looking for 'Besichtigung vereinbaren' button...")
selectors = [
'a[href*="besichtigung-vereinbaren"]',
'a:has-text("Besichtigung vereinbaren")',
'button:has-text("Besichtigung vereinbaren")',
'a:has-text("Anfragen")',
'button:has-text("Anfragen")'
]
apply_btn = None
for sel in selectors:
all_btns = await page.query_selector_all(sel)
logger.info(f"[HOWOGE] Selector '{sel}' found {len(all_btns)} matches")
for btn in all_btns:
try:
if await btn.is_visible():
apply_btn = btn
logger.info(f"[HOWOGE] Found visible button with selector '{sel}'")
break
except Exception as e:
logger.warning(f"[HOWOGE] Error checking button visibility: {e}")
if apply_btn:
break
if apply_btn:
logger.info("[HOWOGE] Found application button, scrolling into view...")
await apply_btn.scroll_into_view_if_needed()
await asyncio.sleep(0.5)
logger.info("[HOWOGE] Clicking button...")
await apply_btn.click()
await asyncio.sleep(2)
result["success"] = True
result["message"] = "Application submitted successfully."
else:
result["message"] = "No application button found."
except Exception as e:
result["message"] = f"Error during application: {e}"
logger.error(f"[HOWOGE] Application error: {e}")
finally:
await page.close()
return result

View file

@ -0,0 +1,59 @@
from .base_handler import BaseHandler
import logging
import asyncio
logger = logging.getLogger(__name__)
class StadtUndLandHandler(BaseHandler):
async def apply(self, listing: dict, result: dict) -> dict:
page = await self.context.new_page()
try:
logger.info(f"[STADT UND LAND] Opening page: {listing['link']}")
await page.goto(listing["link"], wait_until="networkidle")
logger.info("[STADT UND LAND] Page loaded")
await asyncio.sleep(2)
# Handle cookies and consent
await self.handle_cookies(page)
await self.handle_consent(page)
# Look for application button
logger.info("[STADT UND LAND] Looking for application button...")
selectors = [
'a[href*="bewerben"]',
'button:has-text("Bewerben")'
]
apply_btn = None
for sel in selectors:
all_btns = await page.query_selector_all(sel)
logger.info(f"[STADT UND LAND] Selector '{sel}' found {len(all_btns)} matches")
for btn in all_btns:
try:
if await btn.is_visible():
apply_btn = btn
logger.info(f"[STADT UND LAND] Found visible button with selector '{sel}'")
break
except Exception as e:
logger.warning(f"[STADT UND LAND] Error checking button visibility: {e}")
if apply_btn:
break
if apply_btn:
logger.info("[STADT UND LAND] Found application button, scrolling into view...")
await apply_btn.scroll_into_view_if_needed()
await asyncio.sleep(0.5)
logger.info("[STADT UND LAND] Clicking button...")
await apply_btn.click()
await asyncio.sleep(2)
result["success"] = True
result["message"] = "Application submitted successfully."
else:
result["message"] = "No application button found."
except Exception as e:
result["message"] = f"Error during application: {e}"
logger.error(f"[STADT UND LAND] Application error: {e}")
finally:
await page.close()
return result

59
handlers/wbm_handler.py Normal file
View file

@ -0,0 +1,59 @@
from .base_handler import BaseHandler
import logging
import asyncio
logger = logging.getLogger(__name__)
class WBMHandler(BaseHandler):
async def apply(self, listing: dict, result: dict) -> dict:
page = await self.context.new_page()
try:
logger.info(f"[WBM] Opening page: {listing['link']}")
await page.goto(listing["link"], wait_until="networkidle")
logger.info("[WBM] Page loaded")
await asyncio.sleep(2)
# Handle cookies and consent
await self.handle_cookies(page)
await self.handle_consent(page)
# Look for application button
logger.info("[WBM] Looking for application button...")
selectors = [
'a[href*="bewerben"]',
'button:has-text("Bewerben")'
]
apply_btn = None
for sel in selectors:
all_btns = await page.query_selector_all(sel)
logger.info(f"[WBM] Selector '{sel}' found {len(all_btns)} matches")
for btn in all_btns:
try:
if await btn.is_visible():
apply_btn = btn
logger.info(f"[WBM] Found visible button with selector '{sel}'")
break
except Exception as e:
logger.warning(f"[WBM] Error checking button visibility: {e}")
if apply_btn:
break
if apply_btn:
logger.info("[WBM] Found application button, scrolling into view...")
await apply_btn.scroll_into_view_if_needed()
await asyncio.sleep(0.5)
logger.info("[WBM] Clicking button...")
await apply_btn.click()
await asyncio.sleep(2)
result["success"] = True
result["message"] = "Application submitted successfully."
else:
result["message"] = "No application button found."
except Exception as e:
result["message"] = f"Error during application: {e}"
logger.error(f"[WBM] Application error: {e}")
finally:
await page.close()
return result