diff --git a/export_image.py b/export_image.py index 6d66a51..3f163db 100644 --- a/export_image.py +++ b/export_image.py @@ -4,6 +4,7 @@ import argparse import os import time from playwright.sync_api import sync_playwright +from pathlib import Path # Define the output directory as a constant OUTPUT_DIR = "images" @@ -12,17 +13,17 @@ OUTPUT_DIR = "images" def export_image(url_path, filename_prefix): """ Launches a headless browser, navigates to a URL path, and screenshots - the .image-container element, saving it to the OUTPUT_DIR. + the main content element, saving it to the OUTPUT_DIR. """ print(f"-> Preparing to export image for: {filename_prefix}") - # 1. Ensure the output directory exists os.makedirs(OUTPUT_DIR, exist_ok=True) - base_url = "http://localhost:5000" + base_url = "http://localhost" + # Ensure the URL path starts correctly + url_path = url_path.lstrip("/") url = f"{base_url}/{url_path}" - # 2. Construct the full output path including the new directory output_file = os.path.join(OUTPUT_DIR, f"{filename_prefix}_{int(time.time())}.png") with sync_playwright() as p: @@ -33,10 +34,15 @@ def export_image(url_path, filename_prefix): page.set_viewport_size({"width": 1920, "height": 1080}) print(f" Navigating to {url}...") - page.goto(url, wait_until="networkidle") # Wait for network to be idle + # Use 'domcontentloaded' for faster navigation when possible + page.goto(url, wait_until="domcontentloaded") - # Target the specific element we want to screenshot - element = page.locator(".image-container") + # Give fonts and styles a moment to render after the DOM is ready + page.wait_for_timeout(500) + + # --- THIS IS THE CORRECTED LINE --- + # Target the new, correct class for our main content card + element = page.locator(".max-w-3xl") print(f" Saving screenshot to {output_file}...") element.screenshot(path=output_file) @@ -52,7 +58,6 @@ def export_image(url_path, filename_prefix): if __name__ == "__main__": - # Use a mutually exclusive group to ensure only one mode is chosen parser = argparse.ArgumentParser(description="Export subreddit sentiment images.") group = parser.add_mutually_exclusive_group(required=True) group.add_argument("-s", "--subreddit", help="The name of the subreddit to export.") @@ -64,20 +69,18 @@ if __name__ == "__main__": "-w", "--weekly", action="store_true", - help="Export the weekly view instead of the daily view (only for --subreddit).", + help="Export the weekly view instead of the daily view.", ) args = parser.parse_args() - # Determine the correct URL path and filename based on arguments + view_type = "weekly" if args.weekly else "daily" + if args.subreddit: - view_type = "weekly" if args.weekly else "daily" - # Add ?view=... and the new &image=true parameter url_path_to_render = f"subreddit/{args.subreddit}?view={view_type}&image=true" filename_prefix_to_save = f"{args.subreddit}_{view_type}" export_image(url_path_to_render, filename_prefix_to_save) elif args.overall: - # For overall, we assume daily view for the image - url_path_to_render = "/?view=daily&image=true" - filename_prefix_to_save = "overall_summary_daily" + url_path_to_render = f"/?view={view_type}&image=true" + filename_prefix_to_save = f"overall_summary_{view_type}" export_image(url_path_to_render, filename_prefix_to_save)