wohnbot/handlers/wbm_handler.py

176 lines
7.9 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-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/wbm")
DATA_DIR.mkdir(parents=True, exist_ok=True)
2025-12-27 11:59:04 +01:00
class WBMHandler(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:
logger.info(f"[WBM] Opening listing overview page: {listing['link']}")
2025-12-27 11:59:04 +01:00
await page.goto(listing["link"], wait_until="networkidle")
logger.info("[WBM] Overview page loaded")
2025-12-27 11:59:04 +01:00
await asyncio.sleep(2)
# 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)
# 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 / "wbm_debug.html", "w", encoding="utf-8") as f:
f.write(html_content)
except Exception as e:
logger.warning(f"[WBM] Could not save debug HTML: {e}")
# 404/permanent fail detection
error_texts = [
"Keine passenden Angebote gefunden",
"Das Angebot existiert nicht mehr",
"Die gewünschte Seite konnte nicht gefunden werden",
"404",
"Es wurden keine Immobilien gefunden"
]
page_text = await page.text_content('body')
if page_text:
for err in error_texts:
if err in page_text:
2026-01-01 22:14:55 +01:00
result["deactivated"] = True
result["message"] = "Listing is no longer available (404 detected on WBM)."
logger.warning(f"[WBM] Permanent fail: {err}")
await page.close()
return result
# Find and follow the 'Details' link to the detail page
logger.info("[WBM] Looking for 'Details' link to open detail page...")
detail_link = None
detail_selectors = [
'a.btn.sign[title="Details"]',
'a.immo-button-cta[title="Details"]',
'a[title="Details"]',
]
for sel in detail_selectors:
links = await page.query_selector_all(sel)
logger.info(f"[WBM] Selector '{sel}' found {len(links)} matches for details link")
for link in links:
try:
if await link.is_visible():
detail_link = link
break
except Exception as e:
logger.warning(f"[WBM] Error checking details link visibility: {e}")
if detail_link:
break
if not detail_link:
result["message"] = "No details link found on overview page."
await page.close()
return result
# Click the details link and wait for navigation
logger.info("[WBM] Clicking details link to open detail page...")
await detail_link.click()
await page.wait_for_load_state("networkidle")
await asyncio.sleep(2)
# Save HTML of detail page for debugging
try:
html_content = await page.content()
with open("data/wbm_detail_debug.html", "w", encoding="utf-8") as f:
f.write(html_content)
except Exception as e:
logger.warning(f"[WBM] Could not save detail debug HTML: {e}")
# Look for application button on detail page
logger.info("[WBM] Looking for application button on detail page...")
2025-12-27 11:59:04 +01:00
selectors = [
2026-01-02 11:23:35 +01:00
'button:has-text("Anfrage absenden")',
'a:has-text("Anfrage absenden")',
'a[href*="expose-anfordern"]',
2025-12-27 11:59:04 +01:00
'a[href*="bewerben"]',
'a:has-text("Anfragen")',
'button:has-text("Interesse")',
'a:has-text("Bewerben")',
'button:has-text("Bewerben")',
'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.info(f"[WBM] Selector '{sel}' found {len(all_btns)} matches on detail page")
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"[WBM] Found visible application button with selector '{sel}' on detail page")
2025-12-27 11:59:04 +01:00
break
except Exception as e:
logger.warning(f"[WBM] Error checking button visibility: {e}")
if apply_btn:
break
if apply_btn:
logger.info("[WBM] Found application button, scrolling into view...")
await apply_btn.scroll_into_view_if_needed()
await asyncio.sleep(0.5)
logger.info("[WBM] Clicking application button...")
2025-12-27 11:59:04 +01:00
await apply_btn.click()
await asyncio.sleep(2)
2025-12-29 22:46:10 +01:00
# --- Post-click confirmation logic ---
logger.info("[WBM] Clicked application button, checking for confirmation...")
# Save screenshot and HTML after click
try:
await page.screenshot(path="data/wbm_after_apply.png")
logger.info("[WBM] Saved screenshot after application click.")
except Exception as e:
logger.warning(f"[WBM] Could not save screenshot: {e}")
try:
html_after = await page.content()
with open("data/wbm_after_apply.html", "w", encoding="utf-8") as f:
f.write(html_after)
logger.info("[WBM] Saved HTML after application click.")
except Exception as e:
logger.warning(f"[WBM] 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"[WBM] Found confirmation element: {sel}")
confirmed = True
break
except Exception as e:
logger.debug(f"[WBM] Error checking confirmation selector {sel}: {e}")
if confirmed:
result["success"] = True
result["message"] = "Application submitted and confirmation detected."
else:
logger.warning("[WBM] 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 on detail page."
2025-12-27 11:59:04 +01:00
except Exception as e:
result["message"] = f"Error during application: {e}"
logger.error(f"[WBM] Application error: {e}")
finally:
await page.close()
return result