|
| 1 | +""" |
| 2 | +Capture HTML files from output_* directories as images using Playwright. |
| 3 | +""" |
| 4 | +import argparse |
| 5 | +import asyncio |
| 6 | +from pathlib import Path |
| 7 | +from typing import List |
| 8 | + |
| 9 | +from playwright.async_api import async_playwright |
| 10 | + |
| 11 | + |
| 12 | +async def capture_html_file_async( |
| 13 | + html_path: Path, |
| 14 | + output_path: Path, |
| 15 | + width: int = 800, |
| 16 | +) -> None: |
| 17 | + """Capture a single HTML file as an image.""" |
| 18 | + html_content = html_path.read_text(encoding="utf-8") |
| 19 | + |
| 20 | + async with async_playwright() as p: |
| 21 | + browser = await p.chromium.launch(headless=True) |
| 22 | + try: |
| 23 | + page = await browser.new_page(viewport={"width": width, "height": 600}) |
| 24 | + await page.set_content(html_content) |
| 25 | + await page.screenshot(path=output_path, full_page=True) |
| 26 | + finally: |
| 27 | + await browser.close() |
| 28 | + |
| 29 | + |
| 30 | +async def capture_batch_async( |
| 31 | + html_files: List[Path], |
| 32 | + output_dir: Path, |
| 33 | + width: int = 800, |
| 34 | +) -> None: |
| 35 | + """Capture multiple HTML files, reusing a single browser instance.""" |
| 36 | + async with async_playwright() as p: |
| 37 | + browser = await p.chromium.launch(headless=True) |
| 38 | + try: |
| 39 | + for html_path in html_files: |
| 40 | + output_path = output_dir / f"{html_path.stem}.png" |
| 41 | + if output_path.exists(): |
| 42 | + print(f" [SKIP] {output_path.name} already exists") |
| 43 | + continue |
| 44 | + |
| 45 | + try: |
| 46 | + html_content = html_path.read_text(encoding="utf-8") |
| 47 | + page = await browser.new_page(viewport={"width": width, "height": 600}) |
| 48 | + await page.set_content(html_content) |
| 49 | + await page.screenshot(path=output_path, full_page=True) |
| 50 | + await page.close() |
| 51 | + print(f" [OK] {html_path.name} -> {output_path.name}") |
| 52 | + except Exception as e: |
| 53 | + print(f" [ERROR] {html_path.name}: {e}") |
| 54 | + finally: |
| 55 | + await browser.close() |
| 56 | + |
| 57 | + |
| 58 | +def main(): |
| 59 | + parser = argparse.ArgumentParser(description="Capture HTML files as images") |
| 60 | + parser.add_argument( |
| 61 | + "--output-dirs", |
| 62 | + nargs="+", |
| 63 | + default=None, |
| 64 | + help="Specific output directories to process (e.g., output_academic output_finance)", |
| 65 | + ) |
| 66 | + parser.add_argument( |
| 67 | + "--width", |
| 68 | + type=int, |
| 69 | + default=800, |
| 70 | + help="Viewport width for rendering (default: 800)", |
| 71 | + ) |
| 72 | + parser.add_argument( |
| 73 | + "--force", |
| 74 | + action="store_true", |
| 75 | + help="Overwrite existing images", |
| 76 | + ) |
| 77 | + args = parser.parse_args() |
| 78 | + |
| 79 | + base_dir = Path(__file__).parent |
| 80 | + |
| 81 | + # Find output_* directories |
| 82 | + if args.output_dirs: |
| 83 | + output_dirs = [base_dir / d for d in args.output_dirs] |
| 84 | + else: |
| 85 | + output_dirs = sorted(base_dir.glob("output_*")) |
| 86 | + output_dirs = [d for d in output_dirs if d.is_dir()] |
| 87 | + |
| 88 | + if not output_dirs: |
| 89 | + print("No output_* directories found.") |
| 90 | + return |
| 91 | + |
| 92 | + print(f"Found {len(output_dirs)} output directories to process") |
| 93 | + |
| 94 | + for output_dir in output_dirs: |
| 95 | + html_dir = output_dir / "html" |
| 96 | + if not html_dir.exists(): |
| 97 | + print(f"\n[SKIP] {output_dir.name}: no html/ subdirectory") |
| 98 | + continue |
| 99 | + |
| 100 | + # Create images directory |
| 101 | + images_dir = output_dir / "images" |
| 102 | + images_dir.mkdir(exist_ok=True) |
| 103 | + |
| 104 | + html_files = sorted(html_dir.glob("*.html")) |
| 105 | + if not html_files: |
| 106 | + print(f"\n[SKIP] {output_dir.name}: no HTML files found") |
| 107 | + continue |
| 108 | + |
| 109 | + # Filter out already processed files unless --force |
| 110 | + if not args.force: |
| 111 | + html_files = [ |
| 112 | + f for f in html_files |
| 113 | + if not (images_dir / f"{f.stem}.png").exists() |
| 114 | + ] |
| 115 | + |
| 116 | + if not html_files: |
| 117 | + print(f"\n[SKIP] {output_dir.name}: all files already processed") |
| 118 | + continue |
| 119 | + |
| 120 | + print(f"\n[Processing] {output_dir.name}: {len(html_files)} HTML files") |
| 121 | + asyncio.run(capture_batch_async(html_files, images_dir, args.width)) |
| 122 | + |
| 123 | + print("\nDone!") |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + main() |
0 commit comments