Stelle den Frontend-MVP und die v0.3-API auf develop bereit, damit Dev API und PWA zusammen deployen.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
827e6a2a11
commit
b276758ee1
152 files changed
+25846
-34
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,85 @@
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
from typing import Any, Protocol
|
||||
from uuid import UUID
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SourceProduct:
|
||||
source_key: str
|
||||
external_id: str
|
||||
id: UUID
|
||||
category: str
|
||||
brand: str
|
||||
name: str
|
||||
model: str | None
|
||||
variants: tuple["SourceVariant", ...]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SourceVariant:
|
||||
id: UUID
|
||||
name: str
|
||||
manufacturer_sku: str | None
|
||||
identifiers: dict[str, Any]
|
||||
attributes: tuple["SourceAttribute", ...]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SourceAttribute:
|
||||
id: UUID
|
||||
attribute: str
|
||||
raw_value: Any
|
||||
normalized_value: Any
|
||||
unit: str | None
|
||||
normalization_status: str
|
||||
normalizer_version: str
|
||||
confidence: Decimal
|
||||
source_reference: str
|
||||
observed_at: datetime
|
||||
normalization_error: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SourceOffer:
|
||||
id: UUID
|
||||
variant_id: UUID
|
||||
external_id: str
|
||||
seller: str
|
||||
url: str | None
|
||||
market: str
|
||||
currency: str
|
||||
condition: str
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SourcePriceObservation:
|
||||
id: UUID | None
|
||||
external_offer_id: str
|
||||
external_observation_id: str
|
||||
observed_at: datetime
|
||||
item_price: Decimal
|
||||
shipping_price: Decimal | None
|
||||
currency: str
|
||||
availability: str
|
||||
raw_payload: Any
|
||||
confidence: Decimal
|
||||
|
||||
|
||||
class ProductSource(Protocol):
|
||||
source_key: str
|
||||
|
||||
def get_product(self, external_id: str) -> SourceProduct: ...
|
||||
def get_offers(self, external_product_id: str) -> list[SourceOffer]: ...
|
||||
def get_price_history(self, external_offer_id: str) -> list[SourcePriceObservation]: ...
|
||||
|
||||
|
||||
class DiscoverySourceAdapter(Protocol):
|
||||
source_key: str
|
||||
|
||||
def discover_taxonomy(self, category_ref: str) -> dict[str, Any]: ...
|
||||
def build_query(self, criteria: list[dict[str, Any]], mappings: list[dict[str, Any]]) -> dict[str, Any]: ...
|
||||
def fetch_results(self, query: dict[str, Any], pagination: dict[str, Any]) -> dict[str, Any]: ...
|
||||
def parse_results(self, response: dict[str, Any]) -> list[dict[str, Any]]: ...
|
||||
def normalize_result(self, result: dict[str, Any]) -> dict[str, Any]: ...
|
||||
@@ -0,0 +1,3 @@
|
||||
from app.sources.geizhals.adapter import GeizhalsAdapter
|
||||
|
||||
__all__ = ["GeizhalsAdapter"]
|
||||
@@ -0,0 +1,81 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
from urllib.parse import parse_qs, urlparse
|
||||
|
||||
from app.discovery.enums import FilterType
|
||||
from app.errors import AppError
|
||||
|
||||
FIXTURE_DIR = Path(__file__).resolve().parents[3] / "fixtures" / "geizhals"
|
||||
|
||||
|
||||
class GeizhalsAdapter:
|
||||
source_key = "geizhals"
|
||||
|
||||
def __init__(self, fixture_dir: Path | None = None, live: bool = False) -> None:
|
||||
self.fixture_dir = fixture_dir or FIXTURE_DIR
|
||||
self.live = live
|
||||
|
||||
def discover_taxonomy(self, category_ref: str) -> dict[str, Any]:
|
||||
payload = self._load(f"{category_ref}-taxonomy.json")
|
||||
return payload
|
||||
|
||||
def build_query(self, criteria: list[dict[str, Any]], mappings: list[dict[str, Any]]) -> dict[str, Any]:
|
||||
from app.discovery.urlgen import generate_search_url
|
||||
|
||||
category_url = f"https://geizhals.de/?cat={self._category(mappings, criteria)}"
|
||||
url, internal_only = generate_search_url(
|
||||
category_url=category_url,
|
||||
criteria_items=criteria,
|
||||
mappings=mappings,
|
||||
)
|
||||
return {"url": url, "internal_only": internal_only}
|
||||
|
||||
def fetch_results(self, query: dict[str, Any], pagination: dict[str, Any]) -> dict[str, Any]:
|
||||
if self.live:
|
||||
raise AppError(422, "LIVE_FETCH_DISABLED", "live Geizhals fetch is not enabled in this process")
|
||||
page = int(pagination.get("page") or 1)
|
||||
category = parse_qs(urlparse(query["url"]).query).get("cat", ["nb"])[0]
|
||||
return self._load(f"{category}-search-page{page}.json")
|
||||
|
||||
def parse_results(self, response: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
return list(response.get("items") or [])
|
||||
|
||||
def normalize_result(self, result: dict[str, Any]) -> dict[str, Any]:
|
||||
attrs = result.get("normalized_attributes") or {}
|
||||
return {
|
||||
"source_external_id": result.get("source_external_id"),
|
||||
"source_url": result.get("source_url"),
|
||||
"source_title": result.get("source_title"),
|
||||
"manufacturer": result.get("manufacturer"),
|
||||
"model": result.get("model"),
|
||||
"manufacturer_sku": result.get("manufacturer_sku"),
|
||||
"gtin_ean": result.get("gtin_ean"),
|
||||
"best_price": result.get("best_price"),
|
||||
"currency": result.get("currency") or "EUR",
|
||||
"availability": result.get("availability") or "unknown",
|
||||
"seller_count": result.get("seller_count"),
|
||||
"raw_attributes": result.get("raw_attributes") or {},
|
||||
"normalized_attributes": attrs,
|
||||
}
|
||||
|
||||
def _load(self, name: str) -> dict[str, Any]:
|
||||
path = self.fixture_dir / name
|
||||
if not path.exists():
|
||||
raise AppError(404, "FIXTURE_NOT_FOUND", f"geizhals fixture missing: {name}")
|
||||
return json.loads(path.read_text(encoding="utf-8"))
|
||||
|
||||
def _category(self, mappings: list[dict[str, Any]], criteria: list[dict[str, Any]]) -> str:
|
||||
for mapping in mappings:
|
||||
if mapping.get("category_key"):
|
||||
return str(mapping["category_key"])
|
||||
return "nb"
|
||||
|
||||
|
||||
def filter_type(raw: str) -> str:
|
||||
try:
|
||||
return FilterType(raw).value
|
||||
except ValueError:
|
||||
return FilterType.UNKNOWN.value
|
||||
@@ -0,0 +1,110 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
from pathlib import Path
|
||||
from uuid import UUID
|
||||
|
||||
from app.errors import AppError
|
||||
from app.sources.base import (
|
||||
SourceAttribute,
|
||||
SourceOffer,
|
||||
SourcePriceObservation,
|
||||
SourceProduct,
|
||||
SourceVariant,
|
||||
)
|
||||
|
||||
|
||||
def _parse_dt(value: str) -> datetime:
|
||||
return datetime.fromisoformat(value.replace("Z", "+00:00"))
|
||||
|
||||
|
||||
class ManualSource:
|
||||
source_key = "manual"
|
||||
|
||||
def __init__(self, path: Path) -> None:
|
||||
self.path = path
|
||||
self._data = json.loads(path.read_text(encoding="utf-8"))
|
||||
self._products = {item["external_id"]: item for item in self._data["products"]}
|
||||
self._offers = {item["external_id"]: item for item in self._data.get("offers", [])}
|
||||
self._prices = self._data.get("prices", [])
|
||||
|
||||
def list_external_product_ids(self) -> list[str]:
|
||||
return list(self._products)
|
||||
|
||||
def get_product(self, external_id: str) -> SourceProduct:
|
||||
raw = self._products.get(external_id)
|
||||
if raw is None:
|
||||
raise AppError(404, "NOT_FOUND", "manual product not found", {"external_id": external_id})
|
||||
variants = []
|
||||
for variant in raw["variants"]:
|
||||
attributes = tuple(
|
||||
SourceAttribute(
|
||||
id=UUID(item["id"]),
|
||||
attribute=item["attribute"],
|
||||
raw_value=item["raw_value"],
|
||||
normalized_value=item.get("normalized_value"),
|
||||
unit=item.get("unit"),
|
||||
normalization_status=item["normalization_status"],
|
||||
normalizer_version=item.get("normalizer_version", "manual-v1"),
|
||||
confidence=Decimal(str(item["confidence"])),
|
||||
source_reference=item["source_reference"],
|
||||
observed_at=_parse_dt(item["observed_at"]),
|
||||
normalization_error=item.get("normalization_error"),
|
||||
)
|
||||
for item in variant["attributes"]
|
||||
)
|
||||
variants.append(
|
||||
SourceVariant(
|
||||
id=UUID(variant["id"]),
|
||||
name=variant["name"],
|
||||
manufacturer_sku=variant.get("manufacturer_sku"),
|
||||
identifiers=variant.get("identifiers") or {},
|
||||
attributes=attributes,
|
||||
)
|
||||
)
|
||||
return SourceProduct(
|
||||
source_key=self.source_key,
|
||||
external_id=external_id,
|
||||
id=UUID(raw["id"]),
|
||||
category=raw["category"],
|
||||
brand=raw["brand"],
|
||||
name=raw["name"],
|
||||
model=raw.get("model"),
|
||||
variants=tuple(variants),
|
||||
)
|
||||
|
||||
def get_offers(self, external_product_id: str) -> list[SourceOffer]:
|
||||
product = self.get_product(external_product_id)
|
||||
variant_ids = {variant.id for variant in product.variants}
|
||||
return [
|
||||
SourceOffer(
|
||||
id=UUID(item["id"]),
|
||||
variant_id=UUID(item["variant_id"]),
|
||||
external_id=item["external_id"],
|
||||
seller=item["seller"],
|
||||
url=item.get("url"),
|
||||
market=item["market"],
|
||||
currency=item["currency"],
|
||||
condition=item["condition"],
|
||||
)
|
||||
for item in self._offers.values()
|
||||
if UUID(item["variant_id"]) in variant_ids
|
||||
]
|
||||
|
||||
def get_price_history(self, external_offer_id: str) -> list[SourcePriceObservation]:
|
||||
return [
|
||||
SourcePriceObservation(
|
||||
id=UUID(item["id"]) if item.get("id") else None,
|
||||
external_offer_id=external_offer_id,
|
||||
external_observation_id=item["external_observation_id"],
|
||||
observed_at=_parse_dt(item["observed_at"]),
|
||||
item_price=Decimal(str(item["item_price"])),
|
||||
shipping_price=None if item.get("shipping_price") is None else Decimal(str(item["shipping_price"])),
|
||||
currency=item["currency"],
|
||||
availability=item["availability"],
|
||||
raw_payload=item["raw_payload"],
|
||||
confidence=Decimal(str(item["confidence"])),
|
||||
)
|
||||
for item in self._prices
|
||||
if item["external_offer_id"] == external_offer_id
|
||||
]
|
||||
Reference in new issue
Block a user