Format all code.
This commit is contained in:
@@ -8,6 +8,7 @@ from playwright.sync_api import sync_playwright
|
||||
# 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
|
||||
@@ -20,7 +21,7 @@ def export_image(url_path, filename_prefix):
|
||||
|
||||
base_url = "http://127.0.0.1:5000"
|
||||
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")
|
||||
|
||||
@@ -28,24 +29,26 @@ def export_image(url_path, filename_prefix):
|
||||
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
|
||||
|
||||
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.")
|
||||
print(
|
||||
"Please ensure the 'rstat-dashboard' server is running in another terminal."
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -53,9 +56,16 @@ if __name__ == "__main__":
|
||||
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.")
|
||||
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).")
|
||||
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()
|
||||
|
||||
# Determine the correct URL path and filename based on arguments
|
||||
@@ -65,9 +75,9 @@ if __name__ == "__main__":
|
||||
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"
|
||||
export_image(url_path_to_render, filename_prefix_to_save)
|
||||
export_image(url_path_to_render, filename_prefix_to_save)
|
||||
|
Reference in New Issue
Block a user