# export_image.py import argparse from playwright.sync_api import sync_playwright import time def export_subreddit_image(subreddit_name, weekly=False): """ Launches a headless browser to take a screenshot of a subreddit's image view. """ view_type = "weekly" if weekly else "daily" print(f"Exporting {view_type} image for r/{subreddit_name}...") # The URL our Flask app serves base_url = "http://127.0.0.1:5000" path = f"image/weekly/{subreddit_name}" if weekly else f"image/{subreddit_name}" url = f"{base_url}/{path}" # Define the output filename output_file = f"{subreddit_name}_{'weekly' if weekly else 'daily'}_{int(time.time())}.png" with sync_playwright() as p: browser = p.chromium.launch() page = browser.new_page() # Set a large viewport for high-quality screenshots page.set_viewport_size({"width": 1920, "height": 1080}) print(f"Navigating to {url}...") page.goto(url) # Important: Give the page a second to ensure all styles and fonts have loaded page.wait_for_timeout(1000) # Target the specific element we want to screenshot element = page.locator(".image-container") print(f"Saving screenshot to {output_file}...") element.screenshot(path=output_file) browser.close() print("Export complete!") if __name__ == "__main__": parser = argparse.ArgumentParser(description="Export subreddit sentiment images.") parser.add_argument("subreddit", help="The name of the subreddit to export.") parser.add_argument("--weekly", action="store_true", help="Export the weekly view instead of the daily view.") args = parser.parse_args() # NOTE: This script assumes your 'rstat-dashboard' server is already running in another terminal. export_subreddit_image(args.subreddit, args.weekly)