From 0e083bb23be166b2d9ecb00b81e9fef7c566d347 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Mon, 11 Aug 2025 22:56:02 +0200 Subject: [PATCH] Add csv-based persistence --- main.py | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index dd68118..2dbc00b 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,8 @@ -import json import csv +import json from dataclasses import dataclass +from datetime import datetime +from pathlib import Path from pprint import pprint from typing import Any @@ -175,11 +177,36 @@ def get_lowest_price(prices: list[Price]) -> Price: lowest = p return lowest -CSV_LOWEST_FILE="lowest.csv" -def add_to_csv(price: Price) -> None: - with open(CSV_LOWEST_FILE, "a") as f: + +CSV_LOWEST_FILE = "lowest.csv" +CSV_ALL_PRICES_SUFFIX = "_all_prices.csv" + + +def dump_all_prices_to_csv(prices: list[Price]) -> None: + with open(f"{int(datetime.now().timestamp())}{CSV_ALL_PRICES_SUFFIX}", "w") as f: writer = csv.writer(f) - writer.writerow([price.id, price.price, price.name]) + writer.writerow(["id", "price", "name"]) + writer.writerows([[price.id, price.price, price.name] for price in prices]) + + +def add_to_csv(price: Price) -> None: + if not Path(CSV_LOWEST_FILE).is_file(): + with open(CSV_LOWEST_FILE, "w") as f: + csv.writer(f).writerow(["id", "price", "name"]) + + with open(CSV_LOWEST_FILE, "a") as f: + csv.writer(f).writerow([price.id, price.price, price.name]) + + +def get_last_price_from_csv() -> Price | None: + if not Path(CSV_LOWEST_FILE).is_file(): + return + + with open(CSV_LOWEST_FILE) as f: + last = next(reversed(list(csv.reader(f)))) + return Price(last[0], last[2], float(last[1])) + + def main(): @@ -189,9 +216,18 @@ def main(): bookings = [request_bookings(token, req) for req in booking_requests] prices = extract_prices(bookings) - lowest = get_lowest_price(prices) - add_to_csv(lowest) - # dprint(extract_prices(bookings)) + # create a snapshot of all current prices + dump_all_prices_to_csv(prices) + + # extract the lowest and the last lowest price + new = get_lowest_price(prices) + previous = get_last_price_from_csv() + + # if the price changed, add it to lowest prices + if not previous or new.price != previous.price: + print(f"PRICE CHANGE. {previous} -> {new}") + notify_user(previous or Price("", "", 0.0), new, "alerta-alerta-pichi-133") + add_to_csv(new) if __name__ == "__main__":