From aeb17d69693dbacc523ad0d7c6ecd6e030a41d50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20=C5=A0trauch?= Date: Wed, 25 Sep 2024 23:43:28 +0200 Subject: [PATCH] Widget with today's and tomorrow's prices --- calculator/index.html | 153 ++++++++++++++++++++++++++++++++++++++++++ calculator/main.py | 24 ++++++- calculator/schema.py | 7 ++ 3 files changed, 181 insertions(+), 3 deletions(-) create mode 100644 calculator/index.html diff --git a/calculator/index.html b/calculator/index.html new file mode 100644 index 0000000..c4cd0d1 --- /dev/null +++ b/calculator/index.html @@ -0,0 +1,153 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
000102030405060708091011121314151617181920212223
Dnes------------------------
Zítra------------------------
+ + + diff --git a/calculator/main.py b/calculator/main.py index e3601d6..3441d6f 100644 --- a/calculator/main.py +++ b/calculator/main.py @@ -3,10 +3,11 @@ import calendar from typing import List, Optional from fastapi import FastAPI, HTTPException -from fastapi.responses import RedirectResponse +from fastapi.responses import RedirectResponse, HTMLResponse +from fastapi.middleware.cors import CORSMiddleware from calculator.miner import PriceNotFound, get_energy_prices, get_eur_czk_ratio -from calculator.schema import CheapestHours, DayPrice, Price +from calculator.schema import CheapestHours, DayPrice, MostExpensiveHours, Price from .consts import VAT @@ -14,6 +15,13 @@ def days_in_month(year: int, month: int) -> int: return calendar.monthrange(year, month)[1] app = FastAPI(title="Spot market home calculator", version="0.1", description="Calculate your energy costs based on spot market prices") +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) @app.get("/", description="Redirect to /docs") def docs(): @@ -31,6 +39,7 @@ Return spot prices for the whole day with all fees included.
**kwh_fees_high** - additional fees per kWh in high tariff, usually distribution + other fees, default is 1.83474 (D57d, BezDodavatele)
**sell_fees** - selling energy fees, default is 0.45 (BezDodavatele)
**num_cheapest_hours** - number of cheapest hours to return, default is 8, use this to plan your consumption
+**num_most_expensive_hours** - number of the most expensive hours to return, default is 8, use this to plan your consumption
**low_tariff_hours** - list of low tariff hours, default is 0,1,2,3,4,5,6,7,9,10,11,13,14,16,17,18,20,21,22,23 (D57d, ČEZ)

Output:
@@ -80,6 +89,7 @@ def read_item( low_tariff_hours:str="0,1,2,3,4,5,6,7,9,10,11,13,14,16,17,18,20,21,22,23", no_cache:bool = False, num_cheapest_hours:int = 8, + num_most_expensive_hours:int = 8, ) -> DayPrice: if not date: @@ -115,6 +125,7 @@ def read_item( spot_for_sell = Price(hours=spot_hours_for_sell, now=spot_hours_for_sell[str(hour)] if is_today else None) cheapest_hours = [int(k) for k, v in list(spot_hours_total_sorted.items())[0:num_cheapest_hours]] + most_expensive_hours = [int(k) for k, v in list(reversed(spot_hours_total_sorted.items()))[0:num_most_expensive_hours]] data = DayPrice( monthly_fees=monthly_fees * VAT, @@ -128,6 +139,13 @@ def read_item( spot=spot, total=spot_total, sell=spot_for_sell, - cheapest_hours=CheapestHours(hours=cheapest_hours, is_cheapest=hour in cheapest_hours if is_today else None) + cheapest_hours=CheapestHours(hours=cheapest_hours, is_cheapest=hour in cheapest_hours if is_today else None), + most_expensive_hours=MostExpensiveHours(hours=most_expensive_hours, is_the_most_expensive=hour in most_expensive_hours if is_today else None), ) return data + +@app.get("/widget", response_class=HTMLResponse) +def get_widget(): + with open("calculator/index.html", "r") as file: + html_content = file.read() + return HTMLResponse(content=html_content) diff --git a/calculator/schema.py b/calculator/schema.py index 13b9807..a158ca3 100644 --- a/calculator/schema.py +++ b/calculator/schema.py @@ -14,6 +14,12 @@ class CheapestHours: is_cheapest: Optional[bool] = None +@dataclass +class MostExpensiveHours: + hours: List[int] + is_the_most_expensive: Optional[bool] = None + + @dataclass class DayPrice: monthly_fees: float @@ -28,6 +34,7 @@ class DayPrice: total: Price sell: Price cheapest_hours: CheapestHours + most_expensive_hours: MostExpensiveHours