wohnbot/handlers/degewo_handler.py

95 lines
4.6 KiB
Python
Raw Normal View History

2025-12-29 22:46:10 +01:00
from handlers.base_handler import BaseHandler
2025-12-27 11:59:04 +01:00
import logging
import asyncio
2025-12-29 22:46:10 +01:00
import os
from pathlib import Path
2025-12-27 11:59:04 +01:00
logger = logging.getLogger(__name__)
class DegewoHandler(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:
2025-12-29 22:46:10 +01:00
DATA_DIR = Path("data/degewo")
DATA_DIR.mkdir(parents=True, exist_ok=True)
2025-12-27 11:59:04 +01:00
page = await self.context.new_page()
try:
2025-12-29 22:46:10 +01:00
logger.info(f"[DEGEWO] Opening page: {listing['link']}")
response = await page.goto(listing["link"], wait_until="networkidle")
2025-12-29 22:46:10 +01:00
logger.info("[DEGEWO] 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()
if status == 404 or (page_title and "404" in page_title):
logger.warning(f"[DEGEWO] 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
2025-12-29 22:46:10 +01:00
await page.close()
return result
2025-12-29 22:46:10 +01:00
# Check for 'INSERAT DEAKTIVIERT' (deactivated listing)
page_content = await page.content()
if "INSERAT DEAKTIVIERT" in page_content or "Inserat deaktiviert" in page_content:
logger.warning("[DEGEWO] Listing is deactivated (INSERAT DEAKTIVIERT detected), treating as 404")
result["success"] = False
result["message"] = "Listing deactivated (404)"
result["deactivated"] = True # Mark for removal from retries
await page.close()
return result
2025-12-27 11:59:04 +01:00
2025-12-29 22:46:10 +01:00
# Dismiss cookie banner
try:
2025-12-29 22:46:10 +01:00
cookie_btn = await page.query_selector('button:has-text("Alle akzeptieren"), #CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll')
if cookie_btn and await cookie_btn.is_visible():
await cookie_btn.click()
logger.info("[DEGEWO] Dismissed cookie banner")
await asyncio.sleep(1)
except Exception as e:
2025-12-29 22:46:10 +01:00
logger.debug(f"[DEGEWO] Cookie banner dismiss failed: {e}")
2025-12-29 22:46:10 +01:00
logger.info("[DEGEWO] Looking for kontaktieren button...")
apply_btn = await page.query_selector('a:has-text("kontaktieren"), button:has-text("kontaktieren"), a:has-text("Kontaktieren"), button:has-text("Kontaktieren")')
if apply_btn and await apply_btn.is_visible():
logger.info("[DEGEWO] Found kontaktieren button, clicking...")
2025-12-27 11:59:04 +01:00
await apply_btn.click()
2025-12-29 22:46:10 +01:00
await asyncio.sleep(3)
# Degewo uses Wohnungshelden iframe for the application form
# Find the iframe and get its URL to navigate directly
iframe_element = await page.query_selector('iframe[src*="wohnungshelden.de"]')
if iframe_element:
iframe_url = await iframe_element.get_attribute('src')
logger.info(f"[DEGEWO] Found Wohnungshelden iframe: {iframe_url}")
# Navigate to the iframe URL directly in a new page for full access
iframe_page = await self.context.new_page()
try:
await iframe_page.goto(iframe_url, wait_until="networkidle")
await asyncio.sleep(2)
logger.info("[DEGEWO] Loaded Wohnungshelden application page")
# TODO: Implement form-filling and submission logic here
finally:
await iframe_page.close()
else:
# No iframe found - try the old approach (fallback for different page structure)
logger.warning("[DEGEWO] Wohnungshelden iframe not found, trying direct form...")
# TODO: Implement fallback logic here
2025-12-27 11:59:04 +01:00
else:
2025-12-29 22:46:10 +01:00
result["message"] = "No kontaktieren button found"
logger.warning("[DEGEWO] Could not find kontaktieren button")
screenshot_path = DATA_DIR / f"degewo_nobtn_{listing['id']}.png"
await page.screenshot(path=str(screenshot_path), full_page=True)
await page.close()
return result
2025-12-27 11:59:04 +01:00
except Exception as e:
2025-12-29 22:46:10 +01:00
result["success"] = False
result["message"] = f"Error: {str(e)}"
logger.error(f"[DEGEWO] Exception: {str(e)}")
2025-12-27 11:59:04 +01:00
await page.close()
2025-12-29 22:46:10 +01:00
return result