This commit is contained in:
2025-05-03 16:46:09 +10:00
parent 73e0ef4f53
commit b265b5aa30
2 changed files with 78 additions and 23 deletions
+31
View File
@@ -46,6 +46,11 @@ logger = logging.getLogger(__name__)
load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
# Initialize global variables
used_images = set()
pixabay_rate_limiter = RateLimiter(max_requests=100, time_window=3600) # 100 requests per hour
flickr_rate_limiter = RateLimiter(max_requests=3600, time_window=3600) # 3600 requests per hour
def validate_json_entry(entry: Dict[str, Any]) -> bool:
"""Validate the structure of a JSON entry."""
required_fields = {"title", "timestamp"}
@@ -862,6 +867,32 @@ def prune_recent_posts():
except Exception as e:
logger.error(f"Failed to prune recent_posts.json: {e}")
def load_used_images():
"""Load the set of used image URLs from file."""
global used_images
try:
if os.path.exists(USED_IMAGES_FILE):
with open(USED_IMAGES_FILE, 'r') as f:
used_images = set(json.loads(line.strip())['url'] for line in f if line.strip())
logger.info(f"Loaded {len(used_images)} used images from {USED_IMAGES_FILE}")
except Exception as e:
logger.error(f"Failed to load used images: {e}")
used_images = set()
def save_used_images():
"""Save the set of used image URLs to file."""
try:
with open(USED_IMAGES_FILE, 'w') as f:
for url in used_images:
json.dump({'url': url, 'timestamp': datetime.now(timezone.utc).isoformat()}, f)
f.write('\n')
logger.info(f"Saved {len(used_images)} used images to {USED_IMAGES_FILE}")
except Exception as e:
logger.error(f"Failed to save used images: {e}")
# Load used images on startup
load_used_images()
def get_image(search_query: str) -> Tuple[Optional[str], Optional[str], Optional[str], Optional[str]]:
"""Get an image with improved rate limiting and error handling."""
headers = {'User-Agent': 'InsiderFoodieBot/1.0 (https://insiderfoodie.com; contact@insiderfoodie.com)'}