From 44671535539780735e00c34296ea0144b73c2b2a Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 21:17:39 +0800 Subject: [PATCH] some code improvements --- cli.py | 3 --- main.py | 26 ++++++++++++++----------- video_creation/final_video.py | 12 ++++++------ video_creation/screenshot_downloader.py | 15 +++++--------- 4 files changed, 26 insertions(+), 30 deletions(-) diff --git a/cli.py b/cli.py index 9c0fba4..4c6179d 100644 --- a/cli.py +++ b/cli.py @@ -1,14 +1,11 @@ import argparse -from rich.console import Console - from main import main from setup_program import setup from utils.console import print_substep def program_options(): - console = Console() description = """\ DESCRIPTION HERE. diff --git a/main.py b/main.py index 3e08dcb..f1607f8 100644 --- a/main.py +++ b/main.py @@ -10,6 +10,7 @@ from video_creation.voices import save_text_to_mp3 from video_creation.screenshot_downloader import download_screenshots_of_reddit_posts from video_creation.final_video import make_final_video from utils.console import print_markdown, print_substep +from setup_program import setup def main(subreddit_=None, background=None, filename=None, thread_link_=None): @@ -40,13 +41,17 @@ def main(subreddit_=None, background=None, filename=None, thread_link_=None): configured = True if not os.path.exists(".env"): shutil.copy(".env.template", ".env") - console.print("[red] Your .env file is invalid, or was never created. Standby.[/red]") + console.print( + "[bold red] Your .env file is invalid, or was never created. Standby.[/bold red]" + ) - console.print("[bold green]Checking environment variables...") + console.print("[bold green]Checking environment variables...[/bol green]") for val in REQUIRED_VALUES: if not os.getenv(val): - print_substep(f"Please set the variable \"{val}\" in your .env file.", style="bold red") + print_substep( + f"Please set the variable \"{val}\" in your .env file.", style="bold red" + ) configured = False if configured: @@ -54,12 +59,11 @@ def main(subreddit_=None, background=None, filename=None, thread_link_=None): float(os.getenv("OPACITY")) except: console.print( - f"[red]Please ensure that OPACITY is set between 0 and 1 in your .env file" + f"[bold red]Please ensure that OPACITY is between 0 and 1 in .env file.[/bold red]" ) - configured = False - exit() + raise SystemExit() - console.log("[bold green]Enviroment Variables are set! Continuing...[/bold green]") + console.print("[bold green]Enviroment Variables are set! Continuing...[/bold green]") reddit_object = get_subreddit_threads(subreddit_, thread_link_) length, number_of_comments = save_text_to_mp3(reddit_object) @@ -70,12 +74,12 @@ def main(subreddit_=None, background=None, filename=None, thread_link_=None): ) download_background(background) chop_background_video(length) - final_video = make_final_video(number_of_comments, filename) + make_final_video(number_of_comments, filename) else: console.print( - "[red]Looks like you need to set your Reddit credentials in the .env file. " - + "Please follow the instructions in the README.md file to set them up.[/red]" + "[bold red]Looks like you need to set your Reddit credentials in the .env file. " + + "Please follow the instructions in the README.md file to set them up.[/bold red]" ) setup_ask = input("\033[1mLaunch setup wizard? [y/N] > \033[0m") if setup_ask in ["y", "Y"]: - os.system("python3 setup.py") + setup() diff --git a/video_creation/final_video.py b/video_creation/final_video.py index e6fa7da..0ad5fee 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -42,7 +42,9 @@ def make_final_video(number_of_clips, file_name): FloatingPointError, TypeError ): - print(f"Please ensure that OPACITY is set between 0 and 1 in your .env file") + print_substep( + f"Please ensure that OPACITY is between 0 and 1 in .env file", style="bold red" + ) # Gather all audio clips audio_clips = [] @@ -72,7 +74,7 @@ def make_final_video(number_of_clips, file_name): .set_position("center") .resize(width=W - 100) .set_opacity(float(opacity)), - ) + ) else: image_clips.insert( 0, @@ -81,11 +83,9 @@ def make_final_video(number_of_clips, file_name): .set_position("center") .resize(width=W - 100) .set_opacity(float(opacity)), - ) + ) - image_concat = concatenate_videoclips( - image_clips).set_position(("center", "center") - ) + image_concat = concatenate_videoclips(image_clips).set_position(("center", "center")) image_concat.audio = audio_composite final = CompositeVideoClip([background_clip, image_concat]) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index c7d9abd..136deda 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -40,14 +40,11 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme): print_substep("Post is NSFW. You are spicy...") page.locator('[data-testid="content-gate"] button').click() - page.locator('[data-test-id="post-content"]').screenshot( - path="assets/png/title.png" - ) + page.locator('[data-test-id="post-content"]').screenshot(path="assets/png/title.png") for idx, comment in track( enumerate(reddit_object["comments"]), "Downloading screenshots..." ): - # Stop if we have reached the screenshot_num if idx >= screenshot_num: break @@ -56,10 +53,8 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme): page.locator('[data-testid="content-gate"] button').click() page.goto(f'https://reddit.com{comment["comment_url"]}') - page.locator(f"#t1_{comment['comment_id']}").screenshot( - path=f"assets/png/comment_{idx}.png" - ) + page.locator( + f"#t1_{comment['comment_id']}" + ).screenshot(path=f"assets/png/comment_{idx}.png") - print_substep( - "Screenshots downloaded Successfully.", style="bold green" - ) + print_substep("Screenshots downloaded Successfully.", style="bold green")