|
|
|
|
@ -38,12 +38,13 @@ IMAGE_UPLOAD_TIMEOUT = 30 # Added to fix NameError |
|
|
|
|
|
|
|
|
|
IMAGE_EXPIRATION_DAYS = 7 # 7 days, consistent with foodie_automator_rss.py |
|
|
|
|
|
|
|
|
|
def load_json_file(file_path, expiration_hours=None): |
|
|
|
|
def load_json_file(file_path, expiration_hours=None, default=None): |
|
|
|
|
""" |
|
|
|
|
Load JSON file, optionally filtering out expired entries. |
|
|
|
|
Load JSON file, optionally filtering expired entries and returning default if invalid. |
|
|
|
|
""" |
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
default = [] |
|
|
|
|
if default is None: |
|
|
|
|
default = [] # Default to list for posted_rss_titles.json and used_images.json |
|
|
|
|
|
|
|
|
|
if not os.path.exists(file_path): |
|
|
|
|
logger.info(f"File {file_path} does not exist. Returning default: {default}") |
|
|
|
|
@ -82,7 +83,7 @@ def save_json_file(file_path, data, timestamp=None): |
|
|
|
|
try: |
|
|
|
|
# If timestamp is provided, append as a new entry |
|
|
|
|
if timestamp: |
|
|
|
|
current_data = load_json_file(file_path) |
|
|
|
|
current_data = load_json_file(file_path, default=[]) |
|
|
|
|
new_entry = {'title': data, 'timestamp': timestamp} |
|
|
|
|
if new_entry not in current_data: # Avoid duplicates |
|
|
|
|
current_data.append(new_entry) |
|
|
|
|
@ -1134,31 +1135,32 @@ def check_author_rate_limit(author, max_requests=10, window_seconds=3600): |
|
|
|
|
rate_limit_file = '/home/shane/foodie_automator/rate_limit_info.json' |
|
|
|
|
rate_limit_info = load_json_file(rate_limit_file, default={}) |
|
|
|
|
|
|
|
|
|
if author['username'] not in rate_limit_info: |
|
|
|
|
rate_limit_info[author['username']] = { |
|
|
|
|
username = author['username'] |
|
|
|
|
if username not in rate_limit_info: |
|
|
|
|
rate_limit_info[username] = { |
|
|
|
|
'remaining': max_requests, |
|
|
|
|
'reset': time.time() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
info = rate_limit_info[author['username']] |
|
|
|
|
info = rate_limit_info[username] |
|
|
|
|
current_time = time.time() |
|
|
|
|
|
|
|
|
|
# Reset if window expired |
|
|
|
|
if current_time >= info['reset']: |
|
|
|
|
info['remaining'] = max_requests |
|
|
|
|
info['reset'] = current_time + window_seconds |
|
|
|
|
logger.info(f"Reset rate limit for {author['username']}: {max_requests} requests available") |
|
|
|
|
logger.info(f"Reset rate limit for {username}: {max_requests} requests available") |
|
|
|
|
save_json_file(rate_limit_file, rate_limit_info) |
|
|
|
|
|
|
|
|
|
if info['remaining'] <= 0: |
|
|
|
|
reset_time = datetime.fromtimestamp(info['reset'], tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S') |
|
|
|
|
logger.info(f"Author {author['username']} is rate-limited. Remaining: {info['remaining']}, Reset at: {reset_time}") |
|
|
|
|
logger.info(f"Author {username} is rate-limited. Remaining: {info['remaining']}, Reset at: {reset_time}") |
|
|
|
|
return True |
|
|
|
|
|
|
|
|
|
# Decrement remaining requests |
|
|
|
|
info['remaining'] -= 1 |
|
|
|
|
save_json_file(rate_limit_file, rate_limit_info) |
|
|
|
|
logger.info(f"Updated rate limit for {author['username']}: {info['remaining']} requests remaining") |
|
|
|
|
logger.info(f"Updated rate limit for {username}: {info['remaining']} requests remaining") |
|
|
|
|
return False |
|
|
|
|
|
|
|
|
|
def get_next_author_round_robin(): |
|
|
|
|
@ -1178,6 +1180,7 @@ def get_next_author_round_robin(): |
|
|
|
|
return author |
|
|
|
|
|
|
|
|
|
logger.warning("No authors available due to rate limits. Selecting a random author as fallback.") |
|
|
|
|
import random |
|
|
|
|
author = random.choice(AUTHORS) |
|
|
|
|
logger.info(f"Selected author via random fallback: {author['username']}") |
|
|
|
|
return author |
|
|
|
|
|