import random import logging from datetime import datetime import openai from foodie_utils import post_tweet, AUTHORS # Setup logging logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') def generate_engagement_tweet(author): author_handle = author["handle"] prompt = ( f"Generate a concise tweet (under 280 characters) for {author_handle}. " f"Create an engaging food-related question or statement to spark interaction. " f"Include a call to action to follow {author_handle} or like the tweet, and mention InsiderFoodie.com with a link to https://insiderfoodie.com. " f"Avoid using the word 'elevate'—use more humanized language like 'level up' or 'bring to life'. " f"Do not include emojis, hashtags, or reward-driven incentives (e.g., giveaways)." ) try: response = openai.ChatCompletion.create( model="gpt-4o", messages=[ {"role": "system", "content": "You are a social media expert crafting engaging tweets."}, {"role": "user", "content": prompt} ], max_tokens=100, temperature=0.7 ) tweet = response.choices[0].message.content.strip() if len(tweet) > 280: tweet = tweet[:277] + "..." return tweet except Exception as e: logging.warning(f"Failed to generate engagement tweet for {author['username']}: {e}") # Fallback templates engagement_templates = [ "Whats the most mouthwatering dish youve seen this week Share below and follow {handle} for more foodie ideas on InsiderFoodie.com Link: https://insiderfoodie.com", "Food lovers unite Whats your go to comfort food Tell us and like this tweet for more tasty ideas from {handle} on InsiderFoodie.com Link: https://insiderfoodie.com", "Ever tried a dish that looked too good to eat Share your favorites and follow {handle} for more culinary trends on InsiderFoodie.com Link: https://insiderfoodie.com", "What food trend are you loving right now Let us know and like this tweet to keep up with {handle} on InsiderFoodie.com Link: https://insiderfoodie.com" ] template = random.choice(engagement_templates) return template.format(handle=author_handle) def post_engagement_tweet(): for author in AUTHORS: tweet = generate_engagement_tweet(author) logging.info(f"Posting engagement tweet for {author['username']}: {tweet}") if post_tweet(author, tweet): logging.info(f"Successfully posted engagement tweet for {author['username']}") else: logging.warning(f"Failed to post engagement tweet for {author['username']}") if __name__ == "__main__": # Run only on Mondays if datetime.now(timezone.utc).weekday() == 0: # Monday (0 = Monday) post_engagement_tweet() else: logging.info("Not Monday - skipping engagement tweet posting")