import json import os from pathlib import Path from typing import List, Dict from datetime import datetime MEAL_FILE = "meal_tracker.json" def save_preferences(preferences: Dict) -> str: with open('user_preferences.json', 'w') as f: json.dump(preferences, f, indent=4) return "Preferences saved successfully!" def load_preferences() -> Dict: if os.path.exists('user_preferences.json'): with open('user_preferences.json', 'r') as f: return json.load(f) return { "physical_info": {"height": 170, "weight": 70.0, "age": 30}, "focus_areas": {"custom": "", "selected": []}, "diet_preferences": {"custom": "", "selected": []} } def load_meals(): """Load meals from the JSON file.""" if Path(MEAL_FILE).exists(): with open(MEAL_FILE, 'r') as f: return json.load(f) return [] def save_meal_to_database(new_meal): """Save a new meal to the JSON file, maintaining chronological order.""" meals = load_meals() meals.append(new_meal) meals.sort(key=lambda x: datetime.strptime(x['date'], "%Y-%m-%d %H:%M:%S")) with open(MEAL_FILE, 'w') as f: json.dump(meals, f, indent=2) def get_meals_markdown(meals): """Convert meals to a markdown string for display.""" if not meals: return "No meals recorded yet." markdown = "## Recorded Meals\n\n" for meal in meals: markdown += f"**Date:** {meal['date']}\n" markdown += f"**Type:** {meal['meal_type']}\n" markdown += f"**Description:** {meal['description']}\n" if meal['tags']: markdown += f"**Tags:** {', '.join(meal['tags'])}\n" markdown += "\n---\n\n" return markdown def save_meals(meals): """Save all meals to the JSON file.""" with open(MEAL_FILE, 'w') as f: json.dump(meals, f, indent=2) def get_macros_for_diet(diet_name: str) -> Dict[str, int]: """ Return macronutrient ratios for a given diet """ # You can expand this function with more diet types and their corresponding macros macros = { "Balanced Diet": {"carbs": 50, "protein": 20, "fat": 30}, "Zone Diet": {"carbs": 40, "protein": 30, "fat": 30}, "Ketogenic Diet (Keto)": {"carbs": 75, "protein": 20, "fat": 5}, "Vegan Diet": {"carbs": 58, "protein": 15, "fat": 27}, "Mediterranean Diet": {"carbs": 50, "protein": 17, "fat": 33}, "Paleo Diet": {"carbs": 30, "protein": 30, "fat": 40}, "Low-Carb Diet": {"carbs": 20, "protein": 35, "fat": 45}, "High-Protein Diet": {"carbs": 25, "protein": 35, "fat": 40}, "Vegetarian Diet": {"carbs": 55, "protein": 20, "fat": 25} } return macros.get(diet_name, {"carbs": 50, "protein": 20, "fat": 30}) # Default balanced diet