Repository Public
1# =============================================================================2# SP BOUCHER | SOFTWARE & RESEARCH3# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project : Auto·Ka9# File : autoka/connectors/okaze.py10# Module : autoka11# Repository : https://git.spboucher.ai/auto-ka12# Created : 2026-08-1213# Last Modified : 2026-08-1214#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# Auto-Ka — Agrégateur de voitures usagées à vendre (province de Québec)89# Auteur : Simon-Pierre Boucher — contact@spboucher.ai90# connectors/okaze.py : OKAZE (Saguenay) — site Webflow (CMS w-dyn-list).91#92# Stratégie : la page /inventaire est rendue côté serveur avec TOUTES les93# annonces (pas de pagination). Chaque carte embarque un <script> inline qui94# pose des attributs data-* dont `data-search` : une chaîne CSV complète —95# nom, année, carrosserie, catégorie, couleur ext., couleur int., marque,96# modèle, moteur, portes, km, prix, motricité, transmission, carburant.97# On parse ces cartes en HTML brut (regex) : une seule requête par sync.98# -----------------------------------------------------------------------------99from __future__ import annotations100101import re102103from ..schema import Vehicle104from .base import BaseConnector105106# OKAZE « vend tout » : écarter les catégories non automobiles107_EXCLUDE_RE = re.compile(108 r"motoneige|vtt|bateau|ponton|motomarine|\bmoto\b|roulotte|remorque|"109 r"\bvr\b|campeur|tracteur|souffleuse|scooter|spyder|side-by-side|atv",110 re.I)111112_CARD_SPLIT = re.compile(r'class="item-inventaire w-dyn-item"')113_HREF_RE = re.compile(r'href="(/vehicules/[^"]+)"')114_IMG_RE = re.compile(r'<img src="(https://[^"]+)"')115_ATTR_RE = re.compile(116 r"item\.setAttribute\('data-([a-z-]+)',\s*(?:formatDate)?\(\"(.*?)\"\)",117 re.S)118119120class Okaze(BaseConnector):121 source_id = "okaze"122 base_url = "https://www.okaze.ca"123 dealer_name = "OKAZE"124 city = "Saguenay"125 request_delay: float = 1.0126127 def fetch(self) -> list[Vehicle]:128 html = self.get(f"{self.base_url}/inventaire").text129 vehicles: list[Vehicle] = []130 for block in _CARD_SPLIT.split(html)[1:]:131 veh = self._parse_card(block)132 if veh is not None:133 vehicles.append(veh)134 return vehicles135136 def _parse_card(self, block: str) -> Vehicle | None:137 m = _HREF_RE.search(block)138 if not m:139 return None140 path = m.group(1)141 ext_id = path.rstrip("/").rsplit("/", 1)[-1]142143 attrs = {name: value for name, value in _ATTR_RE.findall(block)}144 title = " ".join((attrs.get("name") or "").split())145 search = attrs.get("search") or ""146 fields = [f.strip() for f in search.split(",")]147148 # data-search : positions fixes quand la fiche est complète (15 champs)149 body = category = ext_color = int_color = engine = ""150 drivetrain = transmission = fuel = ""151 doors = km = None152 if len(fields) >= 15:153 (_, _, body, category, ext_color, int_color, _, _,154 engine) = fields[:9]155 drivetrain, transmission, fuel = fields[-3:]156 try:157 doors = int(fields[9]) or None158 except ValueError:159 doors = None160 try:161 km = float(fields[10])162 except ValueError:163 km = None164 else: # fiche partielle : heuristiques165 for f in fields:166 if re.fullmatch(r"\d{2,6}", f) and km is None:167 km = float(f)168 elif "intégrale" in f.lower() or "traction" in f.lower() \169 or "propulsion" in f.lower():170 drivetrain = f171 elif f.lower() in ("automatique", "manuelle", "cvt"):172 transmission = f173174 if _EXCLUDE_RE.search(f"{title} {category} {body}"):175 return None176 if not title:177 return None178179 price_label = attrs.get("price", "")180 if not price_label:181 pm = re.search(r'class="inventory-price"[^>]*>([^<]+)<', block)182 price_label = pm.group(1).strip() if pm else ""183184 details = {}185 if category:186 details["category"] = category187 if attrs.get("added"):188 details["listed_date"] = attrs["added"]189190 img = _IMG_RE.search(block)191192 return Vehicle(193 source=self.source_id,194 external_id=ext_id,195 url=f"{self.base_url}{path}",196 title=title,197 make=attrs.get("brand", "").title(),198 model=attrs.get("model", ""),199 price_label=price_label,200 mileage_km=km,201 mileage_label=(f"{km:,.0f} km".replace(",", " ") if km else ""),202 transmission=transmission,203 fuel=fuel,204 drivetrain=drivetrain,205 body_type=body,206 exterior_color=ext_color.capitalize(),207 interior_color=int_color.capitalize(),208 engine=engine,209 doors=doors,210 dealer_name=self.dealer_name,211 city=self.city,212 details=details,213 images=[img.group(1)] if img else [],214 )215