SPB Git

Repository Public

jsonld.py 5.3 KB 138 lines python

1# =============================================================================2#                  SP BOUCHER | SOFTWARE & RESEARCH3#             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4# =============================================================================5#6# PROJECT METADATA7# -----------------------------------------------------------------------------8# Project          : Immo·Ka9# File             : immoka/connectors/jsonld.py10# Module           : immoka11# Repository       : https://git.spboucher.ai/immo-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# Immo-Ka — Agrégateur de maisons à vendre (province de Québec)89# Auteur : Simon-Pierre Boucher — contact@spboucher.ai90# connectors/jsonld.py : utilitaires partagés d'extraction schema.org (JSON-LD)91#   Beaucoup de sites d'agences (Sutton, et d'autres CMS immobiliers) publient92#   la liste des propriétés en JSON-LD `ItemList` — données propres et stables.93# -----------------------------------------------------------------------------94from __future__ import annotations9596import json97import re9899_LD_RE = re.compile(r'<script[^>]+type="application/ld\+json"[^>]*>(.*?)</script>',100                    re.S | re.I)101102103def iter_ld(html: str):104    """Itère les objets JSON-LD d'une page (chaque bloc, aplati depuis @graph)."""105    for block in _LD_RE.findall(html):106        block = block.strip()107        if not block:108            continue109        try:110            data = json.loads(block)111        except ValueError:112            continue113        nodes = data.get("@graph", [data]) if isinstance(data, dict) else data114        for node in (nodes if isinstance(nodes, list) else [nodes]):115            if isinstance(node, dict):116                yield node117118119def item_list_elements(html: str) -> list[dict]:120    """Retourne les `itemListElement` (schema.org ItemList) trouvés dans la page."""121    out: list[dict] = []122    for node in iter_ld(html):123        elems = node.get("itemListElement")124        if isinstance(elems, list):125            out.extend(e for e in elems if isinstance(e, dict))126        # certaines pages imbriquent l'ItemList sous mainEntity127        me = node.get("mainEntity")128        if isinstance(me, dict) and isinstance(me.get("itemListElement"), list):129            out.extend(e for e in me["itemListElement"] if isinstance(e, dict))130    return out131132133def sqm_to_sqft(value) -> float | None:134    try:135        return round(float(value) * 10.7639)136    except (TypeError, ValueError):137        return None138
Files main
Menu

Theme