From ebef8f35d5a0f3e47901dafc1b6f666bd16608b3 Mon Sep 17 00:00:00 2001 From: Shane Date: Wed, 7 May 2025 13:26:22 +1000 Subject: [PATCH] try --- foodie_engagement_tweet.py | 113 ++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 59 deletions(-) diff --git a/foodie_engagement_tweet.py b/foodie_engagement_tweet.py index 607c6d0..8b6b7df 100644 --- a/foodie_engagement_tweet.py +++ b/foodie_engagement_tweet.py @@ -21,6 +21,7 @@ from dotenv import load_dotenv load_dotenv() +# Define constants 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" @@ -31,64 +32,59 @@ URL = "https://insiderfoodie.com" URL_SHORTENED_LENGTH = 23 # Twitter's shortened URL length CURRENT_YEAR = "2025" # Explicitly set the current year for the prompt -def setup_logging(): - """Initialize logging with pruning of old logs.""" - try: - # Ensure the logs directory exists - os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True) - - # Configure logging first - logging.basicConfig( - filename=LOG_FILE, - level=logging.DEBUG, - format='%(asctime)s - %(levelname)s - %(message)s', - datefmt='%Y-%m-%d %H:%M:%S' - ) - console_handler = logging.StreamHandler() - console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) - console_handler.setLevel(logging.DEBUG) # Show DEBUG messages in console too - logging.getLogger().addHandler(console_handler) - logging.getLogger("openai").setLevel(logging.WARNING) - logging.getLogger("tweepy").setLevel(logging.WARNING) - - # Now that logging is configured, prune old logs if the file exists - if os.path.exists(LOG_FILE): - with open(LOG_FILE, 'r') as f: - lines = f.readlines() - cutoff = datetime.now(timezone.utc) - timedelta(days=LOG_PRUNE_DAYS) - pruned_lines = [] - malformed_count = 0 - for line in lines: - if len(line) < 19 or not line[:19].replace('-', '').replace(':', '').replace(' ', '').isdigit(): - malformed_count += 1 - continue - try: - timestamp = datetime.strptime(line[:19], '%Y-%m-%d %H:%M:%S').replace(tzinfo=timezone.utc) - if timestamp > cutoff: - pruned_lines.append(line) - except ValueError: - malformed_count += 1 - continue - if malformed_count > 0: - logging.info(f"Skipped {malformed_count} malformed log lines during pruning") - with open(LOG_FILE, 'w') as f: - f.writelines(pruned_lines) - - logging.info("Logging initialized for foodie_engagement_tweet.py") - except Exception as e: - # Fallback to console-only logging if file logging fails - logging.basicConfig( - level=logging.DEBUG, - format='%(asctime)s - %(levelname)s - %(message)s', - datefmt='%Y-%m-%d %H:%M:%S' - ) - console_handler = logging.StreamHandler() - console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) - console_handler.setLevel(logging.DEBUG) - logging.getLogger().addHandler(console_handler) - logging.getLogger("openai").setLevel(logging.WARNING) - logging.getLogger("tweepy").setLevel(logging.WARNING) - logging.error(f"Failed to setup file logging to {LOG_FILE}: {e}. Falling back to console-only logging.") +# Setup logging at the very start +logger = logging.getLogger() +logger.setLevel(logging.DEBUG) + +# Clear any existing handlers (in case another script configured the logger) +logger.handlers = [] + +# Console handler +console_handler = logging.StreamHandler() +console_handler.setLevel(logging.DEBUG) +console_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')) +logger.addHandler(console_handler) + +# File handler +try: + os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True) + file_handler = logging.FileHandler(LOG_FILE, mode='a') + file_handler.setLevel(logging.DEBUG) + file_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')) + logger.addHandler(file_handler) + logging.info("File logging initialized successfully.") +except Exception as e: + logging.error(f"Failed to setup file logging to {LOG_FILE}: {e}. Continuing with console-only logging.") + +# Prune old logs if the file exists +try: + if os.path.exists(LOG_FILE): + with open(LOG_FILE, 'r') as f: + lines = f.readlines() + cutoff = datetime.now(timezone.utc) - timedelta(days=LOG_PRUNE_DAYS) + pruned_lines = [] + malformed_count = 0 + for line in lines: + if len(line) < 19 or not line[:19].replace('-', '').replace(':', '').replace(' ', '').isdigit(): + malformed_count += 1 + continue + try: + timestamp = datetime.strptime(line[:19], '%Y-%m-%d %H:%M:%S').replace(tzinfo=timezone.utc) + if timestamp > cutoff: + pruned_lines.append(line) + except ValueError: + malformed_count += 1 + continue + if malformed_count > 0: + logging.info(f"Skipped {malformed_count} malformed log lines during pruning") + with open(LOG_FILE, 'w') as f: + f.writelines(pruned_lines) +except Exception as e: + logging.warning(f"Failed to prune log file {LOG_FILE}: {e}") + +logging.info("Logging initialized for foodie_engagement_tweet.py") +logging.getLogger("openai").setLevel(logging.WARNING) +logging.getLogger("tweepy").setLevel(logging.WARNING) def acquire_lock(): """Acquire a lock to prevent concurrent runs.""" @@ -434,7 +430,6 @@ def main(): lock_fd = None try: lock_fd = acquire_lock() - setup_logging() post_engagement_tweet() except Exception as e: logging.error(f"Fatal error in main: {e}", exc_info=True)