update realtime rate limit for X

This commit is contained in:
2025-05-08 13:35:41 +10:00
parent 3405572ab0
commit 167506ef30
7 changed files with 222 additions and 145 deletions
+18 -4
View File
@@ -9,7 +9,7 @@ import os
from datetime import datetime, timezone, timedelta
from openai import OpenAI
from foodie_config import OPENAI_API_KEY, AUTHORS, LIGHT_TASK_MODEL, PERSONA_CONFIGS, AUTHOR_BACKGROUNDS_FILE
from foodie_utils import load_json_file, post_tweet
from foodie_utils import load_json_file, post_tweet, check_author_rate_limit
from dotenv import load_dotenv
load_dotenv()
@@ -99,10 +99,24 @@ def main():
global is_posting
logging.info("***** X Poster Launched *****")
for author in AUTHORS:
# Check if the author can post before generating the tweet
can_post, remaining, reset = check_author_rate_limit(author)
if not can_post:
reset_time = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(reset)) if reset else "Unknown"
logging.info(f"Skipping engagement tweet for {author['username']} due to rate limit. Remaining: {remaining}, Reset at: {reset_time}")
continue
is_posting = True
tweet = generate_engagement_tweet(author, author["persona"])
post_tweet(author, tweet)
is_posting = False
try:
tweet = generate_engagement_tweet(author, author["persona"])
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']}")
except Exception as e:
logging.error(f"Error posting engagement tweet for {author['username']}: {e}", exc_info=True)
finally:
is_posting = False
time.sleep(random.uniform(3600, 7200))
logging.info("X posting completed")