SPB Git forge

spb/house-ka

Public
18commits 1branches 0releases
1.9 MBsize
maindefault branch
19 days agolast push
Python 67% TypeScript 18.2% CSS 14.4%
17.1 KB · 405 lines python
Raw Blame History
1# -----------------------------------------------------------------------------2# House-Ka — Agrégateur de maisons à vendre (Canada hors Québec)3# Auteur : Simon-Pierre Boucher — contact@spboucher.ai4# commerces.py : grands commerces à proximité — API Mapbox Search Box,5#   bannières ADAPTÉES À CHAQUE PROVINCE.6#7#   Pour chaque grande bannière du panier provincial, on interroge l'API8#   Search Box de Mapbox (jeton PUBLIC pk.… lu dans9#   frontend/src/kamaps/config.ts) avec la position de l'annonce en10#   `proximity`, et on retient le point de vente le plus proche. Cache par11#   cellule d'environ 1 km (data/commerces.db, TTL 30 jours).12#13#   Paniers par province (2026-08) — fondés sur la présence réelle des14#   chaînes : Loblaw n°1 national (Superstore/No Frills, Zehrs ON, Dominion NL),15#   Sobeys n°2 (dominant en Atlantique : Sobeys/Foodland/Lawtons), Metro n°316#   (Ontario+Québec, Food Basics), Pattison/Save-On-Foods ~180 magasins dans17#   l'Ouest + Yukon, Co-op (FCL) ~300 magasins en Saskatchewan, monopoles18#   d'alcool provinciaux (LCBO, BC Liquor, Liquor Mart MB, NB Liquor, NSLC),19#   Kent (Irving) en quincaillerie atlantique, London Drugs (BC/AB),20#   Colemans (chaîne terre-neuvienne).21# -----------------------------------------------------------------------------22from __future__ import annotations2324import json25import math26import re27import sqlite328import time29import urllib.parse30import urllib.request31from concurrent.futures import ThreadPoolExecutor32from pathlib import Path3334ROOT = Path(__file__).resolve().parent.parent35DB_PATH = ROOT / "data" / "commerces.db"36UA = "HouseKaBot/1.0 (+https://www.house-ka.com; contact@spboucher.ai)"37TTL = 30 * 8640038API = "https://api.mapbox.com/search/searchbox/v1/forward"3940# id -> (libellé, requête Mapbox, mot-clé de validation — le nom du POI doit le41# contenir, sans accents ni casse ; écarte « Station Métro », « Super Qualité »…)42BRAND_DEFS = {43    # nationaux44    "costco": ("Costco", "Costco Wholesale", "costco"),45    "walmart": ("Walmart", "Walmart Supercentre", "walmart"),46    "canadiantire": ("Canadian Tire", "Canadian Tire", "canadian tire"),47    "dollarama": ("Dollarama", "Dollarama", "dollarama"),48    "homedepot": ("Home Depot", "Home Depot", "home depot"),49    "homehardware": ("Home Hardware", "Home Hardware", "home hardware"),50    "shoppers": ("Shoppers Drug Mart", "Shoppers Drug Mart", "shoppers"),51    "gianttiger": ("Giant Tiger", "Giant Tiger", "giant tiger"),52    # épicerie — Loblaw53    "loblaws": ("Loblaws", "Loblaws", "loblaws"),54    "superstore": ("Real Canadian Superstore", "Real Canadian Superstore", "superstore"),55    "atlanticsuperstore": ("Atlantic Superstore", "Atlantic Superstore", "superstore"),56    "nofrills": ("No Frills", "No Frills", "no frills"),57    "zehrs": ("Zehrs", "Zehrs", "zehrs"),58    "dominion": ("Dominion", "Dominion grocery", "dominion"),59    "independent": ("Your Independent Grocer", "Your Independent Grocer", "independent"),60    # épicerie — Empire/Sobeys61    "sobeys": ("Sobeys", "Sobeys", "sobeys"),62    "safeway": ("Safeway", "Safeway", "safeway"),63    "foodland": ("Foodland", "Foodland", "foodland"),64    "freshco": ("FreshCo", "FreshCo", "freshco"),65    # épicerie — Metro (Ontario)66    "metro": ("Metro", "Metro grocery", "metro"),67    "foodbasics": ("Food Basics", "Food Basics", "food basics"),68    # épicerie — Ouest / coopératives / régionales69    "saveon": ("Save-On-Foods", "Save-On-Foods", "save-on"),70    "coop": ("Co-op", "Co-op Food Store", "co-op"),71    "colemans": ("Colemans", "Colemans grocery", "colemans"),72    "iga": ("IGA", "IGA", "iga"),73    # alcool (monopoles/sociétés provinciales)74    "lcbo": ("LCBO", "LCBO", "lcbo"),75    "beerstore": ("The Beer Store", "The Beer Store", "beer store"),76    "bcliquor": ("BC Liquor", "BC Liquor Store", "liquor"),77    "liquormart": ("Liquor Mart", "Manitoba Liquor Mart", "liquor mart"),78    "nbliquor": ("NB Liquor", "NB Liquor Alcool NB", "liquor"),79    "nslc": ("NSLC", "NSLC", "nslc"),80    # pharmacies régionales81    "londondrugs": ("London Drugs", "London Drugs", "london drugs"),82    "rexall": ("Rexall", "Rexall", "rexall"),83    "lawtons": ("Lawtons", "Lawtons Drugs", "lawtons"),84    # quincaillerie85    "rona": ("RONA", "RONA", "rona"),86    "kent": ("Kent", "Kent Building Supplies", "kent"),87}8889_NATIONAL = ["costco", "walmart", "canadiantire", "dollarama", "homedepot",90             "shoppers"]9192# panier par province — les bannières LES PLUS PRÉSENTES dans chaque marché93PROVINCE_BRANDS = {94    "British Columbia": _NATIONAL + ["saveon", "superstore", "safeway",95                                     "nofrills", "iga", "londondrugs",96                                     "bcliquor", "homehardware"],97    "Alberta": _NATIONAL + ["superstore", "safeway", "saveon", "sobeys",98                            "nofrills", "coop", "rexall", "homehardware"],99    "Saskatchewan": _NATIONAL + ["coop", "superstore", "sobeys", "safeway",100                                 "nofrills", "gianttiger", "homehardware"],101    "Manitoba": _NATIONAL + ["superstore", "coop", "sobeys", "safeway",102                             "nofrills", "liquormart", "gianttiger",103                             "homehardware"],104    "Ontario": _NATIONAL + ["loblaws", "nofrills", "foodbasics", "metro",105                            "sobeys", "freshco", "zehrs", "lcbo", "beerstore",106                            "rexall", "rona"],107    "New Brunswick": _NATIONAL + ["sobeys", "atlanticsuperstore", "foodland",108                                  "nofrills", "nbliquor", "kent", "lawtons",109                                  "gianttiger"],110    "Nova Scotia": _NATIONAL + ["sobeys", "atlanticsuperstore", "foodland",111                                "nofrills", "nslc", "kent", "lawtons",112                                "gianttiger"],113    "Prince Edward Island": _NATIONAL + ["sobeys", "atlanticsuperstore",114                                         "foodland", "kent", "lawtons",115                                         "gianttiger"],116    "Newfoundland and Labrador": _NATIONAL + ["sobeys", "dominion", "colemans",117                                              "foodland", "kent", "lawtons"],118    "Yukon": _NATIONAL + ["saveon", "superstore", "independent",119                          "homehardware"],120    "Northwest Territories": _NATIONAL + ["independent", "coop",121                                          "homehardware"],122    "Nunavut": ["canadiantire", "independent", "coop", "homehardware"],123}124_DEFAULT_PROVINCE = "Ontario"125126# frontières longitudinales approximatives (repli quand la région est absente)127def _infer_province(lat: float, lng: float) -> str:128    if lat >= 60:129        if lng < -124:130            return "Yukon"131        return "Northwest Territories" if lng < -102 else "Nunavut"132    if lng < -120:133        return "British Columbia"134    if lng < -110:135        return "Alberta"136    if lng < -101.4:137        return "Saskatchewan"138    if lng < -95.15:139        return "Manitoba"140    if lng < -74.3:141        return "Ontario"142    if -64.5 <= lng <= -61.9 and 45.9 <= lat <= 47.1:143        return "Prince Edward Island"144    if lng >= -59.5 or lat >= 50.5:145        return "Newfoundland and Labrador"146    if lat < 46.05 or lng > -64.4:147        return "Nova Scotia"148    return "New Brunswick"149150151def _brands_for(region: str | None, lat: float, lng: float):152    prov = (region or "").strip()153    if prov not in PROVINCE_BRANDS:154        prov = _infer_province(lat, lng)155    ids = PROVINCE_BRANDS.get(prov, PROVINCE_BRANDS[_DEFAULT_PROVINCE])156    return [(bid, *BRAND_DEFS[bid]) for bid in ids if bid in BRAND_DEFS]157158_BAN = ("station", "stationnement", "kentucky", "kentville",159        "co-operators", "cooperators")160161162def _norm(s: str) -> str:163    import unicodedata164    s = unicodedata.normalize("NFD", s or "")165    return "".join(c for c in s if unicodedata.category(c) != "Mn").lower()166167_token_cache: list[str] = []168169170def _token() -> str:171    if not _token_cache:172        cfg = (ROOT / "frontend" / "src" / "kamaps" / "config.ts").read_text()173        m = re.search(r'"(pk\.[A-Za-z0-9._-]+)"', cfg)174        if not m:175            raise RuntimeError("jeton Mapbox introuvable (kamaps/config.ts)")176        _token_cache.append(m.group(1))177    return _token_cache[0]178179180def _connect() -> sqlite3.Connection:181    DB_PATH.parent.mkdir(parents=True, exist_ok=True)182    con = sqlite3.connect(DB_PATH, timeout=15)183    con.row_factory = sqlite3.Row184    con.execute("""CREATE TABLE IF NOT EXISTS commerces_cache (185        cellule TEXT, brand TEXT, nom TEXT, adresse TEXT,186        lat REAL, lng REAL, fetched_at REAL,187        PRIMARY KEY (cellule, brand))""")188    return con189190191def _dist_m(lat1, lng1, lat2, lng2) -> float:192    dlat = math.radians(lat2 - lat1)193    dlng = math.radians(lng2 - lng1)194    a = (math.sin(dlat / 2) ** 2 + math.cos(math.radians(lat1))195         * math.cos(math.radians(lat2)) * math.sin(dlng / 2) ** 2)196    return 6371000 * 2 * math.asin(math.sqrt(a))197198199def _fetch_brand(brand_q: str, lat: float, lng: float,200                 match: str = "") -> dict | None:201    params = urllib.parse.urlencode({202        "q": brand_q, "proximity": f"{lng},{lat}", "limit": 5,203        "types": "poi", "language": "fr", "country": "CA",204        "access_token": _token()})205    req = urllib.request.Request(f"{API}?{params}",206                                 headers={"User-Agent": UA})207    try:208        with urllib.request.urlopen(req, timeout=12) as r:209            feats = json.load(r).get("features") or []210    except Exception:211        return None212    for f in feats:213        p = f.get("properties") or {}214        nom = _norm(p.get("name") or "")215        if match and match not in nom:216            continue217        if any(b in nom for b in _BAN):218            continue219        lng2, lat2 = f["geometry"]["coordinates"][:2]220        return {"nom": p.get("name") or brand_q,221                "adresse": p.get("full_address")222                           or p.get("place_formatted") or "",223                "lat": lat2, "lng": lng2}224    return None225226227OVERPASS = ["https://overpass.kumi.systems/api/interpreter",228            "https://overpass-api.de/api/interpreter"]229230231def _fetch_transit(lat: float, lng: float) -> list[tuple[str, dict]]:232    """Station de métro et arrêt de bus les plus proches (OpenStreetMap)."""233    q = f"""[out:json][timeout:20];234(235  node["railway"="station"]["station"="subway"](around:3000,{lat},{lng});236  node["highway"="bus_stop"](around:1000,{lat},{lng});237);238out body;"""239    data = None240    for url in OVERPASS:241        try:242            req = urllib.request.Request(243                url, data=urllib.parse.urlencode({"data": q}).encode(),244                headers={"User-Agent": UA})245            with urllib.request.urlopen(req, timeout=25) as r:246                data = json.load(r)247            break248        except Exception:249            continue250    if not data:251        return []252    best: dict[str, tuple[float, dict]] = {}253    for el in data.get("elements", []):254        tags = el.get("tags") or {}255        kind = ("metro_station" if tags.get("railway") == "station"256                else "arret_bus")257        d = _dist_m(lat, lng, el["lat"], el["lon"])258        if kind not in best or d < best[kind][0]:259            best[kind] = (d, {"nom": tags.get("name")260                              or ("Station de métro" if kind == "metro_station"261                                  else "Arrêt de bus"),262                              "adresse": "", "lat": el["lat"],263                              "lng": el["lon"]})264    return [(k, v[1]) for k, v in best.items()]265266267TRANSIT = [("metro_station", "Station de métro"),268           ("rem_station", "Station REM"),269           ("arret_bus", "Arrêt de bus"),270           ("gare_train", "Gare de train")]271272_DB_GENRE = {"metro": "metro_station", "rem": "rem_station",273             "bus": "arret_bus", "train": "gare_train"}274275276def _transit_from_db(lat: float, lng: float) -> list[tuple[str, dict]]:277    """Arrêts/stations depuis data/transit.db (extrait OpenStreetMap278    pré-calculé : ~37 000 arrêts de bus, métro, REM, gares du Québec)."""279    db = ROOT / "data" / "transit.db"280    if not db.exists():281        return []282    con = sqlite3.connect(f"file:{db}?mode=ro", uri=True)283    con.row_factory = sqlite3.Row284    out = []285    for genre, rayon in (("metro", 6000), ("rem", 6000), ("bus", 1500),286                         ("train", 8000)):287        d = rayon / 111320.0288        rows = con.execute(289            "SELECT nom, lat, lng FROM arrets WHERE genre=? AND lat BETWEEN "290            "? AND ? AND lng BETWEEN ? AND ?",291            (genre, lat - d, lat + d, lng - d, lng + d)).fetchall()292        best = None293        for r in rows:294            dd = _dist_m(lat, lng, r["lat"], r["lng"])295            if dd <= rayon and (best is None or dd < best[0]):296                best = (dd, r)297        if best:298            out.append((_DB_GENRE[genre],299                        {"nom": best[1]["nom"] or "", "adresse": "",300                         "lat": best[1]["lat"], "lng": best[1]["lng"]}))301    con.close()302    return out303304_POI_CAT = {"metro": "metro_station", "bus": "arret_bus"}305306307def _transit_from_poi(lat: float, lng: float) -> list[tuple[str, dict]]:308    """Métro/bus depuis le cache POI du projet (louka.db, déjà calculé par309    immeuble) — la fiche interroge avec les mêmes coordonnées que poi.py."""310    db_main = ROOT / "data" / next(311        (n for n in ("louka.db", "immoka.db", "immo.db")312         if (ROOT / "data" / n).exists()), "louka.db")313    if not db_main.exists():314        return []315    try:316        con = sqlite3.connect(f"file:{db_main}?mode=ro", uri=True)317        con.row_factory = sqlite3.Row318        d = 300 / 111320.0319        row = con.execute(320            "SELECT pois, lat, lng FROM poi_cache WHERE lat BETWEEN ? AND ? "321            "AND lng BETWEEN ? AND ? ORDER BY (lat-?)*(lat-?)+(lng-?)*(lng-?) "322            "LIMIT 1", (lat - d, lat + d, lng - d, lng + d,323                        lat, lat, lng, lng)).fetchone()324        con.close()325    except sqlite3.Error:326        return []327    if row is None:328        return []329    out = []330    for e in json.loads(row["pois"] or "[]"):331        k = _POI_CAT.get(e.get("cat"))332        if k:333            out.append((k, {"nom": e.get("name") or "", "adresse": "",334                            "lat": lat, "lng": lng,335                            "_dist": e.get("dist_m")}))336    return out337338339def nearby(lat: float, lng: float, region: str | None = None) -> dict:340    """Grand commerce le plus proche par bannière PROVINCIALE (cache ~1 km)."""341    brands = _brands_for(region, lat, lng)342    cell = f"{round(lat, 2)},{round(lng, 2)}"343    con = _connect()344    now = time.time()345    cached = {r["brand"]: r for r in con.execute(346        "SELECT * FROM commerces_cache WHERE cellule=? AND fetched_at>?",347        (cell, now - TTL))}348    manquants = [(bid, q, m) for bid, _, q, m in brands349                 if bid not in cached]350    transit_manquant = any(k not in cached for k, _ in TRANSIT)351    if manquants or transit_manquant:352        res: list[tuple[str, dict | None]] = []353        if manquants:354            with ThreadPoolExecutor(max_workers=6) as ex:355                res = list(ex.map(356                    lambda b: (b[0], _fetch_brand(b[1], lat, lng, b[2])),357                    manquants))358        if transit_manquant:359            tr = (_transit_from_db(lat, lng)360                  or _transit_from_poi(lat, lng) or _fetch_transit(lat, lng))361            res.extend(tr)362        with con:363            for bid, hit in res:364                if hit is None:365                    continue366                con.execute(367                    "INSERT OR REPLACE INTO commerces_cache VALUES "368                    "(?,?,?,?,?,?,?)",369                    (cell, bid, hit["nom"],370                     hit.get("adresse") or (str(hit["_dist"])371                                            if hit.get("_dist") is not None372                                            else ""),373                     hit["lat"], hit["lng"], now))374        cached = {r["brand"]: r for r in con.execute(375            "SELECT * FROM commerces_cache WHERE cellule=? AND fetched_at>?",376            (cell, now - TTL))}377    con.close()378379    items = []380    transit = []381    for bid, label in TRANSIT:382        r = cached.get(bid)383        if r is not None:384            # distance : celle du cache POI si disponible (adresse numérique)385            d = (float(r["adresse"]) if (r["adresse"] or "").replace(386                 ".", "").isdigit() else _dist_m(lat, lng, r["lat"], r["lng"]))387            if d <= 5000:388                transit.append({"id": bid, "commerce": label,389                                "nom": r["nom"], "adresse": "",390                                "dist_m": round(d),391                                "lat": r["lat"], "lng": r["lng"]})392    for bid, label, _q, _m in brands:393        r = cached.get(bid)394        if r is None:395            continue396        d = _dist_m(lat, lng, r["lat"], r["lng"])397        if d > 40000:                 # au-delà de 40 km : non pertinent398            continue399        items.append({"id": bid, "commerce": label, "nom": r["nom"],400                      "adresse": r["adresse"], "dist_m": round(d),401                      "lat": r["lat"], "lng": r["lng"]})402    items.sort(key=lambda x: x["dist_m"])403    transit.sort(key=lambda x: x["dist_m"])404    return {"n": len(items), "commerces": items, "transit": transit}405