update tweet post every 2 days
This commit is contained in:
+24
-16
@@ -1,8 +1,8 @@
|
|||||||
import random
|
import random
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime
|
from datetime import datetime, timedelta
|
||||||
import openai
|
import openai
|
||||||
from foodie_utils import post_tweet, AUTHORS
|
from foodie_utils import post_tweet, AUTHORS, SUMMARY_MODEL
|
||||||
|
|
||||||
# Setup logging
|
# Setup logging
|
||||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||||
@@ -19,7 +19,7 @@ def generate_engagement_tweet(author):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
response = openai.ChatCompletion.create(
|
response = openai.ChatCompletion.create(
|
||||||
model="gpt-4o",
|
model=SUMMARY_MODEL,
|
||||||
messages=[
|
messages=[
|
||||||
{"role": "system", "content": "You are a social media expert crafting engaging tweets."},
|
{"role": "system", "content": "You are a social media expert crafting engaging tweets."},
|
||||||
{"role": "user", "content": prompt}
|
{"role": "user", "content": prompt}
|
||||||
@@ -44,18 +44,26 @@ def generate_engagement_tweet(author):
|
|||||||
return template.format(handle=author_handle)
|
return template.format(handle=author_handle)
|
||||||
|
|
||||||
def post_engagement_tweet():
|
def post_engagement_tweet():
|
||||||
for author in AUTHORS:
|
# Reference date for calculating the 2-day interval
|
||||||
tweet = generate_engagement_tweet(author)
|
reference_date = datetime(2025, 4, 29, tzinfo=timezone.utc) # Starting from April 29, 2025
|
||||||
|
current_date = datetime.now(timezone.utc)
|
||||||
logging.info(f"Posting engagement tweet for {author['username']}: {tweet}")
|
|
||||||
if post_tweet(author, tweet):
|
# Calculate the number of days since the reference date
|
||||||
logging.info(f"Successfully posted engagement tweet for {author['username']}")
|
days_since_reference = (current_date - reference_date).days
|
||||||
else:
|
|
||||||
logging.warning(f"Failed to post engagement tweet for {author['username']}")
|
# 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__":
|
if __name__ == "__main__":
|
||||||
# Run only on Mondays
|
post_engagement_tweet()
|
||||||
if datetime.now(timezone.utc).weekday() == 0: # Monday (0 = Monday)
|
|
||||||
post_engagement_tweet()
|
|
||||||
else:
|
|
||||||
logging.info("Not Monday - skipping engagement tweet posting")
|
|
||||||
@@ -3,7 +3,7 @@ from datetime import datetime, timedelta
|
|||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
import openai
|
import openai
|
||||||
from foodie_utils import post_tweet, AUTHORS
|
from foodie_utils import post_tweet, AUTHORS, SUMMARY_MODEL
|
||||||
|
|
||||||
# Setup logging
|
# Setup logging
|
||||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||||||
@@ -46,7 +46,7 @@ def generate_intro_tweet(author):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
response = openai.ChatCompletion.create(
|
response = openai.ChatCompletion.create(
|
||||||
model="gpt-4o",
|
model=SUMMARY_MODEL,
|
||||||
messages=[
|
messages=[
|
||||||
{"role": "system", "content": "You are a social media expert crafting engaging tweets."},
|
{"role": "system", "content": "You are a social media expert crafting engaging tweets."},
|
||||||
{"role": "user", "content": prompt}
|
{"role": "user", "content": prompt}
|
||||||
|
|||||||
@@ -1,55 +0,0 @@
|
|||||||
import json
|
|
||||||
import logging
|
|
||||||
from openai import OpenAI
|
|
||||||
from foodie_config import OPENAI_API_KEY, AUTHORS, LIGHT_TASK_MODEL
|
|
||||||
from datetime import datetime, timezone
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
|
||||||
|
|
||||||
client = OpenAI(api_key=OPENAI_API_KEY)
|
|
||||||
|
|
||||||
def generate_background(author):
|
|
||||||
# Include the DOB in the prompt to contextualize the timeline
|
|
||||||
dob = author.get('dob', '1980-01-01') # Fallback DOB if not provided
|
|
||||||
current_year = datetime.now().year
|
|
||||||
birth_year = int(dob.split('-')[0])
|
|
||||||
age = current_year - birth_year # Calculate approximate age
|
|
||||||
|
|
||||||
prompt = (
|
|
||||||
f"Generate a fictional background for a food writer named {author['username']} with the persona '{author['persona']}'. "
|
|
||||||
f"They were born on {dob} and are currently {age} years old. Use this to create a realistic timeline for their life events. "
|
|
||||||
"Include: hometown, cultural influences, an early food memory (from their childhood, appropriate for their birth year), "
|
|
||||||
"and how they became a food writer (considering their age and career timeline). "
|
|
||||||
f"Match the tone of their bio: '{author['bio']}'. Keep it concise (100-150 words). "
|
|
||||||
"Return as JSON: {'username': '...', 'hometown': '...', 'cultural_influences': '...', 'early_memory': '...', 'career_path': '...'}"
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
response = client.chat.completions.create(
|
|
||||||
model=LIGHT_TASK_MODEL,
|
|
||||||
messages=[
|
|
||||||
{"role": "system", "content": prompt},
|
|
||||||
{"role": "user", "content": f"Generate background for {author['username']}."}
|
|
||||||
],
|
|
||||||
max_tokens=200
|
|
||||||
)
|
|
||||||
raw_result = response.choices[0].message.content.strip()
|
|
||||||
cleaned_result = raw_result.replace('```json', '').replace('```', '').strip()
|
|
||||||
return json.loads(cleaned_result)
|
|
||||||
except Exception as e:
|
|
||||||
logging.error(f"Failed to generate background for {author['username']}: {e}")
|
|
||||||
return None
|
|
||||||
|
|
||||||
def main():
|
|
||||||
backgrounds = []
|
|
||||||
for author in AUTHORS:
|
|
||||||
background = generate_background(author)
|
|
||||||
if background:
|
|
||||||
backgrounds.append(background)
|
|
||||||
logging.info(f"Generated background for {author['username']}")
|
|
||||||
|
|
||||||
with open('/home/shane/foodie_automator/author_backgrounds.json', 'w') as f:
|
|
||||||
json.dump(backgrounds, f, indent=2)
|
|
||||||
logging.info("Saved backgrounds to author_backgrounds.json")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
Reference in New Issue
Block a user