fix parsing db and tests
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import json
|
||||
import logging
|
||||
from contextlib import contextmanager
|
||||
from datetime import datetime, timezone
|
||||
@@ -13,6 +14,41 @@ from .schemas import CarRecord
|
||||
logger = logging.getLogger("iaai_scraper.db")
|
||||
|
||||
|
||||
CAR_DB_FIELDS = {
|
||||
"parser_id",
|
||||
"brand",
|
||||
"model",
|
||||
"year",
|
||||
"price",
|
||||
"currency",
|
||||
"mileage",
|
||||
"country",
|
||||
"is_sold",
|
||||
"color",
|
||||
"drive",
|
||||
"gearbox",
|
||||
"steering_wheel",
|
||||
"body_type",
|
||||
"engine_volume",
|
||||
"selling_type",
|
||||
"one_owner",
|
||||
"new_car",
|
||||
"is_hidden",
|
||||
"origin",
|
||||
"origin_url",
|
||||
"origin_id",
|
||||
"is_damaged",
|
||||
"evaluation",
|
||||
"non_smoking",
|
||||
"rental",
|
||||
"repair_history",
|
||||
"slug",
|
||||
"last_seen_at",
|
||||
"content_hash",
|
||||
"raw_attributes",
|
||||
}
|
||||
|
||||
|
||||
class PersistenceService:
|
||||
|
||||
def __init__(self, settings: Settings) -> None:
|
||||
@@ -64,15 +100,21 @@ class PersistenceService:
|
||||
for image_payload in images:
|
||||
session.add(Image(fullres_image=str(image_payload["fullres_image"]), preview_image=str(image_payload["preview_image"]), order_index=int(image_payload.get("order_index", 0)), car_id=car_id))
|
||||
|
||||
@staticmethod
|
||||
def _car_payload(record: CarRecord) -> dict[str, object]:
|
||||
payload = record.model_dump(mode="python")
|
||||
result = {key: value for key, value in payload.items() if key in CAR_DB_FIELDS}
|
||||
# Serialize raw_attributes dict to JSON string for Text column.
|
||||
if "raw_attributes" in result and isinstance(result["raw_attributes"], dict):
|
||||
result["raw_attributes"] = json.dumps(result["raw_attributes"], ensure_ascii=False, default=str)
|
||||
return result
|
||||
|
||||
def upsert_car(self, record: CarRecord):
|
||||
"""Insert/update/skip по content_hash."""
|
||||
# Готовим payload отдельно от вложенных изображений и служебных полей.
|
||||
payload = record.model_dump(mode="python")
|
||||
images = payload.pop("images", [])
|
||||
payload.pop("raw_attributes", None)
|
||||
payload.pop("mapping_notes", None)
|
||||
content_hash = payload.pop("content_hash", "")
|
||||
payload["content_hash"] = content_hash
|
||||
# В БД отправляем только поля, реально существующие в финальной схеме cars.
|
||||
payload = self._car_payload(record)
|
||||
images = [image.model_dump(mode="python") for image in record.images]
|
||||
content_hash = str(payload.get("content_hash") or "")
|
||||
with self.session_scope() as session:
|
||||
# поиск по origin_id
|
||||
car = session.execute(select(Car).where(Car.origin_id == record.origin_id)).scalar_one_or_none()
|
||||
|
||||
Reference in New Issue
Block a user