wohnbot/handlers/gewobag_handler.py
2025-12-29 22:46:10 +01:00

142 lines
No EOL
6.7 KiB
Python

from .base_handler import BaseHandler
import logging
import asyncio
logger = logging.getLogger(__name__)
class GewobagHandler(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"[GEWOBAG] Opening page: {listing['link']}")
response = await page.goto(listing["link"], wait_until="networkidle")
logger.info("[GEWOBAG] Page loaded")
await asyncio.sleep(2)
# Detect 404 by status or page title
status = response.status if response else None
page_title = await page.title()
if status == 404 or (page_title and "404" in page_title):
logger.warning(f"[GEWOBAG] Listing is down (404): {listing['link']}")
result["success"] = False
result["message"] = "Listing is no longer available (404). Application impossible. Will not retry."
result["permanent_fail"] = True
return result
# Always handle cookies and consent before anything else
await self.handle_cookies(page)
await self.handle_consent(page)
# Save HTML after modal handling for debugging
try:
html_content = await page.content()
with open("data/gewobag_debug.html", "w", encoding="utf-8") as f:
f.write(html_content)
except Exception as e:
logger.warning(f"[GEWOBAG] Could not save debug HTML: {e}")
# Log listing details
await self.log_listing_details(listing)
# Look for application button ("Anfrage senden") in tab or footer
logger.info("[GEWOBAG] Looking for application button...")
selectors = [
'button.rental-contact',
'button:has-text("Anfrage senden")',
'div.contact-button button',
'iframe#contact-iframe',
]
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 not found, check for iframe (Wohnungshelden)
if not apply_btn:
iframe = await page.query_selector('iframe#contact-iframe')
if iframe:
logger.info("[GEWOBAG] Found Wohnungshelden iframe, switching context...")
frame = await iframe.content_frame()
if frame:
# Try to find a submit/apply button in the iframe
iframe_btns = await frame.query_selector_all('button, input[type="submit"]')
for btn in iframe_btns:
try:
if await btn.is_visible():
apply_btn = btn
logger.info("[GEWOBAG] Found visible button in iframe")
break
except Exception as e:
logger.warning(f"[GEWOBAG] Error checking iframe button visibility: {e}")
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)
# --- Post-click confirmation logic ---
logger.info("[GEWOBAG] Clicked application button, checking for confirmation...")
# Save screenshot and HTML after click
try:
await page.screenshot(path="data/gewobag_after_apply.png")
logger.info("[GEWOBAG] Saved screenshot after application click.")
except Exception as e:
logger.warning(f"[GEWOBAG] Could not save screenshot: {e}")
try:
html_after = await page.content()
with open("data/gewobag_after_apply.html", "w", encoding="utf-8") as f:
f.write(html_after)
logger.info("[GEWOBAG] Saved HTML after application click.")
except Exception as e:
logger.warning(f"[GEWOBAG] 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"[GEWOBAG] Found confirmation element: {sel}")
confirmed = True
break
except Exception as e:
logger.debug(f"[GEWOBAG] Error checking confirmation selector {sel}: {e}")
if confirmed:
result["success"] = True
result["message"] = "Application submitted and confirmation detected."
else:
logger.warning("[GEWOBAG] No confirmation message detected after application click.")
result["success"] = False
result["message"] = "Clicked application button, but no confirmation detected. Check screenshot and HTML."
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