Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project : Lou·Ka9# File : louka/connectors/immogex.py10# Module : louka11# Repository : https://git.spboucher.ai/lou-ka12# Created : 2026-08-0913# Last Modified : 2026-10-2614#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/immogex.py : connecteur Immogex (immogex.com)91# Refonte 2026-10-26 : l'ancien site GoDaddy Website Builder (blocs ABOUT_*)92# a été remplacé par un site statique Netlify (www.immogex.com). La page93# /a-louer liste les logements en cartes <li class="annonce"> (SSR) avec94# titre, adresse, prix et disponibilité ; chaque carte pointe vers une page95# détail /logements/<slug> (galerie, immeuble, étage, grandeur, description,96# inclusions). external_id = slug de la page détail (stable, PAR UNITÉ —97# l'ancien schéma nom+civique fusionnait les unités d'un même immeuble).98# Ville fixe Drummondville (tout le parc Immogex y est).99# -----------------------------------------------------------------------------100from __future__ import annotations101102import hashlib103from urllib.parse import urljoin104105from bs4 import BeautifulSoup106107from ..schema import Listing, normalize_unit_type, parse_price108from .base import BaseConnector109110BASE = "https://www.immogex.com"111LIST_URL = f"{BASE}/a-louer"112113114class ImmogexConnector(BaseConnector):115 source_id = "immogex"116 request_delay = 0.6117118 def fetch(self) -> list[Listing]:119 soup = BeautifulSoup(self.get(LIST_URL).text, "html.parser")120 grille = soup.select_one("ul.annonces")121 if grille is None:122 # gabarit changé : erreur franche plutôt que zéro silencieux123 raise RuntimeError(124 "immogex : grille ul.annonces introuvable sur /a-louer — "125 "gabarit changé ?")126 listings: dict[str, Listing] = {}127 for card in grille.select("li.annonce a[href]"):128 lst = self._parse_card(card)129 if lst and lst.external_id not in listings:130 listings[lst.external_id] = lst131 cards = grille.select("li.annonce")132 if cards and not listings:133 raise RuntimeError(134 f"immogex : {len(cards)} carte(s) affichée(s) mais aucune "135 "parsée — gabarit des cartes changé ?")136 return list(listings.values())137138 def _parse_card(self, card) -> Listing | None:139 url = urljoin(LIST_URL, card["href"])140 ext_id = url.rstrip("/").rsplit("/", 1)[-1].removesuffix(".html")141 if not ext_id:142 return None143144 def _txt(sel: str) -> str:145 el = card.select_one(sel)146 return el.get_text(" ", strip=True) if el else ""147148 title = _txt(".annonce-titre")149 address = _txt(".annonce-adresse")150 price_label = _txt(".annonce-prix strong")151 availability = _txt(".annonce-dispo")152 images: list[str] = []153 img = card.select_one(".annonce-photo img")154 if img and img.get("src"):155 images = [urljoin(LIST_URL, img["src"])]156157 key = hashlib.sha256("|".join(158 [title, address, price_label, availability]).encode()).hexdigest()159 det = self.detail(ext_id, key, lambda: self._fetch_detail(url))160161 faits = det.get("faits") or {}162 building = det.get("building") or ""163 unit_type = normalize_unit_type(164 faits.get("Grandeur") or title.split("·")[0].strip())165 details: dict = {}166 if building:167 details["building"] = building168 if faits.get("Étage"):169 details["floor"] = faits["Étage"]170171 return Listing(172 source=self.source_id,173 external_id=ext_id,174 url=url,175 title=f"{building} · {title}" if building else title,176 address=faits.get("Adresse") or address,177 city="Drummondville",178 unit_type=unit_type,179 price=parse_price(price_label),180 price_label=price_label,181 availability=faits.get("Disponibilité") or availability,182 description=(det.get("description") or "")[:2000],183 amenities=det.get("amenities") or [],184 details=details,185 images=det.get("images") or images,186 )187188 def _fetch_detail(self, url: str) -> dict:189 soup = BeautifulSoup(self.get(url).text, "html.parser")190 out: dict = {}191 sur = soup.select_one(".fl-resume .surtitre")192 if sur:193 out["building"] = sur.get_text(" ", strip=True)194 faits = {}195 for row in soup.select(".fl-faits div"):196 dt, dd = row.select_one("dt"), row.select_one("dd")197 if dt and dd:198 faits[dt.get_text(" ", strip=True)] = dd.get_text(" ", strip=True)199 out["faits"] = faits200 desc = soup.select_one("#t-desc")201 if desc:202 out["description"] = " ".join(203 p.get_text(" ", strip=True) for p in desc.parent.select("p"))204 out["amenities"] = [li.get_text(" ", strip=True)205 for li in soup.select(".fl-bloc ul.points li")]206 images = []207 for img in soup.select(".fl-piste img"):208 src = img.get("src") or ""209 if src:210 src = urljoin(url, src)211 if src.startswith("http") and src not in images:212 images.append(src)213 out["images"] = images214 return out215