#!/usr/bin/env python3 """ Copy all missing images referenced in posts """ import os import re import shutil from pathlib import Path POSTS_DIR = Path('/Users/ericwagoner/Sites/blog/content/posts') ARCHIVE_DIR = Path('/Users/ericwagoner/Sites/ericwagoner.com') STATIC_DIR = Path('/Users/ericwagoner/Sites/blog/static') def find_all_image_links(): """Find all image links in posts""" links = set() for post in POSTS_DIR.glob('*.md'): with open(post, 'r', encoding='utf-8') as f: content = f.read() # Find all /images/legacy links pattern = r'/images/legacy(/[^)\s"]*(?:jpg|jpeg|gif|png))' for match in re.finditer(pattern, content, re.IGNORECASE): links.add(match.group(1)) return sorted(links) def copy_missing_images(): """Copy any missing images""" image_links = find_all_image_links() print(f"Found {len(image_links)} image links in posts") copied = 0 already_exists = 0 not_found = [] for link in image_links: # Check if it already exists in static dest_path = STATIC_DIR / 'images' / 'legacy' / link.lstrip('/') if dest_path.exists(): already_exists += 1 continue # Try to find in archive src_path = ARCHIVE_DIR / link.lstrip('/') if not src_path.exists() and '/weblog/' in link: # Try without initial slash alt_link = link.replace('/weblog/', 'weblog/', 1) src_path = ARCHIVE_DIR / alt_link.lstrip('/') if src_path.exists(): # Create parent directories and copy dest_path.parent.mkdir(parents=True, exist_ok=True) try: shutil.copy2(src_path, dest_path) copied += 1 print(f"Copied: {link}") except Exception as e: print(f"Error copying {src_path}: {e}") else: not_found.append(link) print(f"\n✅ Summary:") print(f" - Already existed: {already_exists}") print(f" - Newly copied: {copied}") print(f" - Not found: {len(not_found)}") if not_found: print("\n⚠️ Could not find these images:") for link in not_found[:10]: print(f" {link}") if len(not_found) > 10: print(f" ... and {len(not_found) - 10} more") if __name__ == "__main__": copy_missing_images()