mostly working shape?

This commit is contained in:
Aron Petau 2025-12-31 16:06:42 +01:00
parent 3057cda8d3
commit 540a3cc884
10 changed files with 462 additions and 183 deletions

View file

@ -12,6 +12,7 @@ class DegewoHandler(BaseHandler):
self.context = browser_context
async def apply(self, listing: dict, result: dict) -> dict:
import os
DATA_DIR = Path("data/degewo")
DATA_DIR.mkdir(parents=True, exist_ok=True)
page = await self.context.new_page()
@ -60,31 +61,173 @@ class DegewoHandler(BaseHandler):
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
# Screenshot and HTML for debugging
screenshot_path = DATA_DIR / f"degewo_wohnungshelden_{listing['id']}.png"
await iframe_page.screenshot(path=str(screenshot_path), full_page=True)
logger.info(f"[DEGEWO] Saved Wohnungshelden screenshot to {screenshot_path}")
html_content = await iframe_page.content()
html_path = DATA_DIR / f"degewo_wohnungshelden_{listing['id']}.html"
with open(html_path, 'w', encoding='utf-8') as f:
f.write(html_content)
logger.info(f"[DEGEWO] Saved HTML to {html_path}")
# Fill out Wohnungshelden form
form_filled = False
# Anrede (Salutation)
try:
salutation_dropdown = await iframe_page.query_selector('#salutation-dropdown, ng-select[id*="salutation"]')
if salutation_dropdown:
await salutation_dropdown.click()
await asyncio.sleep(0.5)
anrede_option = await iframe_page.query_selector(f'.ng-option:has-text("{os.environ.get("FORM_ANREDE", "Herr")}")')
if anrede_option:
await anrede_option.click()
logger.info(f"[DEGEWO] Selected Anrede: {os.environ.get('FORM_ANREDE', 'Herr')}")
form_filled = True
except Exception as e:
logger.warning(f"[DEGEWO] Could not set Anrede: {e}")
# Vorname
try:
vorname_field = await iframe_page.query_selector('#firstName')
if vorname_field:
await vorname_field.fill(os.environ.get("FORM_VORNAME", "Max"))
logger.info(f"[DEGEWO] Filled Vorname: {os.environ.get('FORM_VORNAME', 'Max')}")
form_filled = True
except Exception as e:
logger.warning(f"[DEGEWO] Could not fill Vorname: {e}")
# Nachname
try:
nachname_field = await iframe_page.query_selector('#lastName')
if nachname_field:
await nachname_field.fill(os.environ.get("FORM_NACHNAME", "Mustermann"))
logger.info(f"[DEGEWO] Filled Nachname: {os.environ.get('FORM_NACHNAME', 'Mustermann')}")
form_filled = True
except Exception as e:
logger.warning(f"[DEGEWO] Could not fill Nachname: {e}")
# E-Mail
try:
email_field = await iframe_page.query_selector('#email')
if email_field:
await email_field.fill(os.environ.get("FORM_EMAIL", "test@example.com"))
logger.info(f"[DEGEWO] Filled E-Mail: {os.environ.get('FORM_EMAIL', 'test@example.com')}")
form_filled = True
except Exception as e:
logger.warning(f"[DEGEWO] Could not fill E-Mail: {e}")
# Telefonnummer
try:
tel_field = await iframe_page.query_selector('input[id*="telefonnummer"]')
if tel_field:
await tel_field.fill(os.environ.get("FORM_PHONE", "0123456789"))
logger.info(f"[DEGEWO] Filled Telefon: {os.environ.get('FORM_PHONE', '0123456789')}")
form_filled = True
except Exception as e:
logger.warning(f"[DEGEWO] Could not fill Telefon: {e}")
# Anzahl einziehende Personen
try:
personen_field = await iframe_page.query_selector('input[id*="numberPersonsTotal"]')
if personen_field:
await personen_field.fill(os.environ.get("FORM_PERSONS", "1"))
logger.info(f"[DEGEWO] Filled Anzahl Personen: {os.environ.get('FORM_PERSONS', '1')}")
form_filled = True
except Exception as e:
logger.warning(f"[DEGEWO] Could not fill Anzahl Personen: {e}")
# "Für sich selbst" dropdown
try:
selbst_dropdown = await iframe_page.query_selector('ng-select[id*="fuer_wen"]')
if selbst_dropdown:
await selbst_dropdown.click()
await asyncio.sleep(0.5)
selbst_option = await iframe_page.query_selector('.ng-option:has-text("Für mich selbst"), .ng-option:has-text("selbst")')
if selbst_option:
await selbst_option.click()
logger.info("[DEGEWO] Selected: Für mich selbst")
form_filled = True
except Exception as e:
logger.warning(f"[DEGEWO] Could not set 'Für sich selbst': {e}")
await asyncio.sleep(1)
# Take screenshot after filling form
screenshot_path = DATA_DIR / f"degewo_form_filled_{listing['id']}.png"
await iframe_page.screenshot(path=str(screenshot_path), full_page=True)
logger.info(f"[DEGEWO] Saved filled form screenshot to {screenshot_path}")
# Try to submit
try:
submit_selectors = [
'button[type="submit"]',
'input[type="submit"]',
'button:has-text("Absenden")',
'button:has-text("Senden")',
'button:has-text("Anfrage")',
'button:has-text("Bewerben")',
'button:has-text("Submit")',
'.btn-primary',
'.submit-btn',
]
submit_btn = None
for selector in submit_selectors:
submit_btn = await iframe_page.query_selector(selector)
if submit_btn and await submit_btn.is_visible():
logger.info(f"[DEGEWO] Found submit button with selector: {selector}")
break
submit_btn = None
if submit_btn:
await submit_btn.click()
logger.info("[DEGEWO] Clicked submit button")
await asyncio.sleep(3)
# Take screenshot after submission
screenshot_path = DATA_DIR / f"degewo_submitted_{listing['id']}.png"
await iframe_page.screenshot(path=str(screenshot_path), full_page=True)
logger.info(f"[DEGEWO] Saved submission screenshot to {screenshot_path}")
result["success"] = True
result["message"] = "Application submitted via Wohnungshelden"
else:
result["success"] = False
result["message"] = "Wohnungshelden form loaded but submit button not found"
logger.warning("[DEGEWO] Submit button not found in Wohnungshelden form")
except Exception as e:
result["success"] = False
result["message"] = f"Wohnungshelden submit error: {str(e)}"
logger.warning(f"[DEGEWO] Submit error: {e}")
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
screenshot_path = DATA_DIR / f"degewo_noiframe_{listing['id']}.png"
await page.screenshot(path=str(screenshot_path), full_page=True)
html_content = await page.content()
html_path = DATA_DIR / "degewo_debug.html"
with open(html_path, 'w', encoding='utf-8') as f:
f.write(html_content)
result["success"] = False
result["message"] = "Wohnungshelden iframe not found on page"
else:
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
except Exception as e: