# ----------------------------------------------------------------------------- # Immo-Ka — Agrégateur de maisons à vendre (province de Québec) # Auteur : Simon-Pierre Boucher — contact@spboucher.ai # connectors/century21_agences.py : Century 21 par SOUS-AGENCES (robuste) # # Le portail central c21.ca délègue à une iframe moxiworks verrouillée # (503/WAF, même via Firecrawl). Mais les bureaux Century 21 du Québec tournent # sur la plateforme source.immo (ID-3 Technologies) : quebec.c21.ca, # excel.c21.ca et century21immoplus.com partagent un même compte # « Century 21 - All Quebec » (inventaire provincial complet), et # vision.c21.ca a son propre compte. L'API publique (api-v1.source.immo) # est paginée par st/nt (voir sourceimmo.py) — inventaire complet accessible. # # Une classe par sous-agence est générée depuis data/century21_agencies.json # (source_id « c21_ag_ »). Le n° Centris sert de clé de déduplication # (voir web.py, sources « %_ag_% ») — aucun double-comptage entre les skins # partageant le même compte ni avec le reste du site. # ----------------------------------------------------------------------------- from __future__ import annotations import json import re from pathlib import Path from .sourceimmo import SourceImmoConnector from ..schema import PropertyListing REGISTRY = Path(__file__).resolve().parent.parent.parent / "data" / "century21_agencies.json" class Century21AgenceConnector(SourceImmoConnector): """Sous-agence Century 21 sur source.immo. Les sous-classes fournissent account_id / api_key / app_id / view_id / site_url / agency_name.""" agency_name = "Century 21" def fetch(self) -> list[PropertyListing]: # auto-guérison : recharger les identifiants depuis la config publique # du site (résilient si source.immo fait tourner ses UUID). try: cfg = self.get( f"{self.site_url.rstrip('/')}/wp-content/uploads/_sourceimmo/_configs.json" ).json() self.account_id = cfg.get("account_id", self.account_id) self.api_key = cfg.get("api_key", self.api_key) self.app_id = cfg.get("app_id", self.app_id) self.view_id = cfg.get("default_view", self.view_id) except Exception: pass # on garde les valeurs du registre return super().fetch() def _to_listing(self, it: dict) -> PropertyListing | None: lst = super()._to_listing(it) if lst is not None: lst.broker_name = self.agency_name ref = it.get("ref_number") or it.get("id") if ref and self.site_url: lst.url = f"{self.site_url.rstrip('/')}/propriete/{ref}" return lst def _load() -> list[dict]: try: return json.loads(REGISTRY.read_text(encoding="utf-8")) except Exception: return [] def _slug(banner: str) -> str: return "c21_ag_" + re.sub(r"[^a-z0-9]+", "_", banner.lower()).strip("_") for _ag in _load(): if not all(_ag.get(k) for k in ("domain", "account", "api", "app", "view")): continue _sid = _slug(_ag["banner"]) _name = "Century 21 " + _ag["banner"].replace("-", " ").title() globals()[_sid.upper()] = type( "".join(p.title() for p in _sid.split("_")), (Century21AgenceConnector,), { "source_id": _sid, "account_id": _ag["account"], "api_key": _ag["api"], "app_id": _ag["app"], "view_id": _ag["view"], "site_url": f"https://{_ag['domain']}", "agency_name": _name, }, )