Fixed exporting.

This commit is contained in:
2025-07-31 22:23:09 +02:00
parent 437f7b055f
commit 2f43380970

View File

@@ -4,6 +4,7 @@ import argparse
import os import os
import time import time
from playwright.sync_api import sync_playwright from playwright.sync_api import sync_playwright
from pathlib import Path
# Define the output directory as a constant # Define the output directory as a constant
OUTPUT_DIR = "images" OUTPUT_DIR = "images"
@@ -12,17 +13,17 @@ OUTPUT_DIR = "images"
def export_image(url_path, filename_prefix): def export_image(url_path, filename_prefix):
""" """
Launches a headless browser, navigates to a URL path, and screenshots 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}") print(f"-> Preparing to export image for: {filename_prefix}")
# 1. Ensure the output directory exists
os.makedirs(OUTPUT_DIR, exist_ok=True) 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}" 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") output_file = os.path.join(OUTPUT_DIR, f"{filename_prefix}_{int(time.time())}.png")
with sync_playwright() as p: with sync_playwright() as p:
@@ -33,10 +34,15 @@ def export_image(url_path, filename_prefix):
page.set_viewport_size({"width": 1920, "height": 1080}) page.set_viewport_size({"width": 1920, "height": 1080})
print(f" Navigating to {url}...") 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 # Give fonts and styles a moment to render after the DOM is ready
element = page.locator(".image-container") 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}...") print(f" Saving screenshot to {output_file}...")
element.screenshot(path=output_file) element.screenshot(path=output_file)
@@ -52,7 +58,6 @@ def export_image(url_path, filename_prefix):
if __name__ == "__main__": if __name__ == "__main__":
# Use a mutually exclusive group to ensure only one mode is chosen
parser = argparse.ArgumentParser(description="Export subreddit sentiment images.") parser = argparse.ArgumentParser(description="Export subreddit sentiment images.")
group = parser.add_mutually_exclusive_group(required=True) group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-s", "--subreddit", help="The name of the subreddit to export.") group.add_argument("-s", "--subreddit", help="The name of the subreddit to export.")
@@ -64,20 +69,18 @@ if __name__ == "__main__":
"-w", "-w",
"--weekly", "--weekly",
action="store_true", 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() args = parser.parse_args()
# Determine the correct URL path and filename based on arguments
if args.subreddit:
view_type = "weekly" if args.weekly else "daily" view_type = "weekly" if args.weekly else "daily"
# Add ?view=... and the new &image=true parameter
if args.subreddit:
url_path_to_render = f"subreddit/{args.subreddit}?view={view_type}&image=true" url_path_to_render = f"subreddit/{args.subreddit}?view={view_type}&image=true"
filename_prefix_to_save = f"{args.subreddit}_{view_type}" filename_prefix_to_save = f"{args.subreddit}_{view_type}"
export_image(url_path_to_render, filename_prefix_to_save) export_image(url_path_to_render, filename_prefix_to_save)
elif args.overall: elif args.overall:
# For overall, we assume daily view for the image url_path_to_render = f"/?view={view_type}&image=true"
url_path_to_render = "/?view=daily&image=true" filename_prefix_to_save = f"overall_summary_{view_type}"
filename_prefix_to_save = "overall_summary_daily"
export_image(url_path_to_render, filename_prefix_to_save) export_image(url_path_to_render, filename_prefix_to_save)