2025-12-27 11:59:04 +01:00
|
|
|
from .base_handler import BaseHandler
|
|
|
|
|
import logging
|
|
|
|
|
import asyncio
|
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/gewobag")
|
|
|
|
|
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
2025-12-27 11:59:04 +01:00
|
|
|
|
|
|
|
|
class GewobagHandler(BaseHandler):
|
2025-12-28 19:59:31 +01:00
|
|
|
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:
|
|
|
|
|
logger.info(f"[GEWOBAG] Opening page: {listing['link']}")
|
2025-12-28 19:59:31 +01:00
|
|
|
response = await page.goto(listing["link"], wait_until="networkidle")
|
2025-12-27 11:59:04 +01:00
|
|
|
logger.info("[GEWOBAG] Page loaded")
|
|
|
|
|
await asyncio.sleep(2)
|
|
|
|
|
|
2025-12-28 19:59:31 +01:00
|
|
|
# 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."
|
2026-01-01 22:14:55 +01:00
|
|
|
result["deactivated"] = True
|
2025-12-28 19:59:31 +01:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
# Always handle cookies and consent before anything else
|
2025-12-27 11:59:04 +01:00
|
|
|
await self.handle_cookies(page)
|
|
|
|
|
await self.handle_consent(page)
|
|
|
|
|
|
2025-12-28 19:59:31 +01:00
|
|
|
# Save HTML after modal handling for debugging
|
|
|
|
|
try:
|
|
|
|
|
html_content = await page.content()
|
2026-01-01 15:27:25 +01:00
|
|
|
with open(DATA_DIR / "gewobag_debug.html", "w", encoding="utf-8") as f:
|
2025-12-28 19:59:31 +01:00
|
|
|
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
|
2025-12-27 11:59:04 +01:00
|
|
|
logger.info("[GEWOBAG] Looking for application button...")
|
|
|
|
|
selectors = [
|
2025-12-28 19:59:31 +01:00
|
|
|
'button.rental-contact',
|
|
|
|
|
'button:has-text("Anfrage senden")',
|
|
|
|
|
'div.contact-button button',
|
|
|
|
|
'iframe#contact-iframe',
|
2025-12-27 11:59:04 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
2025-12-28 19:59:31 +01:00
|
|
|
# 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}")
|
|
|
|
|
|
2025-12-27 11:59:04 +01:00
|
|
|
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)
|
2025-12-29 22:46:10 +01:00
|
|
|
# --- 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."
|
2025-12-27 11:59:04 +01:00
|
|
|
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
|