wohnbot/handlers/gesobau_handler.py

155 lines
7.1 KiB
Python
Raw Normal View History

2025-12-27 11:59:04 +01:00
from .base_handler import BaseHandler
import logging
import asyncio
2026-01-05 13:40:12 +01:00
import os
2026-01-01 15:27:25 +01:00
from pathlib import Path
2025-12-27 11:59:04 +01:00
logger = logging.getLogger(__name__)
2026-01-01 15:27:25 +01:00
DATA_DIR = Path("data/gesobau")
DATA_DIR.mkdir(parents=True, exist_ok=True)
2025-12-27 11:59:04 +01:00
class GesobauHandler(BaseHandler):
def __init__(self, browser_context):
self.context = browser_context
2025-12-27 11:59:04 +01:00
async def apply(self, listing: dict, result: dict) -> dict:
page = await self.context.new_page()
try:
2025-12-29 22:46:10 +01:00
logger.info(f"[GESOBAU] Opening page: {listing['link']}")
response = await page.goto(listing["link"], wait_until="networkidle")
logger.info("[GESOBAU] Page loaded")
2025-12-27 11:59:04 +01:00
await asyncio.sleep(2)
2025-12-29 22:46:10 +01:00
# 404 detection
status = response.status if response else None
page_title = await page.title()
2026-01-05 13:40:12 +01:00
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']}")
2025-12-29 22:46:10 +01:00
result["success"] = False
result["message"] = "Listing is no longer available (404). Application impossible. Will not retry."
2026-01-01 22:14:55 +01:00
result["deactivated"] = True
2025-12-29 22:46:10 +01:00
return result
2026-01-05 13:40:12 +01:00
# 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
2025-12-27 11:59:04 +01:00
2026-01-05 13:40:12 +01:00
# Save debug HTML and screenshot
try:
2026-01-05 13:40:12 +01:00
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)
2026-01-05 13:40:12 +01:00
logger.info(f"[GESOBAU] Saved debug HTML: gesobau_debug_{listing['id']}.html")
except Exception as e:
2026-01-05 13:40:12 +01:00
logger.warning(f"[GESOBAU] Could not save debug HTML: {e}")
2026-01-05 13:40:12 +01:00
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)
2025-12-27 11:59:04 +01:00
# Look for application button
logger.info("[GESOBAU] Searching for application button...")
2025-12-27 11:59:04 +01:00
selectors = [
'a[href*="bewerben"]',
'button:has-text("Bewerben")',
'a:has-text("Bewerben")',
2026-01-05 13:40:12 +01:00
'button:has-text("Interesse")',
'a:has-text("Kontakt")',
'button.btn',
2025-12-27 11:59:04 +01:00
]
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")
2025-12-27 11:59:04 +01:00
for btn in all_btns:
try:
if await btn.is_visible():
apply_btn = btn
logger.info(f"[GESOBAU] Found visible application button: {sel}")
2025-12-27 11:59:04 +01:00
break
except Exception as e:
logger.debug(f"[GESOBAU] Button visibility error: {e}")
2025-12-27 11:59:04 +01:00
if apply_btn:
break
if apply_btn:
await apply_btn.scroll_into_view_if_needed()
await asyncio.sleep(0.5)
2026-01-05 13:40:12 +01:00
logger.info("[GESOBAU] Clicking application button...")
2025-12-27 11:59:04 +01:00
await apply_btn.click()
await asyncio.sleep(2)
2026-01-05 13:40:12 +01:00
2025-12-29 22:46:10 +01:00
# Save screenshot and HTML after click
2026-01-05 13:40:12 +01:00
logger.info("[GESOBAU] Checking for confirmation...")
2025-12-29 22:46:10 +01:00
try:
2026-01-05 13:40:12 +01:00
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")
2025-12-29 22:46:10 +01:00
except Exception as e:
2026-01-05 13:40:12 +01:00
logger.warning(f"[GESOBAU] Could not save after-apply screenshot: {e}")
2025-12-29 22:46:10 +01:00
try:
html_after = await page.content()
2026-01-05 13:40:12 +01:00
with open(DATA_DIR / f"gesobau_after_apply_{listing['id']}.html", "w", encoding="utf-8") as f:
2025-12-29 22:46:10 +01:00
f.write(html_after)
2026-01-05 13:40:12 +01:00
logger.info(f"[GESOBAU] Saved after-apply HTML: gesobau_after_apply_{listing['id']}.html")
2025-12-29 22:46:10 +01:00
except Exception as e:
2026-01-05 13:40:12 +01:00
logger.warning(f"[GESOBAU] Could not save after-apply HTML: {e}")
2025-12-29 22:46:10 +01:00
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."
2025-12-27 11:59:04 +01:00
else:
logger.warning("[GESOBAU] No application button found.")
2025-12-27 11:59:04 +01:00
result["message"] = "No application button found."
2026-01-05 13:40:12 +01:00
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}")
2025-12-27 11:59:04 +01:00
except Exception as e:
result["message"] = f"Error during application: {e}"
logger.error(f"[GESOBAU] Application error: {e}")
finally:
await page.close()
return result