This commit is contained in:
2025-05-08 17:31:29 +10:00
parent ae194b502f
commit 82f4a1d8b1
7 changed files with 294 additions and 175 deletions
+15 -24
View File
@@ -99,28 +99,23 @@ def validate_twitter_credentials():
logging.info("Validating Twitter API credentials for all authors")
valid_credentials = []
for author in AUTHORS:
credentials = X_API_CREDENTIALS.get(author["username"])
if not credentials:
logging.error(f"No X credentials found for {author['username']} in X_API_CREDENTIALS")
continue
for attempt in range(MAX_RETRIES):
try:
twitter_client = tweepy.Client(
consumer_key=credentials["api_key"],
consumer_secret=credentials["api_secret"],
access_token=credentials["access_token"],
access_token_secret=credentials["access_token_secret"]
)
user = twitter_client.get_me()
logging.info(f"Credentials valid for {author['username']} (handle: {credentials['x_username']})")
valid_credentials.append(credentials)
break
except tweepy.TweepyException as e:
remaining, reset = get_x_rate_limit_status(author)
if remaining is not None and reset is not None:
logging.info(f"Credentials valid for {author['username']} (handle: {X_API_CREDENTIALS[author['username']]['x_username']})")
valid_credentials.append(X_API_CREDENTIALS[author['username']])
break
else:
logging.error(f"Rate limit check failed for {author['username']} (attempt {attempt + 1})")
if attempt < MAX_RETRIES - 1:
time.sleep(RETRY_BACKOFF * (2 ** attempt))
except Exception as e:
logging.error(f"Failed to validate credentials for {author['username']} (attempt {attempt + 1}): {e}")
if attempt < MAX_RETRIES - 1:
time.sleep(RETRY_BACKOFF * (2 ** attempt))
else:
logging.error(f"Credentials invalid for {author['username']} after {MAX_RETRIES} attempts")
else:
logging.error(f"Credentials invalid for {author['username']} after {MAX_RETRIES} attempts")
if not valid_credentials:
logging.error("No valid Twitter credentials found for any author")
raise ValueError("No valid Twitter credentials found")
@@ -319,13 +314,9 @@ def post_weekly_thread():
continue
# Check if the author can post before generating the thread
if check_author_rate_limit(author):
reset_time = datetime.fromtimestamp(
load_json_file('/home/shane/foodie_automator/rate_limit_info.json', default={})
.get(username, {})
.get('tweet_reset', time.time()),
tz=timezone.utc
).strftime('%Y-%m-%d %H:%M:%S')
can_post, remaining, reset = check_author_rate_limit(author)
if not can_post:
reset_time = datetime.fromtimestamp(reset, tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S') if reset else "Unknown"
logging.info(f"Skipping weekly thread for {username} due to rate limit. Reset at: {reset_time}")
continue