#!/usr/bin/env python3 # ----------------------------------------------------------------------------- # House-Ka — fusion des candidats RealtyPress reste-du-Canada (2026-08-27) # scripts/merge_canada_candidates.py : lit /tmp/rp_canada_candidates.json # (sortie du scout), filtre (volume, Québec/Ontario), et ajoute les nouveaux # sites à data/canada_agencies.json + data/sources.json. # Usage : .venv/bin/python scripts/merge_canada_candidates.py [--min-cards 10] # ----------------------------------------------------------------------------- import json import re import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent CANDIDATES = Path("/tmp/rp_canada_candidates.json") MIN_CARDS = int(sys.argv[sys.argv.index("--min-cards") + 1]) if "--min-cards" in sys.argv else 10 cands = json.loads(CANDIDATES.read_text()) reg_p = ROOT / "data" / "canada_agencies.json" src_p = ROOT / "data" / "sources.json" registry = json.loads(reg_p.read_text()) sources = json.loads(src_p.read_text()) known_domains = {re.sub(r"^https?://(www\.)?", "", e["site"]).rstrip("/") for e in registry} known_ids = {s["id"] for s in sources["sources"]} added = [] for c in sorted(cands, key=lambda x: -x.get("approx_volume_gte", 0)): dom = c["domain"] if dom in known_domains: continue provs = c.get("provinces", {}) total = sum(provs.values()) or 1 # province dominante observée sur la page 1 top = max(provs, key=provs.get) if provs else c.get("province_hint", "") # hors périmètre : sites majoritairement québécois ou ontariens (l'Ontario # est déjà couvert par les 15 sources existantes — n'ajouter que si gros) if re.search(r"qu[ée]bec", top, re.I): continue if top == "Ontario" and c.get("approx_volume_gte", 0) < 5000: continue if c.get("cards_page1", 0) < MIN_CARDS: continue slug = re.sub(r"[^a-z0-9]+", "", dom.split(".")[0])[:24] sid = f"rp_ag_{slug}" if sid in known_ids: continue pages = max(10, min(1500, c.get("approx_pages_gte", 1) * 2)) entry = { "id": sid, "name": f"{dom} ({top or 'Canada'})", "site": f"https://{dom}", "archive": c.get("archive", "listing"), "max_pages": pages, "province": top or "Ontario", "note": (f"Recensement CANADA 2026-08-27 : ~≥{c.get('approx_volume_gte', '?')} fiches, " f"provinces page 1 : {provs}. RealtyPress/DDF."), } registry.append(entry) sources["sources"].append({ "id": sid, "name": entry["name"], "url": entry["site"], "listing_url": f"{entry['site']}/{entry['archive']}/", "coverage": f"{top or 'Canada'} — ~≥{c.get('approx_volume_gte', '?')} fiches DDF", "connector": "realtypress", "status": "actif", "type": "agence", "note": "House-Ka — generic RealtyPress connector (registry data/canada_agencies.json). WordPress RealtyPress plugin on the CREA DDF feed. external_id ddf: cross-site dedup by MIN(uid).", }) known_ids.add(sid) known_domains.add(dom) added.append((sid, top, c.get("approx_volume_gte"))) reg_p.write_text(json.dumps(registry, ensure_ascii=False, indent=1)) src_p.write_text(json.dumps(sources, ensure_ascii=False, indent=2)) print(f"{len(added)} nouvelles sources :") for sid, top, vol in added: print(f" {sid:34s} {top:24s} ~≥{vol}")