| | import json |
| | import os |
| | from typing import Dict, Any, Optional |
| | import logging |
| |
|
| | |
| | logging.basicConfig(level=logging.INFO) |
| | logger = logging.getLogger(__name__) |
| |
|
| | |
| | _condition_data = {} |
| | _last_load_time = 0 |
| |
|
| | def _load_condition_data() -> Dict[str, Any]: |
| | """Load skin condition data from JSON file with proper error handling""" |
| | json_path = 'data/skin_conditions.json' |
| | |
| | try: |
| | |
| | mtime = os.path.getmtime(json_path) |
| | |
| | |
| | global _condition_data, _last_load_time |
| | if _condition_data and mtime <= _last_load_time: |
| | return _condition_data |
| | |
| | |
| | with open(json_path, encoding='utf-8') as f: |
| | data = json.load(f) |
| | |
| | |
| | condition_map = {} |
| | for entry in data: |
| | condition = entry.get("condition", "").strip().lower() |
| | if condition: |
| | condition_map[condition] = entry |
| | |
| | |
| | _condition_data = condition_map |
| | _last_load_time = mtime |
| | logger.info(f"Loaded {len(condition_map)} skin conditions from {json_path}") |
| | return condition_map |
| | |
| | except FileNotFoundError: |
| | logger.error(f"Skin conditions file not found: {json_path}") |
| | return {} |
| | except json.JSONDecodeError as e: |
| | logger.error(f"Invalid JSON in skin conditions file: {e}") |
| | return {} |
| | except Exception as e: |
| | logger.error(f"Error loading skin conditions: {e}") |
| | return {} |
| |
|
| | def get_recommended_products(condition_query: str) -> Dict[str, Any]: |
| | """ |
| | Get recommended products for a given skin condition. |
| | |
| | Args: |
| | condition_query: The skin condition to look up |
| | |
| | Returns: |
| | Dictionary containing condition info and product recommendations, |
| | or empty dict if no match found |
| | """ |
| | if not condition_query: |
| | return {} |
| | |
| | |
| | condition_query = condition_query.strip().lower() |
| | |
| | |
| | condition_data = _load_condition_data() |
| | |
| | |
| | if condition_query in condition_data: |
| | return condition_data[condition_query] |
| | |
| | |
| | |
| | for cond_name, entry in condition_data.items(): |
| | |
| | if condition_query in cond_name or cond_name in condition_query: |
| | return entry |
| | |
| | |
| | logger.warning(f"No recommendations found for condition: {condition_query}") |
| | return {} |