74 lines
2.3 KiB
Python
74 lines
2.3 KiB
Python
import json
|
|
import random
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
from typing import Dict, List
|
|
|
|
DATA_DIR = Path(__file__).resolve().parent.parent / "data"
|
|
ITEMS_PER_SECTION = 3
|
|
|
|
|
|
def _load_json(filename: str) -> Dict:
|
|
path = DATA_DIR / filename
|
|
with open(path, encoding="utf-8") as file:
|
|
return json.load(file)
|
|
|
|
|
|
MENU_SOURCE = _load_json("menu_items.json")
|
|
|
|
|
|
def _pick_items(options: List[str], count: int) -> List[str]:
|
|
if count >= len(options):
|
|
return list(options)
|
|
return random.sample(options, count)
|
|
|
|
|
|
def _pick_mains(count: int = ITEMS_PER_SECTION) -> List[str]:
|
|
fish_options = MENU_SOURCE["mains"]["fish"]
|
|
other_options = MENU_SOURCE["mains"]["others"]
|
|
|
|
fish_choice = random.choice(fish_options)
|
|
remaining_needed = max(count - 1, 0)
|
|
|
|
pool = [item for item in fish_options if item != fish_choice] + other_options
|
|
if remaining_needed > len(pool):
|
|
remaining_needed = len(pool)
|
|
|
|
mains = [fish_choice] + _pick_items(pool, remaining_needed)
|
|
random.shuffle(mains)
|
|
return mains
|
|
|
|
def _pick_garnish() -> List[str]:
|
|
garnish_options = MENU_SOURCE["mains"]["garnish"]
|
|
|
|
return _pick_items(garnish_options, 2)
|
|
|
|
|
|
def _build_alternative() -> Dict:
|
|
alternative = MENU_SOURCE.get("alternatives", {})
|
|
return {
|
|
"title": alternative.get("title", "Alternativa"),
|
|
"items": alternative.get("items", []),
|
|
"price": alternative.get("price"),
|
|
"note": alternative.get("note", ""),
|
|
}
|
|
|
|
def build_menu(items_per_section: int = ITEMS_PER_SECTION) -> Dict:
|
|
today = datetime.now()
|
|
|
|
return {
|
|
"day": today.strftime("%A").capitalize(),
|
|
"starters": _pick_items(MENU_SOURCE["starters"], items_per_section),
|
|
"mains": _pick_mains(items_per_section),
|
|
"garnish": _pick_garnish(),
|
|
"desserts": _pick_items(MENU_SOURCE["desserts"], items_per_section),
|
|
"notes": _pick_items(MENU_SOURCE["notes"], 3),
|
|
"menu_price": MENU_SOURCE["university_deal"]["current_price"],
|
|
"university_deal": MENU_SOURCE["university_deal"],
|
|
"espetos_tip": random.choice(MENU_SOURCE["espetos_tips"]),
|
|
"alternative": _build_alternative(),
|
|
"availability": {
|
|
"last_updated": today.isoformat(timespec="seconds"),
|
|
},
|
|
}
|