Init
This commit is contained in:
34
backend/app/services/prices.py
Normal file
34
backend/app/services/prices.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import json
|
||||
import random
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Dict
|
||||
|
||||
DATA_DIR = Path(__file__).resolve().parent.parent / "data"
|
||||
|
||||
|
||||
def _load_json(filename: str) -> Dict:
|
||||
path = DATA_DIR / filename
|
||||
with open(path, encoding="utf-8") as file:
|
||||
return json.load(file)
|
||||
|
||||
|
||||
PRICE_RANGES = _load_json("price_ranges.json")["items"]
|
||||
|
||||
|
||||
def random_price(item: str) -> Dict:
|
||||
low, high = PRICE_RANGES.get(item, (1.0, 3.0))
|
||||
price = min(round(random.uniform(low, high), 2), 3.0)
|
||||
return {
|
||||
"item": item,
|
||||
"price": price,
|
||||
"currency": "EUR",
|
||||
"generated_at": datetime.now().isoformat(timespec="seconds"),
|
||||
}
|
||||
|
||||
|
||||
def prices_payload() -> Dict:
|
||||
return {
|
||||
"items": [random_price(item) for item in PRICE_RANGES.keys()],
|
||||
"disclaimer": "Depende de como pilles al de cafete.",
|
||||
}
|
||||
Reference in New Issue
Block a user