diff --git a/crawler/data_access.py b/crawler/data_access.py index 9356f4a..cda1708 100644 --- a/crawler/data_access.py +++ b/crawler/data_access.py @@ -94,6 +94,9 @@ class Listing: def path_last_seen_listing(self) -> pathlib.Path: return self.path_listing() / "last_seen.json" + def path_price_history(self) -> pathlib.Path: + return self.path_listing() / "price_history.json" + def dump_listing(self, d: dict): with open(self.path_listing_json(), "w") as f: json.dump(d, f) @@ -101,6 +104,34 @@ class Listing: 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)) + self.append_price_history(price) + + def append_price_history(self, price: float) -> None: + """Append the price history to the listing's price history file.""" + existing_price_history = ( + json.loads(self.path_price_history().read_text()) + if self.path_price_history().exists() + else [] + ) + now = datetime.datetime.now().isoformat() + # if the last price is the same, just update the date + if len(existing_price_history) > 0: + last_price = existing_price_history[-1]["price"] + if last_price == price: + existing_price_history[-1]["last_seen"] = now + else: + existing_price_history.append( + { + "first_seen": now, + "last_seen": now, + "price": price, + } + ) + with open(self.path_price_history(), "w") as f: + json.dump(existing_price_history, f, indent=4) + def list_floorplans(self): images = list(self.path_floorplan_folder().glob("*")) # todo add check if return is image @@ -341,6 +372,13 @@ class Listing: # If the date format is not as expected, return None return None + @property + def priceHistory(self) -> list[dict[str, Any]]: + if not self.path_price_history().exists(): + return [] + with open(self.path_price_history(), "r") as f: + return json.load(f) + async def dict_nicely(self): travel_time_fastest = {} travel_time_second = {} @@ -395,6 +433,7 @@ class Listing: if self.letDateAvailable else "Ask agent" ), + "price_history": self.priceHistory, } def __routing_cache_key(