SPB Git

Repository Public

first_national.py 7.8 KB 184 lines python

1# =============================================================================2#                  SP BOUCHER | SOFTWARE & RESEARCH3#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project          : Immo·Ka9# File             : immoka/mortgage/providers/first_national.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/first_national.py : First National — table HTML statique91#   (server-side, aucun anti-bot). Taux fixes fermés par catégorie92#   assuré/assurable(LTV)/conventionnel, ARM « Prime - X% », prime maison.93# -----------------------------------------------------------------------------94from __future__ import annotations9596import re9798from bs4 import BeautifulSoup99100from .base import RateProvider, parse_rate, term_to_months101102103def _insured_status(category: str) -> str:104    c = category.lower()105    if c.startswith("insured"):106        return "insured"107    if c.startswith("insurable"):108        return "insurable"109    if c.startswith("conventional"):110        return "uninsured"111    return "unknown"112113114class FirstNationalProvider(RateProvider):115    provider_id = "first_national"116    institution = "First National"117    source_url = "https://www.firstnational.ca/residential/mortgage-rates"118    request_delay = 1.0119120    def fetch(self) -> list[dict]:121        return self.parse(self.get(self.source_url).text)122123    def parse(self, payload: str) -> list[dict]:124        soup = BeautifulSoup(payload, "html.parser")125        out: list[dict] = []126        prime = None127        m = re.search(r"First National Prime Rate:\s*(?:</?\w+[^>]*>\s*)*([\d.]+)",128                      payload)129        if m:130            prime = float(m.group(1))131        # -- taux fixes fermés (table avec aria-label par terme) --------------132        for h3 in soup.find_all("h3"):133            title = h3.get_text(" ", strip=True)134            table = h3.find_next("table")135            if table is None:136                continue137            if title.lower().startswith("fixed rate mortgages"):138                for tr in table.select("tbody tr"):139                    th = tr.find("th")140                    if th is None:141                        continue142                    category = re.sub(r"\s+", " ", th.get_text(" ", strip=True))143                    status = _insured_status(category)144                    for td in tr.find_all("td"):145                        term = term_to_months(td.get("aria-label") or "")146                        rate = parse_rate(td.get_text(strip=True))147                        if term is None or rate is None:148                            continue  # cellule N/A : produit non offert149                        out.append(self.make_product(150                            rate=rate, rate_type="fixed", term_months=term,151                            kind="posted",152                            product_name=f"Fixe fermé — {category}",153                            insured_status=status, conditions=category,154                            raw={"category": category,155                                 "cell": td.get_text(strip=True)}))156            elif title.lower().startswith("adjustable rate"):157                if prime is None:158                    continue  # sans prime confirmée, ne rien deviner159                for tr in table.select("tbody tr"):160                    th = tr.find("th")161                    td = tr.find("td")162                    if th is None or td is None:163                        continue164                    category = re.sub(r"\s+", " ", th.get_text(" ", strip=True))165                    cell = td.get_text(" ", strip=True)166                    dm = re.search(r"Prime\s*([+-])\s*([\d.]+)\s*%", cell)167                    if not dm:168                        continue169                    delta = float(dm.group(2)) * (1 if dm.group(1) == "+" else -1)170                    out.append(self.make_product(171                        rate=round(prime + delta, 2), rate_type="adjustable",172                        term_months=60, kind="special",173                        product_name=f"ARM 5 ans — {category}",174                        insured_status=_insured_status(category),175                        conditions=f"{cell} (prime First National {prime} %)",176                        raw={"category": category, "cell": cell,177                             "prime": prime, "delta": delta}))178        if prime is not None:179            out.append(self.make_product(180                rate=prime, rate_type="other", term_months=12, kind="posted",181                product_name="Taux préférentiel First National",182                purpose="unknown", raw={"prime": prime}))183        return out184
Files main
Menu

Theme