add lock files and update weekly tweet to include last tweet to follow
This commit is contained in:
+238
-58
@@ -1,83 +1,263 @@
|
||||
import random
|
||||
# foodie_engagement_tweet.py
|
||||
import json
|
||||
import logging
|
||||
import random
|
||||
import signal
|
||||
import sys
|
||||
import fcntl
|
||||
import os
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from openai import OpenAI # Add this import
|
||||
from foodie_utils import post_tweet, AUTHORS, SUMMARY_MODEL
|
||||
from foodie_config import X_API_CREDENTIALS
|
||||
from dotenv import load_dotenv # Add this import
|
||||
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
|
||||
|
||||
# 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"))
|
||||
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"
|
||||
LOG_PRUNE_DAYS = 30
|
||||
MAX_RETRIES = 3
|
||||
RETRY_BACKOFF = 2
|
||||
|
||||
def setup_logging():
|
||||
"""Initialize logging with pruning of old logs."""
|
||||
try:
|
||||
os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True)
|
||||
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.basicConfig(
|
||||
filename=LOG_FILE,
|
||||
level=logging.INFO,
|
||||
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'))
|
||||
logging.getLogger().addHandler(console_handler)
|
||||
logging.getLogger("openai").setLevel(logging.WARNING)
|
||||
logging.info("Logging initialized for foodie_engagement_tweet.py")
|
||||
except Exception as e:
|
||||
print(f"Failed to setup logging: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
def acquire_lock():
|
||||
"""Acquire a lock to prevent concurrent runs."""
|
||||
os.makedirs(os.path.dirname(LOCK_FILE), exist_ok=True)
|
||||
lock_fd = open(LOCK_FILE, 'w')
|
||||
try:
|
||||
fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
lock_fd.write(str(os.getpid()))
|
||||
lock_fd.flush()
|
||||
return lock_fd
|
||||
except IOError:
|
||||
logging.info("Another instance of foodie_engagement_tweet.py is running")
|
||||
sys.exit(0)
|
||||
|
||||
def signal_handler(sig, frame):
|
||||
"""Handle termination signals gracefully."""
|
||||
logging.info("Received termination signal, exiting...")
|
||||
sys.exit(0)
|
||||
|
||||
signal.signal(signal.SIGTERM, signal_handler)
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
|
||||
# Initialize OpenAI client
|
||||
try:
|
||||
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
||||
if not os.getenv("OPENAI_API_KEY"):
|
||||
logging.error("OPENAI_API_KEY is not set in environment variables")
|
||||
raise ValueError("OPENAI_API_KEY is required")
|
||||
except Exception as e:
|
||||
logging.error(f"Failed to initialize OpenAI client: {e}", exc_info=True)
|
||||
sys.exit(1)
|
||||
|
||||
# Load author backgrounds
|
||||
try:
|
||||
with open(AUTHOR_BACKGROUNDS_FILE, 'r') as f:
|
||||
AUTHOR_BACKGROUNDS = json.load(f)
|
||||
except Exception as e:
|
||||
logging.error(f"Failed to load author_backgrounds.json: {e}", exc_info=True)
|
||||
sys.exit(1)
|
||||
|
||||
def get_reference_date():
|
||||
"""Load or initialize the reference date for the 2-day interval."""
|
||||
os.makedirs(os.path.dirname(REFERENCE_DATE_FILE), exist_ok=True)
|
||||
if os.path.exists(REFERENCE_DATE_FILE):
|
||||
try:
|
||||
with open(REFERENCE_DATE_FILE, 'r') as f:
|
||||
data = json.load(f)
|
||||
reference_date = datetime.fromisoformat(data["reference_date"]).replace(tzinfo=timezone.utc)
|
||||
logging.info(f"Loaded reference date: {reference_date.date()}")
|
||||
return reference_date
|
||||
except (json.JSONDecodeError, KeyError, ValueError) as e:
|
||||
logging.error(f"Failed to load reference date from {REFERENCE_DATE_FILE}: {e}. Initializing new date.")
|
||||
|
||||
# Initialize with current date (start of day)
|
||||
reference_date = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
try:
|
||||
with open(REFERENCE_DATE_FILE, 'w') as f:
|
||||
json.dump({"reference_date": reference_date.isoformat()}, f)
|
||||
logging.info(f"Initialized reference date: {reference_date.date()}")
|
||||
except Exception as e:
|
||||
logging.error(f"Failed to save reference date to {REFERENCE_DATE_FILE}: {e}. Using current date.")
|
||||
return reference_date
|
||||
|
||||
def generate_engagement_tweet(author):
|
||||
# Fetch x_username from X_API_CREDENTIALS
|
||||
"""Generate an engagement tweet using author background themes."""
|
||||
credentials = next((cred for cred in X_API_CREDENTIALS if cred["username"] == author["username"]), None)
|
||||
if not credentials:
|
||||
logging.error(f"No X credentials found for {author['username']}")
|
||||
return None
|
||||
author_handle = credentials["x_username"]
|
||||
|
||||
background = next((bg for bg in AUTHOR_BACKGROUNDS if bg["username"] == author["username"]), {})
|
||||
if not background or "engagement_themes" not in background:
|
||||
logging.warning(f"No background or engagement themes found for {author['username']}")
|
||||
theme = "food trends"
|
||||
else:
|
||||
theme = random.choice(background["engagement_themes"])
|
||||
|
||||
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"Create an engaging question or statement about {theme} 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
|
||||
for attempt in range(MAX_RETRIES):
|
||||
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] + "..."
|
||||
logging.debug(f"Generated engagement tweet: {tweet}")
|
||||
return tweet
|
||||
except Exception as e:
|
||||
logging.warning(f"Failed to generate engagement tweet for {author['username']} (attempt {attempt + 1}): {e}")
|
||||
if attempt < MAX_RETRIES - 1:
|
||||
time.sleep(RETRY_BACKOFF * (2 ** attempt))
|
||||
else:
|
||||
logging.error(f"Failed to generate engagement tweet after {MAX_RETRIES} attempts")
|
||||
engagement_templates = [
|
||||
f"What's the most mouthwatering {theme} you've seen this week? Share below and follow {author_handle} for more on InsiderFoodie.com! Link: https://insiderfoodie.com",
|
||||
f"{theme.capitalize()} lovers unite! What's your go-to pick? Tell us and like this tweet for more from {author_handle} on InsiderFoodie.com! Link: https://insiderfoodie.com",
|
||||
f"Ever tried a {theme} that blew your mind? Share your favorites and follow {author_handle} for more on InsiderFoodie.com! Link: https://insiderfoodie.com",
|
||||
f"What {theme} 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)
|
||||
logging.info(f"Using fallback engagement tweet: {template}")
|
||||
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)
|
||||
"""Post engagement tweets for authors every 2 days."""
|
||||
try:
|
||||
logging.info("Starting foodie_engagement_tweet.py")
|
||||
print("Starting foodie_engagement_tweet.py")
|
||||
|
||||
# Get reference date
|
||||
reference_date = get_reference_date()
|
||||
current_date = datetime.now(timezone.utc)
|
||||
days_since_reference = (current_date - reference_date).days
|
||||
logging.info(f"Days since reference date ({reference_date.date()}): {days_since_reference}")
|
||||
print(f"Days since reference date ({reference_date.date()}): {days_since_reference}")
|
||||
|
||||
# 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...")
|
||||
print("Today is an engagement tweet day (every 2 days). Posting...")
|
||||
|
||||
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...")
|
||||
# Load post counts to check limits
|
||||
post_counts = load_post_counts()
|
||||
|
||||
for author in AUTHORS:
|
||||
try:
|
||||
# Check post limits
|
||||
author_count = next((entry for entry in post_counts if entry["username"] == author["username"]), None)
|
||||
if not author_count:
|
||||
logging.error(f"No post count entry for {author['username']}, skipping")
|
||||
continue
|
||||
if author_count["monthly_count"] >= 500:
|
||||
logging.warning(f"Monthly post limit (500) reached for {author['username']}, skipping")
|
||||
continue
|
||||
if author_count["daily_count"] >= 20:
|
||||
logging.warning(f"Daily post limit (20) reached for {author['username']}, skipping")
|
||||
continue
|
||||
|
||||
tweet = generate_engagement_tweet(author)
|
||||
if not tweet:
|
||||
logging.error(f"Failed to generate engagement tweet for {author['username']}, skipping")
|
||||
continue
|
||||
|
||||
logging.info(f"Posting engagement tweet for {author['username']}: {tweet}")
|
||||
print(f"Posting engagement tweet for {author['username']}: {tweet}")
|
||||
if post_tweet(author, tweet):
|
||||
logging.info(f"Successfully posted engagement tweet for {author['username']}")
|
||||
# Update post counts
|
||||
author_count["monthly_count"] += 1
|
||||
author_count["daily_count"] += 1
|
||||
save_post_counts(post_counts)
|
||||
else:
|
||||
logging.warning(f"Failed to post engagement tweet for {author['username']}")
|
||||
except Exception as e:
|
||||
logging.error(f"Error posting engagement tweet for {author['username']}: {e}", exc_info=True)
|
||||
continue
|
||||
else:
|
||||
logging.info(f"Today is not an engagement tweet day (every 2 days). Days since reference: {days_since_reference}. Skipping...")
|
||||
print(f"Today is not an engagement tweet day (every 2 days). Days since reference: {days_since_reference}. Skipping...")
|
||||
|
||||
logging.info("Completed foodie_engagement_tweet.py")
|
||||
print("Completed foodie_engagement_tweet.py")
|
||||
except Exception as e:
|
||||
logging.error(f"Unexpected error in post_engagement_tweet: {e}", exc_info=True)
|
||||
print(f"Error in post_engagement_tweet: {e}")
|
||||
|
||||
def main():
|
||||
"""Main function to run the script."""
|
||||
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)
|
||||
print(f"Fatal error: {e}")
|
||||
sys.exit(1)
|
||||
finally:
|
||||
if lock_fd:
|
||||
fcntl.flock(lock_fd, fcntl.LOCK_UN)
|
||||
lock_fd.close()
|
||||
os.remove(LOCK_FILE) if os.path.exists(LOCK_FILE) else None
|
||||
|
||||
if __name__ == "__main__":
|
||||
post_engagement_tweet()
|
||||
main()
|
||||
Reference in New Issue
Block a user