Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project : Lou·Ka9# File : louka/connectors/sofia.py10# Module : louka11# Repository : https://git.spboucher.ai/lou-ka12# Created : 2026-08-2513# Last Modified : 2026-08-2514#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# Lou-Ka — Agrégateur de logements à louer (province de Québec)89# Auteur : Simon-Pierre Boucher — contact@spboucher.ai90# connectors/sofia.py : connecteur Le Sofia — Groupe Lacombe91# (sofiavarennes.com — 48 condos locatifs au 1845, rue du Souvenir,92# Varennes, structure béton, face au fleuve). Site PHP/Perch CMS rendu93# serveur : la page plans.php contient le tableau COMPLET des unités94# (numéro, pi², chambres, modèle 3½/4½, plan JPG, loyer, disponibilité).95# Seules les lignes « Date -> OUI » (badge Disponible) sont retenues —96# les autres n'affichent ni loyer ni date. Granularité = UNITÉ.97# external_id = numéro d'unité (stable, adresse unique).98# -----------------------------------------------------------------------------99from __future__ import annotations100101import re102103from ..schema import Listing104from .base import BaseConnector105106BASE = "https://sofiavarennes.com"107LIST_URL = f"{BASE}/plans.php"108ADDRESS = "1845, rue du Souvenir, Varennes"109CITY = "Varennes"110111# une ligne d'unité = <div data-eventtype1="…" … class="row"> … </div>112ROW_SPLIT_RE = re.compile(r'<div data-eventtype1="[^"]*"')113FIELD_RE = re.compile(114 r'fw-700 mb-12">(Emplacement|Unité|Pi2|Pièces|Modèle|Loyer)</p>'115 r'<p class="fs-12">([^<]*)</p>', re.I)116AVAILABLE_RE = re.compile(r'>Date</p>\s*OUI\b')117DATE_TXT_RE = re.compile(118 r'Loyer</p><p class="fs-12">[^<]*</p></span>\s*'119 r'<span class="col-md-1"><p class="fs-12">([^<]*)</p>', re.I)120PLAN_RE = re.compile(r'href="(perch/resources/[^"]+\.(?:jpe?g|png|webp))"', re.I)121122123def _price(txt: str) -> float | None:124 m = re.search(r"(\d[\d\s ]{2,8})\$", txt or "")125 if not m:126 return None127 try:128 return float(re.sub(r"[\s ]", "", m.group(1)))129 except ValueError:130 return None131132133class SofiaConnector(BaseConnector):134 source_id = "sofia"135 request_delay = 0.8136137 def fetch(self) -> list[Listing]:138 try:139 html = self.get(LIST_URL).text140 except Exception:141 return []142143 listings: list[Listing] = []144 seen: set[str] = set()145 for chunk in ROW_SPLIT_RE.split(html)[1:]:146 chunk = chunk.split("<h6", 1)[0] # fin de la ligne147 if not AVAILABLE_RE.search(chunk):148 continue # unité louée149 fields = {k: v.strip() for k, v in FIELD_RE.findall(chunk)}150 unit = fields.get("Unité", "")151 if not unit or unit in seen:152 continue153 seen.add(unit)154155 price = _price(fields.get("Loyer", ""))156 area = None157 if fields.get("Pi2", "").isdigit():158 area = float(fields["Pi2"])159 bedrooms = None160 bm = re.match(r"(\d+)\s*ch", fields.get("Pièces", ""), re.I)161 if bm:162 bedrooms = float(bm.group(1))163 unit_type = fields.get("Modèle", "") # « 3 1/2 » -> finalize164165 availability = "Disponible"166 dm = DATE_TXT_RE.search(chunk)167 if dm and dm.group(1).strip():168 availability = f"Disponible : {dm.group(1).strip()}"169170 images = [f"{BASE}/{p}" for p in dict.fromkeys(PLAN_RE.findall(chunk))]171172 listings.append(Listing(173 source=self.source_id,174 external_id=unit,175 url=LIST_URL,176 title=f"Le Sofia — unité {unit}",177 address=ADDRESS,178 city=CITY,179 unit_type=unit_type,180 bedrooms=bedrooms,181 price=price,182 price_label=f"{int(price)} $ /mois" if price else "",183 availability=availability,184 area_sqft=area,185 description=f"Unité {unit} au Sofia ({ADDRESS}) — immeuble "186 "locatif du Groupe Lacombe, structure de béton, "187 "ascenseur, climatisation, garage et borne de "188 "recharge.",189 images=images,190 ))191 return listings192