Rate Limit Handling
This commit is contained in:
+8
-15
@@ -1324,31 +1324,20 @@ def check_author_rate_limit(author, max_tweets=17, tweet_window_seconds=86400):
|
||||
author_info = rate_limit_info[username]
|
||||
script_run_id = author_info.get('script_run_id', 0)
|
||||
|
||||
# Check if quota has reset based on previous reset time
|
||||
reset = author_info.get('tweet_reset', current_time + tweet_window_seconds)
|
||||
if current_time >= reset:
|
||||
logger.info(f"Quota reset for {username}, restoring to {max_tweets} tweets")
|
||||
author_info['tweet_remaining'] = max_tweets
|
||||
author_info['tweet_reset'] = current_time + tweet_window_seconds
|
||||
author_info['tweets_posted_in_run'] = 0
|
||||
author_info['script_run_id'] = check_author_rate_limit.script_run_id
|
||||
rate_limit_info[username] = author_info
|
||||
save_json_file(rate_limit_file, rate_limit_info)
|
||||
|
||||
# If script restarted or new author, post a test tweet to sync quota
|
||||
if script_run_id != check_author_rate_limit.script_run_id:
|
||||
logger.info(f"Script restart detected for {username}, posting test tweet to sync quota")
|
||||
remaining, api_reset = get_x_rate_limit_status(author)
|
||||
if remaining is None or api_reset is None:
|
||||
# Fallback: Use last known quota or assume 0 remaining
|
||||
if current_time < author_info.get('tweet_reset', 0):
|
||||
if current_time < author_info.get('tweet_reset', current_time + tweet_window_seconds):
|
||||
remaining = author_info.get('tweet_remaining', 0)
|
||||
reset = author_info.get('tweet_reset', current_time + tweet_window_seconds)
|
||||
logger.warning(f"Test tweet failed for {username}, using last known quota: {remaining} remaining")
|
||||
else:
|
||||
remaining = max_tweets
|
||||
remaining = 0 # Assume exhausted if API fails and reset time has passed
|
||||
reset = current_time + tweet_window_seconds
|
||||
logger.warning(f"Test tweet failed for {username}, resetting quota to {max_tweets}")
|
||||
logger.warning(f"Test tweet failed for {username}, assuming quota exhausted")
|
||||
else:
|
||||
remaining = min(remaining, max_tweets) # Ensure within Free tier limit
|
||||
reset = api_reset
|
||||
@@ -1360,9 +1349,13 @@ def check_author_rate_limit(author, max_tweets=17, tweet_window_seconds=86400):
|
||||
author_info['script_run_id'] = check_author_rate_limit.script_run_id
|
||||
rate_limit_info[username] = author_info
|
||||
save_json_file(rate_limit_file, rate_limit_info)
|
||||
else:
|
||||
# Use existing quota without resetting
|
||||
remaining = author_info.get('tweet_remaining', max_tweets)
|
||||
reset = author_info.get('tweet_reset', current_time + tweet_window_seconds)
|
||||
|
||||
# Calculate remaining tweets
|
||||
remaining = author_info['tweet_remaining'] - author_info['tweets_posted_in_run']
|
||||
remaining = remaining - author_info.get('tweets_posted_in_run', 0)
|
||||
|
||||
can_post = remaining > 0
|
||||
if not can_post:
|
||||
|
||||
Reference in New Issue
Block a user