diff --git a/foodie_utils.py b/foodie_utils.py index 22ed2be..2a1eb06 100644 --- a/foodie_utils.py +++ b/foodie_utils.py @@ -45,13 +45,27 @@ IMAGE_UPLOAD_TIMEOUT = 30 # Added to fix NameError IMAGE_EXPIRATION_DAYS = 7 # 7 days, consistent with foodie_automator_rss.py def load_json_file(file_path, expiration_hours=None, default=None): + """ + Load JSON file, handling specific cases for author_state.json and other files. + Args: + file_path (str): Path to the JSON file. + expiration_hours (float): Hours after which entries expire (for list-based files). + default: Default value to return if file is missing or invalid. + Returns: + Loaded data or default value. + """ logger = logging.getLogger(__name__) + + # Set default based on file type if default is None: - if "rate_limit_info" in file_path or "notification_tracking" in file_path: + if "author_state" in file_path: + default = {"last_author_index": -1} + elif "rate_limit_info" in file_path or "notification_tracking" in file_path: default = {} else: default = [] + # Return default if file doesn't exist if not os.path.exists(file_path): logger.info(f"File {file_path} does not exist. Returning default: {default}") return default @@ -60,18 +74,29 @@ def load_json_file(file_path, expiration_hours=None, default=None): with open(file_path, 'r') as f: data = json.load(f) - # Handle rate_limit_info.json and notification_tracking.json as dicts + # Handle author_state.json (expects dict with last_author_index) + if "author_state" in file_path: + if not isinstance(data, dict): + logger.warning(f"Data in {file_path} is not a dictionary, resetting to default") + return default + if "last_author_index" not in data: + logger.warning(f"Missing last_author_index in {file_path}, resetting to default") + return default + return data + + # Handle rate_limit_info.json and notification_tracking.json (expect dict) if "rate_limit_info" in file_path or "notification_tracking" in file_path: if not isinstance(data, dict): logger.warning(f"Data in {file_path} is not a dictionary, resetting to default") return default return data - # For other files, expect a list + # Handle list-based files if not isinstance(data, list): logger.warning(f"Data in {file_path} is not a list, resetting to default") return default + # Apply expiration filtering for list-based files if expiration_hours is not None: # Use days for used_images.json, hours for others if "used_images" in file_path: