fix flickr image large issue

my-fix-branch
Shane 7 months ago
parent cdc54f3f14
commit c936555741
  1. 53
      foodie_utils.py

@ -283,7 +283,18 @@ def get_image(search_query):
logging.info(f"Skipping image with unwanted keywords: {photo.id} (tags: {tags}, title: {title}, matched: {matched_keywords})") logging.info(f"Skipping image with unwanted keywords: {photo.id} (tags: {tags}, title: {title}, matched: {matched_keywords})")
return None return None
# Try 'Large' size first, fall back to 'Medium' if unavailable
img_url = None
try:
img_url = photo.getPhotoFile(size_label='Large')
except flickr_api.flickrerrors.FlickrError as e:
logging.info(f"Large size not available for photo {photo.id}: {e}, trying Medium")
try:
img_url = photo.getPhotoFile(size_label='Medium') img_url = photo.getPhotoFile(size_label='Medium')
except flickr_api.flickrerrors.FlickrError as e:
logging.warning(f"Medium size not available for photo {photo.id}: {e}")
return None
if not img_url or img_url in used_images: if not img_url or img_url in used_images:
return None return None
@ -307,7 +318,7 @@ def get_image(search_query):
f.write('\n') f.write('\n')
logging.info(f"Saved Flickr image metadata to {flickr_file}: {img_url}") logging.info(f"Saved Flickr image metadata to {flickr_file}: {img_url}")
logging.info(f"Fallback Flickr image: {img_url} by {uploader} for query '{search_query}' (tags: {tags})") logging.info(f"Selected Flickr image: {img_url} by {uploader} for query '{search_query}' (tags: {tags})")
return img_url, "Flickr", uploader, page_url return img_url, "Flickr", uploader, page_url
def search_ddg_for_flickr(query): def search_ddg_for_flickr(query):
@ -1010,24 +1021,19 @@ if os.path.exists(used_images_file):
else: else:
data = json.loads(content) data = json.loads(content)
if not isinstance(data, list): if not isinstance(data, list):
logging.warning(f"Invalid format in {used_images_file}: expected a list, got {type(data)}. Resetting.") logging.warning(f"Invalid format in {used_images_file}: expected a list, got {type(data)}. Converting to list.")
data = [] if isinstance(data, dict):
else: # If it's a dict, try to extract URLs from values
# Handle malformed format (list of lists or invalid entries) data = [v for v in data.values() if isinstance(v, str) and v.startswith('https://')]
flat_data = []
for item in data:
if isinstance(item, str) and item.startswith('https://'):
flat_data.append(item)
elif isinstance(item, list):
logging.warning(f"Fixing malformed entry in {used_images_file}: {item}")
flat_data.extend([sub_item for sub_item in item if isinstance(sub_item, str) and sub_item.startswith('https://')])
else: else:
logging.warning(f"Skipping invalid entry in {used_images_file}: {item}") logging.warning(f"Cannot convert {type(data)} to list. Resetting to empty list.")
data = flat_data data = []
# Filter out non-string or non-URL entries
data = [item for item in data if isinstance(item, str) and item.startswith('https://')]
used_images.update(data) used_images.update(data)
logging.info(f"Loaded {len(used_images)} used image URLs from {used_images_file}") logging.info(f"Loaded {len(used_images)} used image URLs from {used_images_file}")
except Exception as e: except Exception as e:
logging.warning(f"Failed to load used images from {used_images_file}: {e}. Resetting file.") logging.warning(f"Failed to load used images from {used_images_file}: {e}. Resetting to empty set.")
used_images = set() used_images = set()
with open(used_images_file, 'w') as f: with open(used_images_file, 'w') as f:
json.dump([], f) json.dump([], f)
@ -1035,17 +1041,14 @@ if os.path.exists(used_images_file):
# Function to save used_images to file # Function to save used_images to file
def save_used_images(): def save_used_images():
try: try:
# Ensure used_images contains only valid URLs
valid_urls = [url for url in used_images if isinstance(url, str) and url.startswith('https://')]
if len(valid_urls) != len(used_images):
logging.warning(f"Found {len(used_images) - len(valid_urls)} invalid URLs in used_images set")
with open(used_images_file, 'w') as f: with open(used_images_file, 'w') as f:
f.write('[\n') json.dump(valid_urls, f, indent=2)
urls = list(used_images) logging.info(f"Saved {len(valid_urls)} used image URLs to {used_images_file}")
for i, url in enumerate(urls):
f.write(f'"{url}"')
if i < len(urls) - 1:
f.write(',\n')
else:
f.write('\n')
f.write(']')
logging.info(f"Saved {len(used_images)} used image URLs to {used_images_file}")
except Exception as e: except Exception as e:
logging.warning(f"Failed to save used images to {used_images_file}: {e}") logging.warning(f"Failed to save used images to {used_images_file}: {e}")

Loading…
Cancel
Save