Lots of improvements and adding a script to post to reddit.

This commit is contained in:
2025-07-22 16:26:40 +02:00
parent d4ed76e153
commit f6ea86fa91
8 changed files with 384 additions and 119 deletions

View File

@@ -1,51 +1,73 @@
# export_image.py
import argparse
from playwright.sync_api import sync_playwright
import os
import time
from playwright.sync_api import sync_playwright
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}...")
# Define the output directory as a constant
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.
"""
print(f"-> Preparing to export image for: {filename_prefix}")
# 1. Ensure the output directory exists
os.makedirs(OUTPUT_DIR, exist_ok=True)
# 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}"
url = f"{base_url}/{url_path}"
# Define the output filename
output_file = f"{subreddit_name}_{'weekly' if weekly else 'daily'}_{int(time.time())}.png"
# 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:
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!")
try:
browser = p.chromium.launch()
page = browser.new_page()
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
# 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(f"-> Export complete! Image saved to {output_file}")
except Exception as e:
print(f"\nAn error occurred during export: {e}")
print("Please ensure the 'rstat-dashboard' server is running in another terminal.")
if __name__ == "__main__":
# Use a mutually exclusive group to ensure only one mode is chosen
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.")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-s", "--subreddit", help="The name of the subreddit to export.")
group.add_argument("-o", "--overall", action="store_true", help="Export the overall summary image.")
parser.add_argument("-w", "--weekly", action="store_true", help="Export the weekly view instead of the daily view (only for --subreddit).")
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)
# Determine the correct URL path and filename based on arguments
if args.subreddit:
view_type = "weekly" if args.weekly else "daily"
url_path_to_render = f"image/{view_type}/{args.subreddit}"
filename_prefix_to_save = f"{args.subreddit}_{view_type}"
export_image(url_path_to_render, filename_prefix_to_save)
elif args.overall:
if args.weekly:
print("Warning: --weekly flag has no effect with --overall. Exporting overall summary.")
url_path_to_render = "image/overall"
filename_prefix_to_save = "overall_summary"
export_image(url_path_to_render, filename_prefix_to_save)