diff --git a/foodie_config.py b/foodie_config.py index efa4ab4..a9e471b 100644 --- a/foodie_config.py +++ b/foodie_config.py @@ -3,6 +3,8 @@ from dotenv import load_dotenv import os +ENGAGEMENT_REFERENCE_DATE_FILE = "/home/shane/foodie_automator/engagement_reference_date.json" + load_dotenv() OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") PIXABAY_API_KEY = os.getenv("PIXABAY_API_KEY") diff --git a/foodie_engagement_tweet.py b/foodie_engagement_tweet.py index f8c4235..6360e30 100644 --- a/foodie_engagement_tweet.py +++ b/foodie_engagement_tweet.py @@ -6,16 +6,16 @@ import signal import sys import fcntl import os -from foodie_config import ENGAGEMENT_REFERENCE_DATE_FILE -REFERENCE_DATE_FILE = ENGAGEMENT_REFERENCE_DATE_FILE from datetime import datetime, timedelta, timezone from openai import OpenAI from foodie_utils import post_tweet, AUTHORS, SUMMARY_MODEL, load_post_counts, save_post_counts from foodie_config import X_API_CREDENTIALS, AUTHOR_BACKGROUNDS_FILE from dotenv import load_dotenv +from foodie_config import ENGAGEMENT_REFERENCE_DATE_FILE load_dotenv() +REFERENCE_DATE_FILE = ENGAGEMENT_REFERENCE_DATE_FILE LOCK_FILE = "/home/shane/foodie_automator/locks/foodie_engagement_tweet.lock" LOG_FILE = "/home/shane/foodie_automator/logs/foodie_engagement_tweet.log" REFERENCE_DATE_FILE = "/home/shane/foodie_automator/engagement_reference_date.json" diff --git a/foodie_utils.py b/foodie_utils.py index 5aa211f..3042632 100644 --- a/foodie_utils.py +++ b/foodie_utils.py @@ -52,7 +52,7 @@ def load_json_file(file_path, expiration_hours=None): if not line: continue try: - entry = json.loads(line) + entry = json.loads(line.strip()) data.append(entry) except json.JSONDecodeError as e: logging.warning(f"Skipping invalid JSON line in {file_path} at line {line_number}: {e}") @@ -81,13 +81,14 @@ def load_json_file(file_path, expiration_hours=None): logging.info(f"Loaded {len(valid_entries)} entries from {file_path}, {len(valid_entries)} valid after expiration check") return valid_entries + except Exception as e: logging.error(f"Failed to load JSON file {file_path}: {e}") return [] def save_json_file(file_path, title, timestamp): try: - entries = load_json_file(file_path, 24 if "posted_" in file_path else 7 * 24) # 24 hours for titles, 7 days for images + entries = load_json_file(file_path, 24 if "posted_" in file_path else 7 * 24) entry = {"title": title, "timestamp": timestamp} entries.append(entry) @@ -96,9 +97,15 @@ def save_json_file(file_path, title, timestamp): cutoff = datetime.now(timezone.utc) - timedelta(hours=expiration_hours) pruned_entries = [e for e in entries if datetime.fromisoformat(e["timestamp"]) > cutoff] + # Write as a JSON list with each entry on a new line with open(file_path, 'w') as f: - for entry in pruned_entries: - f.write(json.dumps(entry) + '\n') + f.write('[\n') + for i, entry in enumerate(pruned_entries): + f.write(' ' + json.dumps(entry)) + if i < len(pruned_entries) - 1: + f.write(',') + f.write('\n') + f.write(']') logging.info(f"Saved '{title}' to {file_path}") logging.info(f"Pruned {file_path} to {len(pruned_entries)} entries (older than {expiration_hours//24} days removed)")