Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project : Immo·Ka9# File : immoka/mortgage/providers/rbc.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/rbc.py : RBC — API JSON publique publicrates (celle que91# consomme leur propre page de taux, 1 requête par code de taux).92# ⚠️ Les produits variables sont publiés en ÉCART vs prime (ex. -0.500) :93# reconstitués avec le code prime, jamais interprétés comme des taux.94# -----------------------------------------------------------------------------95from __future__ import annotations9697import json9899from .base import RateProvider100101API = ("https://apps.royalbank.com/apps/app-services/public-rates/api/"102 "publicrates?code={code}")103PAGE_URL = "https://www.rbcroyalbank.com/mortgages/mortgage-rates.html"104105PRIME_CODE = "0006470002"106107# code -> (rate_type, terme mois, kind, insured_status, nom, code APR|None)108CODE_MAP: dict[str, tuple] = {109 "0006340010": ("fixed", 6, "posted", "unknown", "Fixe 6 mois convertible", None),110 "0006340013": ("fixed", 12, "posted", "unknown", "Fixe fermé 1 an", None),111 "0006340016": ("fixed", 24, "posted", "unknown", "Fixe fermé 2 ans", None),112 "0006340019": ("fixed", 36, "posted", "unknown", "Fixe fermé 3 ans", None),113 "0006340022": ("fixed", 48, "posted", "unknown", "Fixe fermé 4 ans", None),114 "0006340025": ("fixed", 60, "posted", "unknown", "Fixe fermé 5 ans", None),115 "0006340042": ("fixed", 84, "posted", "unknown", "Fixe fermé 7 ans", None),116 "0006340030": ("fixed", 120, "posted", "unknown", "Fixe fermé 10 ans", None),117 "0386080056": ("fixed", 12, "special", "unknown", "Fixe fermé 1 an (offre spéciale)", "0386140074"),118 "0386080057": ("fixed", 24, "special", "unknown", "Fixe fermé 2 ans (offre spéciale)", "0386140075"),119 "0386080058": ("fixed", 36, "special", "unknown", "Fixe fermé 3 ans (offre spéciale)", "0386140076"),120 "0386080059": ("fixed", 48, "special", "unknown", "Fixe fermé 4 ans (offre spéciale)", "0386140077"),121 "0386080060": ("fixed", 60, "special", "unknown", "Fixe fermé 5 ans (offre spéciale)", "0386140078"),122 "0386080061": ("fixed", 84, "special", "unknown", "Fixe fermé 7 ans (offre spéciale)", "0386140079"),123 "0385940006": ("fixed", 60, "special", "insured", "Fixe fermé 5 ans (ratio élevé)", "0386090006"),124}125# codes variables : ÉCART vs prime -> taux = prime + valeur126SPREAD_MAP: dict[str, tuple] = {127 "0236440012": ("variable", 60, "posted", "unknown", "Variable fermé 5 ans", "0166950009"),128 "0386080070": ("variable", 60, "special", "unknown", "Variable fermé 5 ans (offre spéciale)", "0386140088"),129 "0385940016": ("variable", 60, "special", "insured", "Variable fermé 5 ans (ratio élevé)", "0386090016"),130}131132133def _value(responses: dict, code: str) -> float | None:134 resp = responses.get(code)135 if not isinstance(resp, dict):136 return None137 content = resp.get("result_content") or {}138 try:139 return float(content.get("Value"))140 except (TypeError, ValueError):141 return None142143144class RbcProvider(RateProvider):145 provider_id = "rbc"146 institution = "RBC Banque Royale"147 source_url = PAGE_URL148 request_delay = 0.6149150 def _codes(self) -> list[str]:151 codes = [PRIME_CODE] + list(CODE_MAP) + list(SPREAD_MAP)152 codes += [apr for *_, apr in CODE_MAP.values() if apr]153 codes += [apr for *_, apr in SPREAD_MAP.values() if apr]154 return codes155156 def fetch(self) -> list[dict]:157 responses: dict[str, dict] = {}158 for code in self._codes():159 try:160 responses[code] = json.loads(self.get(API.format(code=code)).text)161 except Exception: # noqa: BLE001 — un code raté n'annule pas le reste162 continue163 return self.parse_codes(responses)164165 def parse(self, payload: str) -> list[dict]:166 """Pour les tests fixtures : payload = JSON {code: réponse API}."""167 data = json.loads(payload)168 return self.parse_codes(data.get("responses") or data)169170 def parse_codes(self, responses: dict) -> list[dict]:171 out: list[dict] = []172 prime = _value(responses, PRIME_CODE)173 for code, (rtype, term, kind, insured, name, apr_code) in CODE_MAP.items():174 rate = _value(responses, code)175 if rate is None or rate <= 0:176 continue177 apr = _value(responses, apr_code) if apr_code else None178 out.append(self.make_product(179 rate=rate, rate_type=rtype, term_months=term, kind=kind,180 product_name=name, apr=apr, insured_status=insured,181 raw={"code": code, "value": rate}))182 if prime is not None:183 for code, (rtype, term, kind, insured, name, apr_code) in SPREAD_MAP.items():184 spread = _value(responses, code)185 if spread is None or abs(spread) > 3:186 continue # un écart vs prime > 3 pts : structure changée187 apr = _value(responses, apr_code) if apr_code else None188 out.append(self.make_product(189 rate=round(prime + spread, 3), rate_type=rtype,190 term_months=term, kind=kind, product_name=name, apr=apr,191 insured_status=insured,192 conditions=f"Prime RBC {prime} % {spread:+} pt",193 raw={"code": code, "spread": spread, "prime": prime}))194 out.append(self.make_product(195 rate=prime, rate_type="other", term_months=12, kind="posted",196 product_name="Taux préférentiel RBC", purpose="unknown",197 raw={"code": PRIME_CODE, "value": prime}))198 return out199