SPB Git

Repository Public

louis_alexandre.py 9.6 KB 248 lines python

1# =============================================================================2#                  SP BOUCHER | SOFTWARE & RESEARCH3#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project          : Lou·Ka9# File             : louka/connectors/louis_alexandre.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/louis_alexandre.py : connecteur Le Louis-Alexandre91#   (condoslouisalexandre.ca — Longueuil, 2100, rue René, près du92#   Vieux-Longueuil). WordPress + Elementor rendu serveur : la page93#   « Appartements disponibles » liste chaque unité en blocs Elementor94#   « Condo #NNN | Superficie de X pi² | n chambres, n sdb | PRIX $ / mois »95#   avec image du plan et fiche PDF par unité. Le numéro d'unité (#NNN)96#   sert d'identifiant stable.97# -----------------------------------------------------------------------------98from __future__ import annotations99100import re101102from bs4 import BeautifulSoup103104from ..schema import Listing105from .base import BaseConnector106107BASE = "https://condoslouisalexandre.ca"108LIST_URL = f"{BASE}/appartements-disponibles-longueuil/"109110ADDRESS = "2100, rue René, Longueuil"111CITY = "Longueuil"112113UNIT_RE = re.compile(r"^Condo\s*#(\d+)\s*$")114SQFT_RE = re.compile(r"Superficie\s+de\s+([\d\s]+)\s*pi", re.I)115ROOMS_RE = re.compile(r"(Studio|\d\s*chambres?)\s*,\s*(\d)\s*sdb", re.I)116PRICE_RE = re.compile(r"(\d[\d\s]{2,6})\s*\$\s*/\s*mois", re.I)117IMG_RE = re.compile(118    r"https://condoslouisalexandre\.ca/wp-content/uploads/"119    r"[^\"\s\\']+\.(?:jpe?g|png|webp)", re.I)120SKIP_IMG_RE = re.compile(r"logo|icon|favicon|-\d{2,3}x\d{2,3}\.", re.I)121122123class LouisAlexandreConnector(BaseConnector):124    source_id = "louis_alexandre"125    request_delay = 0.6126127    def fetch(self) -> list[Listing]:128        listings: list[Listing] = []129        try:130            html = self.get(LIST_URL).text131        except Exception:132            return listings133        soup = BeautifulSoup(html, "html.parser")134135        # contact de l'immeuble (mailto:/tel: structurés dans la page)136        contact: dict = {}137        m = re.search(r'href="mailto:([^"?]+)"', html)138        if m:139            contact["email"] = m.group(1).strip().lower()140        m = re.search(r'href="tel:\+?1?[\s.\-]?(\d{3})[\s.\-]?(\d{3})'141                      r'[\s.\-]?(\d{4})"', html)142        if m:143            contact["phone"] = f"{m.group(1)}-{m.group(2)}-{m.group(3)}"144145        seen: set[str] = set()146        for node in soup.find_all(string=UNIT_RE):147            try:148                unit_no = UNIT_RE.match(node.strip()).group(1)149                if unit_no in seen:150                    continue151152                # bloc de l'unité : on remonte jusqu'au conteneur Elementor153                # qui porte le prix (texte « NNN $ / mois »)154                block = node.parent155                while block is not None and "$" not in block.get_text():156                    block = block.parent157                if block is None:158                    continue159                text = block.get_text(" | ", strip=True)160                if len(re.findall(r"Condo\s*#", text)) != 1:161                    continue    # conteneur trop large (plusieurs unités)162                seen.add(unit_no)163164                price = None165                price_label = ""166                m = PRICE_RE.search(text)167                if m:168                    price_label = m.group(0)169                    try:170                        val = float(m.group(1).replace(" ", "")171                                    .replace(" ", "").replace("\xa0", ""))172                        if 300 <= val <= 20000:173                            price = val174                    except ValueError:175                        pass176177                area = None178                m = SQFT_RE.search(text)179                if m:180                    try:181                        area = float(m.group(1).replace(" ", "")182                                     .replace("\xa0", ""))183                    except ValueError:184                        area = None185186                unit_type = ""187                bedrooms = None188                bathrooms = None189                m = ROOMS_RE.search(text)190                if m:191                    rooms = m.group(1)192                    bathrooms = float(m.group(2))193                    if re.match(r"studio", rooms, re.I):194                        unit_type = "Studio"195                        bedrooms = 0.0196                    else:197                        n = int(re.match(r"\d+", rooms).group(0))198                        bedrooms = float(n)199                        unit_type = f"{n + 2}½"200201                # image du plan + fiche PDF : conteneur parent (colonne voisine)202                images: list[str] = []203                pdf = ""204                wrap = block.parent205                while wrap is not None and not wrap.find("img"):206                    wrap = wrap.parent207                if wrap is not None and \208                        len(re.findall(r"Condo\s*#", wrap.get_text())) == 1:209                    for u in dict.fromkeys(IMG_RE.findall(str(wrap))):210                        if not SKIP_IMG_RE.search(u):211                            images.append(u)212                    a = wrap.find("a", href=re.compile(r"\.pdf$", re.I))213                    if a:214                        pdf = a.get("href", "")215216                details: dict = {}217                if contact:218                    details["contact"] = dict(contact)219                if pdf:220                    details["plan_pdf"] = pdf221222                amenities = []223                if area:224                    amenities.append(f"Superficie de {int(area)} pi²")225                if bathrooms:226                    amenities.append(f"{int(bathrooms)} salle(s) de bain")227228                listings.append(Listing(229                    source=self.source_id,230                    external_id=f"condo-{unit_no}",231                    url=LIST_URL,232                    title=f"Condo #{unit_no} — Le Louis-Alexandre",233                    address=ADDRESS,234                    city=CITY,235                    unit_type=unit_type,236                    bedrooms=bedrooms,237                    bathrooms=bathrooms,238                    price=price,239                    price_label=price_label,240                    area_sqft=area,241                    amenities=amenities,242                    details=details,243                    images=images[:10],244                ))245            except Exception:246                continue247        return listings248
Files main
Menu

Theme