SPB Git forge

spb/rent-ka

Public
8commits 1branches 0releases
7.4 MBsize
maindefault branch
19 days agolast push
Python 68.8% TypeScript 18.6% CSS 8.7% JavaScript 3.3% HTML 0.6%
2.9 KB · 71 lines python
Raw Blame History
1#!/usr/bin/env python32# -----------------------------------------------------------------------------3# Rent-Ka — Rental listings aggregator (Canada, outside Québec)4# scripts/migrate_types_en.py : one-shot migration of the seeded DB rows5#   (imported from Lou-Ka) to the English canon — unit_type («4½» ->6#   «2 bedrooms»), pets (oui/non -> yes/no), amenity/availability/price7#   labels through the central FR->EN funnel. Idempotent.8#   Run from the repo root: .venv/bin/python scripts/migrate_types_en.py9# -----------------------------------------------------------------------------10import json11import sys12from pathlib import Path1314sys.path.insert(0, str(Path(__file__).resolve().parent.parent))1516from rentka import db                                   # noqa: E40217from rentka.normalize import (bedrooms_from_unit_type,  # noqa: E40218                              normalize_unit_type, translate_label_en)1920con = db.connect()21rows = con.execute(22    "SELECT uid, unit_type, pets, availability, price_label, amenities,"23    " bedrooms FROM listings").fetchall()24n_ut = n_pets = n_lbl = n_bed = 025for r in rows:26    sets, args = [], []27    ut = r["unit_type"] or ""28    new_ut = normalize_unit_type(ut) if ut else ut29    if new_ut != ut:30        sets.append("unit_type=?"); args.append(new_ut); n_ut += 131        # backfill bedrooms from the new canonical type when unknown32        if r["bedrooms"] is None:33            b = bedrooms_from_unit_type(new_ut)34            if b is not None:35                sets.append("bedrooms=?"); args.append(b); n_bed += 136    pets = r["pets"]37    if pets in ("oui", "non"):38        sets.append("pets=?"); args.append("yes" if pets == "oui" else "no")39        n_pets += 140    avail = r["availability"] or ""41    new_avail = translate_label_en(avail) if avail else avail42    lbl = r["price_label"] or ""43    new_lbl = translate_label_en(lbl) if lbl else lbl44    try:45        ams = json.loads(r["amenities"] or "[]")46    except ValueError:47        ams = []48    new_ams = [translate_label_en(a) for a in ams]49    if new_avail != avail:50        sets.append("availability=?"); args.append(new_avail)51    if new_lbl != lbl:52        sets.append("price_label=?"); args.append(new_lbl)53    if new_ams != ams:54        sets.append("amenities=?")55        args.append(json.dumps(new_ams, ensure_ascii=False))56    if new_avail != avail or new_lbl != lbl or new_ams != ams:57        n_lbl += 158    if sets:59        con.execute(f"UPDATE listings SET {', '.join(sets)} WHERE uid=?",60                    args + [r["uid"]])61con.commit()62con.close()63con = db.connect()64after = [r["unit_type"] for r in con.execute(65    "SELECT DISTINCT unit_type FROM listings WHERE unit_type<>''")]66con.close()67print(f"[rent-ka] migrate_types_en: unit_type={n_ut} bedrooms={n_bed} "68      f"pets={n_pets} labels={n_lbl} (of {len(rows)} rows)")69print(f"[rent-ka] distinct unit types after: {sorted(after)}")70print("[rent-ka] now run: run.py quality")71