51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class Offer:
|
|
source: str
|
|
external_id: str
|
|
title: str
|
|
description: str
|
|
price: str
|
|
price_amount: int | None
|
|
url: str
|
|
category_id: int
|
|
published_at: str
|
|
seller_username: str | None = None
|
|
|
|
@property
|
|
def dedup_key(self) -> str:
|
|
return f"{self.source}:{self.external_id}"
|
|
|
|
def to_dict(self) -> dict[str, str | int | None]:
|
|
return {
|
|
"source": self.source,
|
|
"external_id": self.external_id,
|
|
"title": self.title,
|
|
"description": self.description,
|
|
"price": self.price,
|
|
"price_amount": self.price_amount,
|
|
"url": self.url,
|
|
"category_id": self.category_id,
|
|
"published_at": self.published_at,
|
|
"seller_username": self.seller_username,
|
|
}
|
|
|
|
@classmethod
|
|
def from_dict(cls, data: dict[str, str | int | None]) -> "Offer":
|
|
return cls(
|
|
source=str(data["source"]),
|
|
external_id=str(data["external_id"]),
|
|
title=str(data["title"]),
|
|
description=str(data["description"]),
|
|
price=str(data["price"]),
|
|
price_amount=int(data["price_amount"]) if data["price_amount"] is not None else None,
|
|
url=str(data["url"]),
|
|
category_id=int(data["category_id"]),
|
|
published_at=str(data["published_at"]),
|
|
seller_username=str(data["seller_username"]) if data["seller_username"] is not None else None,
|
|
)
|