gewobag fix
This commit is contained in:
parent
cc40121e46
commit
4ea437e3e6
4 changed files with 312 additions and 124 deletions
|
|
@ -1,6 +1,7 @@
|
|||
from .base_handler import BaseHandler
|
||||
import logging
|
||||
import asyncio
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
|
@ -22,33 +23,47 @@ class GesobauHandler(BaseHandler):
|
|||
# 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"[GESOBAU] Listing is down (404): {listing['link']}")
|
||||
page_content = await page.content()
|
||||
is_404 = (
|
||||
status == 404 or
|
||||
(page_title and "404" in page_title) or
|
||||
(page_title and "nicht gefunden" in page_title.lower()) or
|
||||
("Angebot nicht mehr verfügbar" in page_content)
|
||||
)
|
||||
if is_404:
|
||||
logger.warning(f"[GESOBAU] Listing is down (404 or unavailable): {listing['link']}")
|
||||
result["success"] = False
|
||||
result["message"] = "Listing is no longer available (404). Application impossible. Will not retry."
|
||||
result["deactivated"] = True
|
||||
await page.close()
|
||||
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 and check for deactivation
|
||||
html_content = await page.content()
|
||||
# Dismiss cookie banner
|
||||
try:
|
||||
with open(DATA_DIR / "gesobau_debug.html", "w", encoding="utf-8") as f:
|
||||
f.write(html_content)
|
||||
except Exception as e:
|
||||
logger.debug(f"[GESOBAU] Debug HTML not saved: {e}")
|
||||
cookie_btn = await page.query_selector('#CybotCookiebotDialogBodyLevelButtonLevelOptinAllowAll, button:has-text("Alle akzeptieren")')
|
||||
if cookie_btn and await cookie_btn.is_visible():
|
||||
await cookie_btn.click()
|
||||
logger.info("[GESOBAU] Dismissed cookie banner")
|
||||
await asyncio.sleep(1)
|
||||
except:
|
||||
pass
|
||||
|
||||
# Tailored 404 detection: Angebot nicht mehr verfügbar
|
||||
if "Angebot nicht mehr verfügbar" in html_content:
|
||||
logger.warning("[GESOBAU] Permanent fail: Angebot nicht mehr verfügbar")
|
||||
result["deactivated"] = True
|
||||
result["message"] = "Listing is no longer available (Angebot nicht mehr verfügbar). Marked as deactivated."
|
||||
await page.close()
|
||||
return result
|
||||
# Save debug HTML and screenshot
|
||||
try:
|
||||
html_content = await page.content()
|
||||
with open(DATA_DIR / f"gesobau_debug_{listing['id']}.html", "w", encoding="utf-8") as f:
|
||||
f.write(html_content)
|
||||
logger.info(f"[GESOBAU] Saved debug HTML: gesobau_debug_{listing['id']}.html")
|
||||
except Exception as e:
|
||||
logger.warning(f"[GESOBAU] Could not save debug HTML: {e}")
|
||||
|
||||
try:
|
||||
await page.screenshot(path=DATA_DIR / f"gesobau_page_{listing['id']}.png", full_page=True)
|
||||
logger.info(f"[GESOBAU] Saved page screenshot: gesobau_page_{listing['id']}.png")
|
||||
except Exception as e:
|
||||
logger.warning(f"[GESOBAU] Could not save screenshot: {e}")
|
||||
|
||||
# Log listing details
|
||||
await self.log_listing_details(listing)
|
||||
|
||||
# Look for application button
|
||||
logger.info("[GESOBAU] Searching for application button...")
|
||||
|
|
@ -56,6 +71,8 @@ class GesobauHandler(BaseHandler):
|
|||
'a[href*="bewerben"]',
|
||||
'button:has-text("Bewerben")',
|
||||
'a:has-text("Bewerben")',
|
||||
'button:has-text("Interesse")',
|
||||
'a:has-text("Kontakt")',
|
||||
'button.btn',
|
||||
]
|
||||
|
||||
|
|
@ -77,22 +94,25 @@ class GesobauHandler(BaseHandler):
|
|||
if apply_btn:
|
||||
await apply_btn.scroll_into_view_if_needed()
|
||||
await asyncio.sleep(0.5)
|
||||
logger.info("[GESOBAU] Clicking application button...")
|
||||
await apply_btn.click()
|
||||
await asyncio.sleep(2)
|
||||
# --- Post-click confirmation logic ---
|
||||
logger.info("[GESOBAU] Clicked application button, checking for confirmation...")
|
||||
|
||||
# Save screenshot and HTML after click
|
||||
logger.info("[GESOBAU] Checking for confirmation...")
|
||||
try:
|
||||
await page.screenshot(path="data/gesobau_after_apply.png")
|
||||
logger.info("[GESOBAU] Saved screenshot after application click.")
|
||||
await page.screenshot(path=DATA_DIR / f"gesobau_after_apply_{listing['id']}.png", full_page=True)
|
||||
logger.info(f"[GESOBAU] Saved after-apply screenshot: gesobau_after_apply_{listing['id']}.png")
|
||||
except Exception as e:
|
||||
logger.warning(f"[GESOBAU] Could not save screenshot: {e}")
|
||||
logger.warning(f"[GESOBAU] Could not save after-apply screenshot: {e}")
|
||||
|
||||
try:
|
||||
html_after = await page.content()
|
||||
with open("data/gesobau_after_apply.html", "w", encoding="utf-8") as f:
|
||||
with open(DATA_DIR / f"gesobau_after_apply_{listing['id']}.html", "w", encoding="utf-8") as f:
|
||||
f.write(html_after)
|
||||
logger.info("[GESOBAU] Saved HTML after application click.")
|
||||
logger.info(f"[GESOBAU] Saved after-apply HTML: gesobau_after_apply_{listing['id']}.html")
|
||||
except Exception as e:
|
||||
logger.warning(f"[GESOBAU] Could not save after-apply HTML: {e}")
|
||||
logger.warning(f"[GESOBAU] Could not save HTML after apply: {e}")
|
||||
|
||||
# Look for confirmation message on the page
|
||||
|
|
@ -123,6 +143,9 @@ class GesobauHandler(BaseHandler):
|
|||
else:
|
||||
logger.warning("[GESOBAU] No application button found.")
|
||||
result["message"] = "No application button found."
|
||||
screenshot_path = DATA_DIR / f"gesobau_nobtn_{listing['id']}.png"
|
||||
await page.screenshot(path=str(screenshot_path))
|
||||
logger.info(f"[GESOBAU] Saved no-button screenshot: {screenshot_path}")
|
||||
except Exception as e:
|
||||
result["message"] = f"Error during application: {e}"
|
||||
logger.error(f"[GESOBAU] Application error: {e}")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue