SPB Git

Repository Public

omhm.py 7.3 KB 173 lines python

1# =============================================================================2#                  SP BOUCHER | SOFTWARE & RESEARCH3#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project          : Lou·Ka9# File             : louka/connectors/omhm.py10# Module           : louka11# Repository       : https://git.spboucher.ai/lou-ka12# Created          : 2026-08-1713# Last Modified    : 2026-08-1714#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/omhm.py : OMHM — Office municipal d'habitation de Montréal.91#   Le HLM classique fonctionne par liste d'attente (non agrégeable) ; SEULE92#   la page « logements disponibles » (résidences abordables ENHARMONIE pour93#   aînés autonomes 60 ans +) liste de vraies unités. Site Drupal SSR, aucun94#   anti-bot. Chaque unité = un <article class="node--type-logement-disponible">95#   avec adresse civique complète et grille de prix par type (« 3 ½ entre96#   1 357 $ et 1 497 $ »). Volume faible (~5) mais différenciateur social97#   (filtre « logement abordable » de Lou-Ka).98# -----------------------------------------------------------------------------99from __future__ import annotations100101import re102103from bs4 import BeautifulSoup104105from ..schema import Listing, normalize_unit_type106from .base import BaseConnector107108URL = "https://www.omhm.qc.ca/fr/soumettre-une-demande/logements-disponibles"109110_POSTAL_RE = re.compile(r"[A-Z]\d[A-Z]\s?\d[A-Z]\d")111# « 3 ½ entre 1 357 $ et 1 497 $ » ou « studio 730 $ à 925 $ » ou « 4 ½ à 2 167 $ »112_UNIT_PRICE_RE = re.compile(113    r"(studio|\d\s*½|\d\s*1/2)\s*(?:entre|à|de|:)?\s*"114    r"([\d\s]+)\s*\$(?:\s*(?:et|à|-)\s*([\d\s]+)\s*\$)?", re.I)115116117class OmhmConnector(BaseConnector):118    source_id = "omhm"119    request_delay = 0.8120121    def fetch(self) -> list[Listing]:122        soup = BeautifulSoup(self.get(URL).text, "html.parser")123        out: list[Listing] = []124        for art in soup.select("article.node--type-logement-disponible"):125            title_el = art.find(re.compile("^h[2-4]$"))126            name = title_el.get_text(" ", strip=True) if title_el else ""127            full = re.sub(r"\s+", " ", art.get_text(" | ", strip=True))128129            # adresse : « 12100, rue Rodolphe-Forget | Montréal | H1E 7S2 »130            m = re.search(r"(\d+[^|]+?)\s*\|\s*([A-ZÉÈÀ][^|]+?)\s*\|\s*"131                          r"(" + _POSTAL_RE.pattern + r")", full)132            address = city = postal = ""133            if m:134                address = m.group(1).strip(" ,")135                city = m.group(2).strip()136                postal = m.group(3).strip()137138            slug = re.sub(r"[^a-z0-9]+", "-",139                          (name or address).lower()).strip("-") or "omhm"140            # une annonce par type d'unité offert dans la résidence141            seen = set()142            for um in _UNIT_PRICE_RE.finditer(full):143                unit_type = normalize_unit_type(um.group(1))144                if unit_type in seen:145                    continue146                seen.add(unit_type)147                try:148                    price = float(um.group(2).replace(" ", ""))149                except ValueError:150                    continue151                pmax = um.group(3)152                label = (f"Entre {price:,.0f} $ et {float(pmax.replace(' ','')):,.0f} $"153                         .replace(",", " ") if pmax154                         else f"À partir de {price:,.0f} $".replace(",", " "))155                out.append(Listing(156                    source=self.source_id,157                    external_id=f"{slug}-{unit_type}",158                    url=URL,159                    title=f"{name} — {unit_type}" if name else unit_type,160                    address=address,161                    city=city or "Montréal",162                    unit_type=unit_type,163                    price=price,164                    price_label=label,165                    availability="Disponible",166                    details={"Code postal": postal,167                             "Logement abordable": "oui",168                             "Résidence pour aînés": "oui"} if postal else169                            {"Logement abordable": "oui",170                             "Résidence pour aînés": "oui"},171                ))172        return out173
Files main
Menu

Theme