You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
3.7 KiB
76 lines
3.7 KiB
import random |
|
import logging |
|
from datetime import datetime, timedelta, timezone |
|
from openai import OpenAI # Add this import |
|
from foodie_utils import post_tweet, AUTHORS, SUMMARY_MODEL |
|
from dotenv import load_dotenv # Add this import |
|
|
|
# Setup logging |
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
|
|
|
# Load environment variables |
|
load_dotenv() |
|
|
|
# Initialize OpenAI client |
|
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) |
|
|
|
def generate_engagement_tweet(author): |
|
author_handle = author["x_username"] # Updated to use x_username from X_API_CREDENTIALS |
|
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 = client.chat.completions.create( |
|
model=SUMMARY_MODEL, |
|
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 = [ |
|
f"Whats the most mouthwatering dish youve seen this week Share below and follow {author_handle} for more foodie ideas on InsiderFoodie.com Link: https://insiderfoodie.com", |
|
f"Food lovers unite Whats your go to comfort food Tell us and like this tweet for more tasty ideas from {author_handle} on InsiderFoodie.com Link: https://insiderfoodie.com", |
|
f"Ever tried a dish that looked too good to eat Share your favorites and follow {author_handle} for more culinary trends on InsiderFoodie.com Link: https://insiderfoodie.com", |
|
f"What food trend are you loving right now Let us know and like this tweet to keep up with {author_handle} on InsiderFoodie.com Link: https://insiderfoodie.com" |
|
] |
|
template = random.choice(engagement_templates) |
|
return template |
|
|
|
def post_engagement_tweet(): |
|
# Reference date for calculating the 2-day interval |
|
reference_date = datetime(2025, 4, 29, tzinfo=timezone.utc) # Starting from April 29, 2025 |
|
current_date = datetime.now(timezone.utc) |
|
|
|
# Calculate the number of days since the reference date |
|
days_since_reference = (current_date - reference_date).days |
|
|
|
# Post only if the number of days since the reference date is divisible by 2 |
|
if days_since_reference % 2 == 0: |
|
logging.info("Today is an engagement tweet day (every 2 days). Posting...") |
|
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']}") |
|
else: |
|
logging.info("Today is not an engagement tweet day (every 2 days). Skipping...") |
|
|
|
if __name__ == "__main__": |
|
post_engagement_tweet() |