wohnbot/handlers/base_handler.py

42 lines
1.7 KiB
Python
Raw Normal View History

2025-12-27 11:59:04 +01:00
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}")