number of entries and each entry’s timestamp

main
Shane 7 months ago
parent 2041084962
commit 77743121b5
  1. 180
      foodie_weekly_thread.py

@ -179,21 +179,21 @@ def load_recent_posts():
return posts return posts
def filter_posts_for_week(posts, start_date, end_date): def filter_posts_for_week(posts, start_date, end_date):
"""Filter posts within the specified week.""" """Filter posts within the given week range."""
filtered_posts = [] filtered_posts = []
logging.debug(f"Filtering {len(posts)} posts for range {start_date} to {end_date}")
for post in posts: for post in posts:
try: try:
timestamp = datetime.fromisoformat(post["timestamp"]) post_date = datetime.fromisoformat(post["timestamp"])
logging.debug(f"Checking post '{post['title']}' with timestamp {timestamp}") logging.debug(f"Checking post: title={post['title']}, timestamp={post_date}, in range {start_date} to {end_date}")
if start_date <= timestamp <= end_date: if start_date <= post_date <= end_date:
filtered_posts.append(post) filtered_posts.append(post)
logging.debug(f"Included post: {post['title']}")
else: else:
logging.debug(f"Post '{post['title']}' timestamp {timestamp} outside range") logging.debug(f"Excluded post: {post['title']} (timestamp {post_date} outside range)")
except ValueError as e: except (KeyError, ValueError) as e:
logging.warning(f"Skipping post with invalid timestamp: {post.get('title', 'Unknown')} - {e}") logging.warning(f"Skipping post due to invalid format: {e}")
logging.info(f"Filtered to {len(filtered_posts)} posts within week range") continue
logging.info(f"Filtered to {len(filtered_posts)} posts for the week")
return filtered_posts return filtered_posts
def generate_intro_tweet(author): def generate_intro_tweet(author):
@ -291,127 +291,89 @@ def generate_final_cta(author):
return fallback return fallback
def post_weekly_thread(): def post_weekly_thread():
"""Post weekly threads for each author.""" """Generate and post a weekly thread of top posts for each author."""
try:
logging.info("Starting foodie_weekly_thread.py") logging.info("Starting foodie_weekly_thread.py")
print("Starting foodie_weekly_thread.py")
valid_credentials = validate_twitter_credentials()
if not valid_credentials:
logging.error("No valid Twitter credentials found, exiting")
return
# Calculate date range: 7 days prior to run date
today = datetime.now(timezone.utc) today = datetime.now(timezone.utc)
days_to_monday = today.weekday() start_date = (today - timedelta(days=7)).replace(hour=0, minute=0, second=0, microsecond=0)
start_date = (today - timedelta(days=days_to_monday + 7)).replace(hour=0, minute=0, second=0, microsecond=0) end_date = (today - timedelta(days=1)).replace(hour=23, minute=59, second=59, microsecond=999999)
end_date = start_date + timedelta(days=6, hours=23, minutes=59, seconds=59)
logging.info(f"Fetching posts from {start_date} to {end_date}") logging.info(f"Fetching posts from {start_date} to {end_date}")
print(f"Fetching posts from {start_date} to {end_date}")
all_posts = load_recent_posts()
logging.info(f"Loaded {len(all_posts)} posts from recent_posts.json")
print(f"Loaded {len(all_posts)} posts from recent_posts.json")
if not all_posts:
logging.warning("No posts loaded, exiting post_weekly_thread")
print("No posts loaded, exiting post_weekly_thread")
return
weekly_posts = filter_posts_for_week(all_posts, start_date, end_date)
logging.info(f"Filtered to {len(weekly_posts)} posts for the week")
print(f"Filtered to {len(weekly_posts)} posts for the week")
# Load and filter posts
recent_posts = load_json_file(RECENT_POSTS_FILE)
logging.info(f"Loaded {len(recent_posts)} posts from {RECENT_POSTS_FILE}")
# Deduplicate posts
seen = set()
deduped_posts = []
for post in recent_posts:
key = (post["title"], post["url"], post["author_username"])
if key not in seen:
seen.add(key)
deduped_posts.append(post)
logging.info(f"Filtered to {len(deduped_posts)} unique posts after deduplication")
weekly_posts = filter_posts_for_week(deduped_posts, start_date, end_date)
if not weekly_posts: if not weekly_posts:
logging.warning("No posts found within the week range, exiting post_weekly_thread") logging.warning(f"No posts found within the week range {start_date} to {end_date}, exiting post_weekly_thread")
print("No posts found within the week range, exiting post_weekly_thread")
return return
posts_by_author = {} # Group posts by author
posts_by_author = {author["username"]: [] for author in AUTHORS}
for post in weekly_posts: for post in weekly_posts:
author = post["author_username"] username = post["author_username"]
if author not in posts_by_author: if username in posts_by_author:
posts_by_author[author] = [] posts_by_author[username].append(post)
posts_by_author[author].append(post)
logging.debug(f"Grouped posts by author: {list(posts_by_author.keys())}")
# Post threads for each author
for author in AUTHORS: for author in AUTHORS:
try: username = author["username"]
author_posts = posts_by_author.get(author["username"], []) author_posts = posts_by_author.get(username, [])
logging.info(f"Processing author {author['username']} with {len(author_posts)} posts")
print(f"Processing author {author['username']} with {len(author_posts)} posts")
if not author_posts: if not author_posts:
logging.info(f"No posts found for {author['username']} this week") logging.info(f"No posts found for {username}, skipping")
print(f"No posts found for {author['username']} this week")
continue
author_posts.sort(key=lambda x: x.get("timestamp", ""), reverse=True)
top_posts = author_posts[:10]
logging.info(f"Selected {len(top_posts)} top posts for {author['username']}")
print(f"Selected {len(top_posts)} top posts for {author['username']}")
intro_tweet = generate_intro_tweet(author)
if not intro_tweet:
logging.error(f"Failed to generate intro tweet for {author['username']}, skipping")
continue
logging.info(f"Posting intro tweet for {author['username']}: {intro_tweet}")
print(f"Posting intro tweet for {author['username']}: {intro_tweet}")
intro_response = post_tweet(author, intro_tweet)
if not intro_response:
logging.error(f"Failed to post intro tweet for {author['username']}, skipping thread")
print(f"Failed to post intro tweet for {author['username']}")
continue continue
intro_tweet_id = intro_response.get("id") # Select top 10 posts (or fewer if less than 10)
last_tweet_id = intro_tweet_id author_posts = sorted(author_posts, key=lambda x: datetime.fromisoformat(x["timestamp"]), reverse=True)[:10]
logging.debug(f"Intro tweet posted with ID {intro_tweet_id}") logging.info(f"Selected {len(author_posts)} posts for {username}")
for i, post in enumerate(top_posts, 1):
try:
post_tweet_content = f"{i}. {post['title']} Link: {post['url']}"
logging.info(f"Posting thread reply {i} for {author['username']}: {post_tweet_content}")
print(f"Posting thread reply {i} for {author['username']}: {post_tweet_content}")
reply_response = post_tweet(author, post_tweet_content, reply_to_id=last_tweet_id)
if not reply_response:
logging.error(f"Failed to post thread reply {i} for {author['username']}")
else:
last_tweet_id = reply_response.get("id")
logging.debug(f"Thread reply {i} posted with ID {last_tweet_id}")
except Exception as e:
logging.error(f"Error posting thread reply {i} for {author['username']}: {e}", exc_info=True)
continue
# Post final CTA tweet # Generate and post thread
if last_tweet_id and top_posts: # Ensure there's a valid thread to reply to
try: try:
final_cta = generate_final_cta(author) # Post lead tweet
if not final_cta: lead_tweet = (
logging.error(f"Failed to generate final CTA tweet for {author['username']}, skipping") f"Top foodie finds this week from {author['name']} (@{author['x_username']})! "
f"Check out these {len(author_posts)} posts on InsiderFoodie.com 🍽"
)
lead_response = post_tweet(author, lead_tweet)
if not lead_response:
logging.error(f"Failed to post lead tweet for {username}, skipping")
continue continue
logging.info(f"Posting final CTA tweet for {author['username']}: {final_cta}") lead_tweet_id = lead_response["id"]
print(f"Posting final CTA tweet for {author['username']}: {final_cta}") logging.info(f"Posted lead tweet for {username}: {lead_tweet}")
cta_response = post_tweet(author, final_cta, reply_to_id=last_tweet_id)
if not cta_response: # Post thread tweets
logging.error(f"Failed to post final CTA tweet for {author['username']}") for i, post in enumerate(author_posts, 1):
thread_tweet = (
f"{i}. {post['title']} "
f"Read more: {post['url']} #FoodieThread"
)
thread_response = post_tweet(author, thread_tweet, reply_to_id=lead_tweet_id)
if thread_response:
lead_tweet_id = thread_response["id"]
logging.info(f"Posted thread tweet {i} for {username}: {thread_tweet}")
else: else:
logging.debug(f"Final CTA tweet posted with ID {cta_response.get('id')}") logging.warning(f"Failed to post thread tweet {i} for {username}")
except Exception as e:
logging.error(f"Error posting final CTA tweet for {author['username']}: {e}", exc_info=True)
logging.info(f"Successfully posted weekly thread for {author['username']}") # Post engagement tweet
print(f"Successfully posted weekly thread for {author['username']}") engagement_tweet = generate_engagement_tweet(author)
if engagement_tweet:
post_tweet(author, engagement_tweet, reply_to_id=lead_tweet_id)
logging.info(f"Posted engagement tweet for {username}: {engagement_tweet}")
except Exception as e: except Exception as e:
logging.error(f"Error processing author {author['username']}: {e}", exc_info=True) logging.error(f"Error posting thread for {username}: {e}", exc_info=True)
continue continue
logging.info("Completed foodie_weekly_thread.py")
print("Completed foodie_weekly_thread.py")
except Exception as e:
logging.error(f"Unexpected error in post_weekly_thread: {e}", exc_info=True)
print(f"Error in post_weekly_thread: {e}")
def main(): def main():
"""Main function to run the script.""" """Main function to run the script."""
lock_fd = None lock_fd = None

Loading…
Cancel
Save