update google & reddit rate limiting realtime
This commit is contained in:
+18
-19
@@ -857,24 +857,22 @@ if os.path.exists(used_images_file):
|
||||
|
||||
# Function to save used_images to file
|
||||
def save_used_images():
|
||||
"""
|
||||
Save used_images to used_images.json as a JSON array, preserving timestamps.
|
||||
"""
|
||||
try:
|
||||
# Load existing entries to preserve timestamps
|
||||
entries = load_json_file(used_images_file, IMAGE_EXPIRATION_DAYS * 24)
|
||||
existing_entries = {entry["title"]: entry for entry in entries if isinstance(entry, dict) and "title" in entry}
|
||||
|
||||
# Create new entries for used_images
|
||||
# Create entries for used_images
|
||||
timestamp = datetime.now(timezone.utc).isoformat()
|
||||
updated_entries = []
|
||||
for url in used_images:
|
||||
if url in existing_entries:
|
||||
updated_entries.append(existing_entries[url])
|
||||
else:
|
||||
updated_entries.append({"title": url, "timestamp": timestamp})
|
||||
|
||||
with open(used_images_file, 'w') as f:
|
||||
for entry in updated_entries:
|
||||
f.write(json.dumps(entry) + '\n')
|
||||
logging.info(f"Saved {len(updated_entries)} used image URLs to {used_images_file}")
|
||||
entries = [
|
||||
{"title": url, "timestamp": entry.get("timestamp", timestamp)}
|
||||
for url, entry in [
|
||||
(url, next((e for e in load_json_file(used_images_file, IMAGE_EXPIRATION_DAYS * 24) if e["title"] == url), {}))
|
||||
for url in used_images
|
||||
]
|
||||
]
|
||||
# Use save_json_file for atomic write
|
||||
save_json_file(used_images_file, entries)
|
||||
logging.info(f"Saved {len(entries)} used image URLs to {used_images_file}")
|
||||
except Exception as e:
|
||||
logging.warning(f"Failed to save used images to {used_images_file}: {e}")
|
||||
|
||||
@@ -1136,17 +1134,18 @@ def check_author_rate_limit(author, max_requests=10, window_seconds=3600):
|
||||
rate_limit_info = load_json_file(rate_limit_file, default={})
|
||||
|
||||
username = author['username']
|
||||
if username not in rate_limit_info:
|
||||
if username not in rate_limit_info or not isinstance(rate_limit_info[username].get('reset'), (int, float)):
|
||||
rate_limit_info[username] = {
|
||||
'remaining': max_requests,
|
||||
'reset': time.time()
|
||||
}
|
||||
logger.info(f"Initialized rate limit for {username}: {max_requests} requests available")
|
||||
|
||||
info = rate_limit_info[username]
|
||||
current_time = time.time()
|
||||
|
||||
# Reset if window expired
|
||||
if current_time >= info['reset']:
|
||||
# Reset if window expired or timestamp is invalid (e.g., 1970)
|
||||
if current_time >= info['reset'] or info['reset'] < 1000000000: # 1000000000 is ~2001
|
||||
info['remaining'] = max_requests
|
||||
info['reset'] = current_time + window_seconds
|
||||
logger.info(f"Reset rate limit for {username}: {max_requests} requests available")
|
||||
|
||||
Reference in New Issue
Block a user