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})")
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')
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:
return None
@ -307,7 +318,7 @@ def get_image(search_query):
f.write('\n')
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
def search_ddg_for_flickr(query):
@ -1010,24 +1021,19 @@ if os.path.exists(used_images_file):
else:
data = json.loads(content)
if not isinstance(data, list):
logging.warning(f"Invalid format in {used_images_file}: expected a list, got {type(data)}. Resetting.")
data = []
else:
# Handle malformed format (list of lists or invalid entries)
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://')])
logging.warning(f"Invalid format in {used_images_file}: expected a list, got {type(data)}. Converting to list.")
if isinstance(data, dict):
# If it's a dict, try to extract URLs from values
data = [v for v in data.values() if isinstance(v, str) and v.startswith('https://')]
else:
logging.warning(f"Skipping invalid entry in {used_images_file}: {item}")
data = flat_data
logging.warning(f"Cannot convert {type(data)} to list. Resetting to empty list.")
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)
logging.info(f"Loaded {len(used_images)} used image URLs from {used_images_file}")
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()
with open(used_images_file, 'w') as f:
json.dump([], f)
@ -1035,17 +1041,14 @@ if os.path.exists(used_images_file):
# Function to save used_images to file
def save_used_images():
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:
f.write('[\n')
urls = list(used_images)
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}")
json.dump(valid_urls, f, indent=2)
logging.info(f"Saved {len(valid_urls)} used image URLs to {used_images_file}")
except Exception as e:
logging.warning(f"Failed to save used images to {used_images_file}: {e}")

Loading…
Cancel
Save