Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project : Immo·Ka9# File : immoka/mortgage/providers/national_bank.py10# Module : immoka11# Repository : https://git.spboucher.ai/immo-ka12# Created : 2026-08-2413# Last Modified : 2026-08-2414#15# =============================================================================16# AUTHOR & PROFESSIONAL AFFILIATION17# =============================================================================18#19# Author : Simon-Pierre Boucher20# Position : Professor of Finance21# Institution : Université du Québec en Outaouais (UQO)22# Department : Département des sciences administratives23# Organization : SP Boucher24#25# Website : https://spboucher.ai26# Git : https://git.spboucher.ai27#28# =============================================================================29# OFFICIAL CONTACT DIRECTORY30# =============================================================================31#32# General : info@spboucher.ai33# Professional : contact@spboucher.ai34# Direct : simon@spboucher.ai35# Research : research@spboucher.ai36# Development : dev@spboucher.ai37# Support : support@spboucher.ai38# Security/Admin : admin@spboucher.ai39# Notifications : notifications@spboucher.ai40#41# Preferred Contact: contact@spboucher.ai42#43# =============================================================================44# COPYRIGHT & INTELLECTUAL PROPERTY45# =============================================================================46#47# Copyright (c) 202648# Simon-Pierre Boucher49#50# All rights reserved where applicable.51#52# This file is governed by the applicable project license,53# copyright notices, institutional agreements and relevant law.54#55# Unless expressly authorized by the applicable license or56# rights holder, copying, redistribution, sublicensing,57# publication and commercial exploitation are not permitted.58#59# Existing third-party rights and notices remain unaffected.60#61# =============================================================================62# CONFIDENTIALITY & SECURITY63# =============================================================================64#65# Classification : Public66# Distribution : Public repository; use is governed by the project license67# Security Contact : admin@spboucher.ai68#69# Access to this source does not independently grant permission70# to redistribute or disclose confidential materials.71#72# Do not embed credentials, private keys, access tokens,73# certificates or confidential personal information.74#75# =============================================================================76# LICENSE & MAINTENANCE77# =============================================================================78#79# License : None specified (no license file in this repository)80# Maintainer : Simon-Pierre Boucher81# Technical Contact: dev@spboucher.ai82#83# =============================================================================84# END OF SP BOUCHER SOURCE HEADER85# =============================================================================8687# -----------------------------------------------------------------------------88# Immo-Ka — Agrégateur de maisons à vendre (province de Québec)89# Auteur : Simon-Pierre Boucher — contact@spboucher.ai90# mortgage/providers/national_bank.py : Banque Nationale — JSON double-échappé91# setProductMap(JSON.parse("…")) dans la page des taux. c1 = produits92# hypothécaires (par productConditionName). ⚠️ Séparateurs décimaux mixtes :93# affichés « 6.090 » (point), promos « 4,84 » (virgule) — les tacNans sont94# les APR des promos. Le tauxBase du produit variable est un ÉCART, pas la95# prime : la prime vient du tauxBase du produit fixe.96# -----------------------------------------------------------------------------97from __future__ import annotations9899import json100import re101102from .base import RateProvider103104PAGE_URL = "https://www.nbc.ca/personal/mortgages/rates.html"105106MAP_RX = re.compile(r'setProductMap\(JSON\.parse\("(.*?)"\)\)', re.S)107108# champs affichés fixes (fermés) -> terme mois ; taux1moisF exclu (< 3 mois)109FIXED_POSTED = {110 "taux3moisF": 3, "taux6moisF": 6, "taux1anF": 12, "taux2ansF": 24,111 "taux3ansF": 36, "taux4ansF": 48, "taux5ansF": 60, "taux6ansF": 72,112 "taux7ansF": 84, "taux10ansF": 120,113}114FIXED_OPEN = {"taux6moisO": 6, "taux1anO": 12}115# promo -> (terme mois, champ APR)116FIXED_PROMO = {117 "tauxPromo3ansF": (36, "tac3ans"),118 "tauxPromo4ansF": (48, "tac4ans"),119 "tauxPromo5ansF": (60, "tac5ans"),120}121122123def _num(v) -> float | None:124 if v in (None, ""):125 return None126 try:127 return float(str(v).replace("\xa0", "").replace(",", ".").strip())128 except ValueError:129 return None130131132class NationalBankProvider(RateProvider):133 provider_id = "national_bank"134 institution = "Banque Nationale"135 source_url = PAGE_URL136 request_delay = 1.5137138 def fetch(self) -> list[dict]:139 return self.parse(self.get(self.source_url).text)140141 def parse(self, payload: str) -> list[dict]:142 m = MAP_RX.search(payload)143 if not m:144 raise ValueError("setProductMap introuvable (structure changée)")145 dec = (m.group(1).replace("\\x22", '"')146 .replace("\\\\", "\\").replace("\\/", "/"))147 # la chaîne JS se termine par «"), true);» : raw_decode ignore la suite148 obj, _ = json.JSONDecoder().raw_decode(dec)149 products = json.loads(obj.get("c1") or "[]")150 by_name = {p.get("productConditionName"): p151 for p in products if isinstance(p, dict)}152 out: list[dict] = []153 fixed = by_name.get("Mortgage fixed rate") or {}154 for field, term in FIXED_POSTED.items():155 rate = _num(fixed.get(field))156 if rate and rate > 0:157 out.append(self.make_product(158 rate=rate, rate_type="fixed", term_months=term,159 kind="posted",160 product_name=f"Fixe fermé {_label(term)}",161 raw={"field": field, "value": fixed.get(field)}))162 for field, term in FIXED_OPEN.items():163 rate = _num(fixed.get(field))164 if rate and rate > 0:165 out.append(self.make_product(166 rate=rate, rate_type="fixed", term_months=term,167 kind="posted",168 product_name=f"Fixe ouvert {_label(term)}",169 raw={"field": field, "value": fixed.get(field)}))170 for field, (term, apr_field) in FIXED_PROMO.items():171 rate = _num(fixed.get(field))172 if rate and rate > 0:173 out.append(self.make_product(174 rate=rate, rate_type="fixed", term_months=term,175 kind="special", apr=_num(fixed.get(apr_field)),176 product_name=f"Fixe fermé {_label(term)} (promotion)",177 raw={"field": field, "value": fixed.get(field)}))178 prime = _num(fixed.get("tauxBase"))179 if prime and prime > 0:180 out.append(self.make_product(181 rate=prime, rate_type="other", term_months=12, kind="posted",182 product_name="Taux préférentiel BNC", purpose="unknown",183 raw={"field": "tauxBase", "value": fixed.get("tauxBase")}))184 variable = by_name.get("Mortgage variable rate") or {}185 v_posted = _num(variable.get("taux5ansO"))186 if v_posted and v_posted > 0:187 out.append(self.make_product(188 rate=v_posted, rate_type="variable", term_months=60,189 kind="posted", product_name="Variable 5 ans",190 conditions="Base = taux préférentiel BNC",191 raw={"field": "taux5ansO", "value": variable.get("taux5ansO")}))192 v_promo = _num(variable.get("tauxPromo5ansF"))193 if v_promo and v_promo > 0:194 out.append(self.make_product(195 rate=v_promo, rate_type="variable", term_months=60,196 kind="special", apr=_num(variable.get("tac5ans")),197 product_name="Variable 5 ans (promotion)",198 raw={"field": "tauxPromo5ansF",199 "value": variable.get("tauxPromo5ansF")}))200 capped = by_name.get("Variable capped-rate mortgage") or {}201 c_rate = _num(capped.get("taux5ansO"))202 if c_rate and c_rate > 0:203 cap = _num(capped.get("tauxPlafond"))204 out.append(self.make_product(205 rate=c_rate, rate_type="variable", term_months=60,206 kind="posted", product_name="Variable plafonné 5 ans",207 conditions=f"Taux plafond {cap} %" if cap else None,208 raw={"field": "taux5ansO", "value": capped.get("taux5ansO"),209 "tauxPlafond": capped.get("tauxPlafond")}))210 return out211212213def _label(months: int) -> str:214 if months < 12:215 return f"{months} mois"216 years = months // 12217 return f"{years} an" if years == 1 else f"{years} ans"218