cleanup and fix runtime bugs
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
@@ -20,7 +18,7 @@ from ..storage.schemas import CarRecord, ImageRecord
|
||||
|
||||
|
||||
class CarMapper:
|
||||
"""IAAI data → CarRecord."""
|
||||
# IAAI data → CarRecord.
|
||||
|
||||
BODY_MAP = {
|
||||
"sedan": "SEDAN", "coupe": "COUPE", "hatchback": "HATCHBACK", "sport utility": "SUV",
|
||||
@@ -51,7 +49,6 @@ class CarMapper:
|
||||
# Собираем нормализованную DB-модель.
|
||||
vehicle_summary = vehicle_summary or {}
|
||||
payload_insights = payload_insights or {}
|
||||
notes: list[str] = []
|
||||
core = payload_insights.get("vehicle_core", {})
|
||||
pricing = payload_insights.get("pricing", {})
|
||||
damage = payload_insights.get("damage", {})
|
||||
@@ -110,55 +107,7 @@ class CarMapper:
|
||||
)
|
||||
slug = self._slugify(" ".join(filter(None, [str(year or ""), brand, model, origin_id])))
|
||||
images_records = self._build_images(images.get("urls") or vehicle_summary.get("image_urls") or [])
|
||||
origin = "IAAI" if "IAAI" in ORIGIN_ENUM_VALUES else "NA"
|
||||
if not price:
|
||||
notes.append("Price is missing or not parseable from the observed payloads.")
|
||||
if not vehicle_summary.get("vin"):
|
||||
notes.append("VIN was not observed in the accessible payloads for this account/session.")
|
||||
if not images_records:
|
||||
notes.append("No image URLs were found in the captured payloads.")
|
||||
|
||||
raw_attributes = {
|
||||
# Сохраняем полезный сырой контекст.
|
||||
"vin": vehicle_summary.get("vin"),
|
||||
"lot_number": first_non_empty([core.get("lot_number"), vehicle_summary.get("lot_number")]),
|
||||
"trim": first_non_empty([core.get("trim"), vehicle_summary.get("trim")]),
|
||||
"fuel_type": first_non_empty([core.get("fuel_type"), vehicle_summary.get("fuel_type")]),
|
||||
"cylinders": first_non_empty([core.get("cylinders"), vehicle_summary.get("cylinders")]),
|
||||
"engine": first_non_empty([core.get("engine"), vehicle_summary.get("engine")]),
|
||||
"manufactured_in": vehicle_summary.get("manufactured_in"),
|
||||
"vehicle_class": vehicle_summary.get("vehicle_class"),
|
||||
"run_and_drive": first_non_empty([core.get("run_and_drive"), vehicle_summary.get("run_and_drive")]),
|
||||
"keys": first_non_empty([core.get("keys"), vehicle_summary.get("keys")]),
|
||||
"title": title_text,
|
||||
"title_brand": vehicle_summary.get("title_brand"),
|
||||
"damage_primary": first_non_empty([damage.get("primary"), vehicle_summary.get("primary_damage")]),
|
||||
"damage_secondary": damage.get("secondary"),
|
||||
"damage_description": damage.get("description"),
|
||||
"buy_now": first_non_empty([pricing.get("buy_now"), vehicle_summary.get("buy_now")]),
|
||||
"current_bid": first_non_empty([pricing.get("current_bid"), vehicle_summary.get("current_bid")]),
|
||||
"actual_cash_value": pricing.get("actual_cash_value"),
|
||||
"estimated_repair_cost": pricing.get("estimated_repair_cost"),
|
||||
"seller": seller,
|
||||
"location": location,
|
||||
"vehicle_location": vehicle_summary.get("vehicle_location"),
|
||||
"auction_date": auction.get("auction_date"),
|
||||
"lane": auction.get("lane"),
|
||||
"branch": auction.get("branch"),
|
||||
"sale_status": auction.get("sale_status"),
|
||||
"source_endpoints": payload_insights.get("source_endpoints", {}),
|
||||
}
|
||||
|
||||
content_hash = hashlib.sha256(json.dumps({
|
||||
# Хеш для пропуска записей без изменений.
|
||||
"brand": brand, "model": model, "year": year, "price": price, "mileage": mileage,
|
||||
"color": color, "drive": drive, "gearbox": gearbox, "body_type": body_type,
|
||||
"engine_volume": engine_volume, "is_damaged": is_damaged, "is_sold": is_sold,
|
||||
"country": country, "selling_type": "AUCTION", "one_owner": one_owner,
|
||||
"new_car": new_car, "evaluation": evaluation, "non_smoking": non_smoking,
|
||||
"rental": rental, "repair_history": repair_history,
|
||||
"images": [image.fullres_image for image in images_records],
|
||||
}, sort_keys=True, default=str).encode()).hexdigest()
|
||||
origin = "IAAI"
|
||||
|
||||
return CarRecord(
|
||||
parser_id=parser_id, brand=brand, model=model, year=year, price=price, currency=currency,
|
||||
@@ -167,8 +116,7 @@ class CarMapper:
|
||||
selling_type=self._normalize_selling_type("AUCTION"), one_owner=one_owner, new_car=new_car,
|
||||
is_hidden=False, origin=origin, origin_url=vehicle_url, origin_id=origin_id, is_damaged=is_damaged,
|
||||
evaluation=evaluation, non_smoking=non_smoking, rental=rental, repair_history=repair_history,
|
||||
slug=slug, last_seen_at=datetime.now(timezone.utc), content_hash=content_hash, images=images_records,
|
||||
raw_attributes=raw_attributes, mapping_notes=notes,
|
||||
slug=slug, last_seen_at=datetime.now(timezone.utc), images=images_records,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -10,7 +10,7 @@ logger = logging.getLogger("iaai_scraper.parsers")
|
||||
|
||||
|
||||
class VehicleParser:
|
||||
"""DOM + JSON парсер страницы авто."""
|
||||
# DOM + JSON парсер страницы авто.
|
||||
|
||||
SUMMARY_KEY_MAP = {
|
||||
"vin": {"vin", "vehicleidentificationnumber"},
|
||||
|
||||
Reference in New Issue
Block a user