from .base_handler import BaseHandler import logging import asyncio import os from pathlib import Path logger = logging.getLogger(__name__) DATA_DIR = Path("data/gesobau") DATA_DIR.mkdir(parents=True, exist_ok=True) class GesobauHandler(BaseHandler): def __init__(self, browser_context): self.context = browser_context async def apply(self, listing: dict, result: dict) -> dict: page = await self.context.new_page() try: logger.info(f"[GESOBAU] Opening page: {listing['link']}") response = await page.goto(listing["link"], wait_until="networkidle") logger.info("[GESOBAU] Page loaded") await asyncio.sleep(2) # 404 detection status = response.status if response else None page_title = await page.title() page_content = await page.content() is_404 = ( status == 404 or (page_title and "404" in page_title) or (page_title and "nicht gefunden" in page_title.lower()) or ("Angebot nicht mehr verfügbar" in page_content) ) if is_404: logger.warning(f"[GESOBAU] Listing is down (404 or unavailable): {listing['link']}") result["success"] = False result["message"] = "Listing is no longer available (404). Application impossible. Will not retry." result["deactivated"] = True return result # Dismiss cookie banner try: cookie_btn = await page.query_selector('#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll, button:has-text("Alle akzeptieren")') if cookie_btn and await cookie_btn.is_visible(): await cookie_btn.click() logger.info("[GESOBAU] Dismissed cookie banner") await asyncio.sleep(1) except: pass # Save debug HTML and screenshot try: html_content = await page.content() with open(DATA_DIR / f"gesobau_debug_{listing['id']}.html", "w", encoding="utf-8") as f: f.write(html_content) logger.info(f"[GESOBAU] Saved debug HTML: gesobau_debug_{listing['id']}.html") except Exception as e: logger.warning(f"[GESOBAU] Could not save debug HTML: {e}") try: await page.screenshot(path=DATA_DIR / f"gesobau_page_{listing['id']}.png", full_page=True) logger.info(f"[GESOBAU] Saved page screenshot: gesobau_page_{listing['id']}.png") except Exception as e: logger.warning(f"[GESOBAU] Could not save screenshot: {e}") # Log listing details await self.log_listing_details(listing) # Look for application button logger.info("[GESOBAU] Searching for application button...") selectors = [ 'a[href*="bewerben"]', 'button:has-text("Bewerben")', 'a:has-text("Bewerben")', 'button:has-text("Interesse")', 'a:has-text("Kontakt")', 'button.btn', ] apply_btn = None for sel in selectors: all_btns = await page.query_selector_all(sel) logger.debug(f"[GESOBAU] Selector '{sel}': {len(all_btns)} matches") for btn in all_btns: try: if await btn.is_visible(): apply_btn = btn logger.info(f"[GESOBAU] Found visible application button: {sel}") break except Exception as e: logger.debug(f"[GESOBAU] Button visibility error: {e}") if apply_btn: break if apply_btn: await apply_btn.scroll_into_view_if_needed() await asyncio.sleep(0.5) logger.info("[GESOBAU] Clicking application button...") await apply_btn.click() await asyncio.sleep(2) # Save screenshot and HTML after click logger.info("[GESOBAU] Checking for confirmation...") try: await page.screenshot(path=DATA_DIR / f"gesobau_after_apply_{listing['id']}.png", full_page=True) logger.info(f"[GESOBAU] Saved after-apply screenshot: gesobau_after_apply_{listing['id']}.png") except Exception as e: logger.warning(f"[GESOBAU] Could not save after-apply screenshot: {e}") try: html_after = await page.content() with open(DATA_DIR / f"gesobau_after_apply_{listing['id']}.html", "w", encoding="utf-8") as f: f.write(html_after) logger.info(f"[GESOBAU] Saved after-apply HTML: gesobau_after_apply_{listing['id']}.html") except Exception as e: logger.warning(f"[GESOBAU] Could not save after-apply HTML: {e}") logger.warning(f"[GESOBAU] Could not save HTML after apply: {e}") # Look for confirmation message on the page confirmation_selectors = [ 'text="Vielen Dank"', 'text="Ihre Anfrage wurde gesendet"', 'text="Bestätigung"', 'div:has-text("Vielen Dank")', 'div:has-text("Ihre Anfrage wurde gesendet")', ] confirmed = False for sel in confirmation_selectors: try: el = await page.query_selector(sel) if el and await el.is_visible(): logger.info(f"[GESOBAU] Found confirmation element: {sel}") confirmed = True break except Exception as e: logger.debug(f"[GESOBAU] Error checking confirmation selector {sel}: {e}") if confirmed: result["success"] = True result["message"] = "Application submitted and confirmation detected." else: logger.warning("[GESOBAU] No confirmation message detected after application click.") result["success"] = False result["message"] = "Clicked application button, but no confirmation detected. Check screenshot and HTML." else: logger.warning("[GESOBAU] No application button found.") result["message"] = "No application button found." screenshot_path = DATA_DIR / f"gesobau_nobtn_{listing['id']}.png" await page.screenshot(path=str(screenshot_path)) logger.info(f"[GESOBAU] Saved no-button screenshot: {screenshot_path}") except Exception as e: result["message"] = f"Error during application: {e}" logger.error(f"[GESOBAU] Application error: {e}") finally: await page.close() return result