Shane 7 months ago
parent efa14d21a2
commit 49e7311abd
  1. 27
      foodie_engagement_tweet.py

@ -56,7 +56,7 @@ def setup_logging():
logging.basicConfig(
filename=LOG_FILE,
level=logging.INFO,
level=logging.DEBUG, # Changed to DEBUG to show more details
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
@ -103,9 +103,20 @@ except Exception as e:
# Load author backgrounds
try:
if not os.path.exists(AUTHOR_BACKGROUNDS_FILE):
logging.error(f"Author backgrounds file not found at {AUTHOR_BACKGROUNDS_FILE}")
raise FileNotFoundError(f"Author backgrounds file not found at {AUTHOR_BACKGROUNDS_FILE}")
with open(AUTHOR_BACKGROUNDS_FILE, 'r') as f:
AUTHOR_BACKGROUNDS = json.load(f)
logging.debug(f"Loaded author backgrounds: {[bg['username'] for bg in AUTHOR_BACKGROUNDS]}")
if not isinstance(AUTHOR_BACKGROUNDS, list):
logging.error(f"Invalid format in {AUTHOR_BACKGROUNDS_FILE}: Expected a list, got {type(AUTHOR_BACKGROUNDS)}")
raise ValueError("Author backgrounds must be a list")
for bg in AUTHOR_BACKGROUNDS:
if "username" not in bg:
logging.error(f"Invalid entry in {AUTHOR_BACKGROUNDS_FILE}: Missing 'username' key in {bg}")
raise ValueError("Each author background must have a 'username' key")
loaded_usernames = [bg["username"] for bg in AUTHOR_BACKGROUNDS]
logging.debug(f"Loaded author backgrounds: {loaded_usernames}")
except Exception as e:
logging.error(f"Failed to load author_backgrounds.json: {e}", exc_info=True)
AUTHOR_BACKGROUNDS = []
@ -172,13 +183,19 @@ def generate_engagement_tweet(author):
persona = author["persona"]
persona_config = PERSONA_CONFIGS.get(persona, PERSONA_CONFIGS["Visionary Editor"])
# Case-insensitive lookup for background
# Case-insensitive lookup for background with whitespace stripping
username_cleaned = username.strip().lower()
background = next(
(bg for bg in AUTHOR_BACKGROUNDS if bg["username"].lower() == username.lower()),
(bg for bg in AUTHOR_BACKGROUNDS if bg["username"].strip().lower() == username_cleaned),
{}
)
if not background or "engagement_themes" not in background:
logging.warning(f"No background or engagement themes found for {username}, using default theme")
available_usernames = [bg["username"] for bg in AUTHOR_BACKGROUNDS]
logging.warning(
f"No background or engagement themes found for {username}. "
f"Attempted username (cleaned): {username_cleaned}. "
f"Available usernames: {available_usernames}. Using default theme."
)
theme = "food trends"
else:
theme = random.choice(background["engagement_themes"])

Loading…
Cancel
Save