lazyflat/alert/web_client.py
EiSiMo 3bb04210c4 secrets tab, drop commute filter, favicon, robust error reports
1. Admin → Geheimnisse sub-tab lets you edit ANTHROPIC_API_KEY +
   BERLIN_WOHNEN_USERNAME/PASSWORD at runtime. Migration v7 adds a
   secrets(key,value,updated_at) table; startup seeds missing keys from
   env (idempotent). web reads secrets DB-first (env fallback) via
   llm._api_key(); alert fetches them from web /internal/secrets on each
   scan, passes them into Scraper(). Rotating creds no longer needs a
   redeploy.
   Masked display: 6 leading + 4 trailing chars, "…" in the middle.
   Blank form fields leave the stored value untouched.

2. Drop the max_morning_commute filter from UI + server + FILTER_KEYS +
   filter summary (the underlying Maps.calculate_score code stays for
   potential future re-enable).

3. /static/didi.webp wired as favicon via <link rel="icon"> in base.html.

4. apply.open_page wraps page.goto in try/except so a failed load still
   produces a "goto.failed" step + screenshot instead of returning an
   empty forensics blob. networkidle + post-submission sleep are also
   made best-effort. The error ZIP export already writes screenshot+HTML
   per step and final_html — with this change every apply run leaves a
   reconstructable trail even when the listing is already offline.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 17:56:57 +02:00

54 lines
1.6 KiB
Python

import logging
import requests
from settings import WEB_URL, INTERNAL_API_KEY
logger = logging.getLogger("alert")
class WebClient:
def __init__(self, base_url: str = WEB_URL):
self.base_url = base_url.rstrip("/")
self.headers = {"X-Internal-Api-Key": INTERNAL_API_KEY}
def submit_flat(self, flat_payload: dict) -> bool:
try:
r = requests.post(
f"{self.base_url}/internal/flats",
json=flat_payload,
headers=self.headers,
timeout=10,
)
if r.status_code >= 400:
logger.error(f"web rejected flat: {r.status_code} {r.text[:300]}")
return False
return True
except requests.RequestException as e:
logger.error(f"web unreachable: {e}")
return False
def heartbeat(self) -> None:
try:
requests.post(
f"{self.base_url}/internal/heartbeat",
json={"service": "alert"},
headers=self.headers,
timeout=5,
)
except requests.RequestException:
pass
def fetch_secrets(self) -> dict:
"""Pull the current runtime secrets dict from web. Empty on failure
— caller falls back to env values."""
try:
r = requests.get(
f"{self.base_url}/internal/secrets",
headers=self.headers,
timeout=5,
)
if r.ok:
return r.json() or {}
except requests.RequestException as e:
logger.warning(f"secrets fetch failed: {e}")
return {}