feat: auto-scrape on product creation and update product data (Step 4)

- Add automatic scraping when creating a new product
- Update product title and image from scraped data
- Add GET /products/{id}/snapshots endpoint for price history
- Add list_snapshots and get_latest_snapshot to CRUD

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-18 19:33:04 +01:00
parent 744d16c2c5
commit 4ff5d3ee79
3 changed files with 59 additions and 4 deletions

View File

@@ -53,6 +53,20 @@ def _save_debug_artifacts(page, product_id: int) -> tuple[Path, Path]:
return screenshot_path, html_path
def _update_product_from_scrape(
session: Session,
product: models.Product,
data: dict,
) -> None:
"""Met à jour le produit avec les données scrappées (titre, image)."""
if data.get("titre") and not product.titre:
product.titre = data["titre"]
if data.get("url_image_principale") and not product.url_image:
product.url_image = data["url_image_principale"]
session.add(product)
session.commit()
def _create_snapshot(
session: Session,
product: models.Product,
@@ -62,6 +76,9 @@ def _create_snapshot(
raw_json_path: Path | None,
error_message: str | None = None,
) -> None:
# Mettre à jour le produit avec titre/image si manquants
_update_product_from_scrape(session, product, data)
snapshot = models.ProductSnapshot(
produit_id=product.id,
run_scrap_id=run.id,