lazyflat/alert/main.py
EiSiMo ebb11178e7 chore: sweep dead code across all three services
Per review §1 — verified no callers before each deletion:

- _next_scrape_utc (context dict key never read by any template)
- ALERT_SCRAPE_INTERVAL_SECONDS settings import (only _next_scrape_utc read it)
- alert/paths.py (imported by nothing)
- alert/settings.py LANGUAGE (alert doesn't use translations.toml)
- alert/main.py: the vestigial `c = {}` connectivity dict, the comment
  about re-enabling it, and the entire connectivity block in
  _flat_payload — the web-side columns stay NULL on insert now
- alert/maps.py: DESTINATIONS, calculate_score, _get_next_weekday,
  _calculate_transfers (only geocode is used in the scraper)
- alert/flat.py: connectivity + display_address properties,
  _connectivity field, unused datetime import
- apply/utils.py str_to_preview (no callers) — file removed
- web/matching.py: max_morning_commute + commute check
- web/app.py: don't pass connectivity dict into flat_matches_filter,
  don't write email_address through update_notifications
- web/db.py: get_error (no callers); drop kill_switch,
  max_morning_commute, email_address from their allowed-sets so they're
  not writable through update_* anymore
- web/settings.py + docker-compose.yml: SMTP_HOST/PORT/USERNAME/PASSWORD/
  FROM/STARTTLS (notifications.py is telegram-only now)

DB columns themselves (kill_switch, email_address, max_morning_commute,
connectivity_morning_time, connectivity_night_time) stay in the schema
— SQLite can't drop them cheaply and they're harmless.

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

102 lines
3.2 KiB
Python

import logging
import time
from rich.console import Console
from rich.logging import RichHandler
from flat import Flat
from scraper import Scraper
from settings import TIME_INTERVALL
from utils import hash_any_object
from web_client import WebClient
def setup_logging():
logging.basicConfig(
level=logging.INFO,
format="%(message)s",
datefmt="[%X]",
handlers=[RichHandler(markup=True, console=Console(width=110))],
)
logging.getLogger("googlemaps").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
logger = logging.getLogger("alert")
setup_logging()
class FlatAlerter:
def __init__(self):
self.web = WebClient()
self.last_response_hash = ""
def _flat_payload(self, flat: Flat) -> dict:
lat, lng = flat.coords
return {
"id": flat.id,
"link": flat.link,
"address": flat.address,
"rooms": flat.rooms,
"size": flat.size,
"cold_rent": flat.cold_rent,
"utilities": flat.utilities,
"total_rent": flat.total_rent,
"sqm_price": flat.sqm_price,
"available_from": flat.available_from,
"published_on": flat.published_on,
"wbs": flat.wbs,
"floor": flat.floor,
"bathrooms": flat.bathrooms,
"year_built": flat.year_built,
"heating": flat.heating,
"energy_carrier": flat.energy_carrier,
"energy_value": flat.energy_value,
"energy_certificate": flat.energy_certificate,
"address_link_gmaps": flat.address_link_gmaps,
"lat": lat,
"lng": lng,
"raw_data": flat.raw_data,
}
def scan(self):
logger.info("starting scan")
# Pull fresh creds from web each scan so admin edits take effect
# without a redeploy.
secrets = self.web.fetch_secrets()
scraper = Scraper(
username=secrets.get("BERLIN_WOHNEN_USERNAME", ""),
password=secrets.get("BERLIN_WOHNEN_PASSWORD", ""),
)
if not scraper.login():
return
flats_data = scraper.get_flats()
response_hashed = hash_any_object(flats_data)
if response_hashed == self.last_response_hash:
logger.info("no change since last scan")
return
self.last_response_hash = response_hashed
for number, data in enumerate(flats_data, 1):
flat = Flat(data)
logger.info(f"{str(number).rjust(2)}: submitting {flat}")
payload = self._flat_payload(flat)
if not self.web.submit_flat(payload):
logger.warning(f"\tcould not submit {flat.id} to web, will retry next loop")
logger.info("scan finished")
if __name__ == "__main__":
logger.info("starting wohnungsdidi alert service")
alerter = FlatAlerter()
while True:
try:
alerter.scan()
alerter.web.heartbeat()
logger.info(f"sleeping for {TIME_INTERVALL} seconds")
time.sleep(TIME_INTERVALL)
except KeyboardInterrupt:
break
except Exception:
logger.exception("unexpected error")
time.sleep(60)