refactor dump listings to start using model instead of the data_access object

This commit is contained in:
Viktor Barzin 2025-06-07 12:46:53 +00:00
parent 842f7cefbe
commit 4f5a934fa9
No known key found for this signature in database
GPG key ID: 4056458DBDBF8863
5 changed files with 52 additions and 38 deletions

View file

@ -13,7 +13,8 @@ import datetime
@dataclass()
class Listing:
identifier: int
_cached: Dict | None = None
_details_object: dict[str, Any] | None = None
_listing_object: dict[str, Any] | None = None
data_dir: pathlib.Path = pathlib.Path("data/rs/")
ALL_COLUMNS = [
"identifier",
@ -98,15 +99,19 @@ class Listing:
def path_price_history(self) -> pathlib.Path:
return self.path_listing() / "price_history.json"
def dump_listing(self, d: dict):
def dump_listing(self) -> None:
if self._listing_object is None:
raise ValueError("No listing data provided to dump.")
with open(self.path_listing_json(), "w") as f:
json.dump(d, f)
json.dump(self._listing_object, f)
with open(self.path_last_seen_listing(), "w") as f:
dt = datetime.datetime.now().isoformat()
json.dump(dt, f)
# some places list pw in price and others pcm
price = max(d["price"], d.get("monthlyRent", 0))
price = max(
self._listing_object["price"], self._listing_object.get("monthlyRent", 0)
)
self.append_price_history(price)
def append_price_history(self, price: float) -> None:
@ -249,19 +254,18 @@ class Listing:
@property
def listingobject(self):
if self._cached is None:
with open(self.path_listing_json()) as f:
return json.load(f)
with open(self.path_listing_json()) as f:
return json.load(f)
@property
def detailobject(self) -> dict[str, Any]:
if self._cached is None:
if self._details_object is None:
if self.path_detail_json().exists():
with open(self.path_detail_json()) as f:
self._cached = json.load(f)
self._details_object = json.load(f)
else:
return {}
return self._cached # type: ignore
return self._details_object # type: ignore
@property
def price(self) -> float: