From 4abf80317039b11be240e7aa0c91b01c1c9ed42b Mon Sep 17 00:00:00 2001 From: Jason Date: Thu, 14 Jul 2022 22:45:21 -0400 Subject: [PATCH] reformatted code --- utils/CONSTANTS.py | 1 - utils/subreddit.py | 16 +++++++++-- utils/video.py | 17 ++++++++---- video_creation/background.py | 1 + video_creation/final_video.py | 1 + video_creation/screenshot_downloader.py | 36 +++++++++++++++++-------- 6 files changed, 53 insertions(+), 19 deletions(-) diff --git a/utils/CONSTANTS.py b/utils/CONSTANTS.py index 76d2ee4..e46ebbc 100644 --- a/utils/CONSTANTS.py +++ b/utils/CONSTANTS.py @@ -1,4 +1,3 @@ - # Supported Background. Can add/remove background video here.... # - : key -> used as keyword for TOML file. value -> background configuration # Format (value): diff --git a/utils/subreddit.py b/utils/subreddit.py index 5638f1e..c386868 100644 --- a/utils/subreddit.py +++ b/utils/subreddit.py @@ -41,13 +41,25 @@ def get_subreddit_undone(submissions: list, subreddit, times_checked=0): continue return submission print("all submissions have been done going by top submission order") - VALID_TIME_FILTERS = ["day", "hour", "month", "week", "year", 'all'] # set doesn't have __getitem__ + VALID_TIME_FILTERS = [ + "day", + "hour", + "month", + "week", + "year", + "all", + ] # set doesn't have __getitem__ index = times_checked + 1 if index == len(VALID_TIME_FILTERS): print("all time filters have been checked you absolute madlad ") return get_subreddit_undone( - subreddit.top(time_filter=VALID_TIME_FILTERS[index], limit=(50 if int(index) == 0 else index+1 * 50)), subreddit, times_checked=index) # all the videos in hot have already been done + subreddit.top( + time_filter=VALID_TIME_FILTERS[index], limit=(50 if int(index) == 0 else index + 1 * 50) + ), + subreddit, + times_checked=index, + ) # all the videos in hot have already been done def already_done(done_videos: list, submission) -> bool: diff --git a/utils/video.py b/utils/video.py index 03ed88c..63dc170 100644 --- a/utils/video.py +++ b/utils/video.py @@ -34,14 +34,21 @@ class Video: im.save(path) return ImageClip(path) - def add_watermark(self, text, opacity=0.5, duration: int | float = 5, position: Tuple = (0.7, 0.9), fontsize=15): - compensation = round((position[0] / ((len(text) * (fontsize / 5) / 1.5) / 100 + position[0] * position[0])), ndigits=2) + def add_watermark( + self, text, opacity=0.5, duration: int | float = 5, position: Tuple = (0.7, 0.9), fontsize=15 + ): + compensation = round( + (position[0] / ((len(text) * (fontsize / 5) / 1.5) / 100 + position[0] * position[0])), + ndigits=2, + ) position = (compensation, position[1]) - #print(f'{compensation=}') - #print(f'{position=}') + # print(f'{compensation=}') + # print(f'{position=}') img_clip = self._create_watermark(text, opacity=opacity, fontsize=fontsize) img_clip = img_clip.set_opacity(opacity).set_duration(duration) - img_clip = img_clip.set_position(position, relative=True) # todo get dara from utils/CONSTANTS.py and adapt position accordingly + img_clip = img_clip.set_position( + position, relative=True + ) # todo get dara from utils/CONSTANTS.py and adapt position accordingly # Overlay the img clip on the first video clip self.video = CompositeVideoClip([self.video, img_clip]) diff --git a/video_creation/background.py b/video_creation/background.py index 7a577e6..6e656fa 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -13,6 +13,7 @@ from utils import settings from utils.CONSTANTS import background_options from utils.console import print_step, print_substep + def get_start_and_end_times(video_length: int, length_of_clip: int) -> Tuple[int, int]: """Generates a random interval of time to be used as the background of the video. diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 3157296..8d12afe 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -35,6 +35,7 @@ def name_normalize(name: str) -> str: lang = settings.config["reddit"]["thread"]["post_lang"] if lang: import translators as ts + print_substep("Translating filename...") translated_name = ts.google(name, to_language=lang) return translated_name diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 8010862..1f28c74 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -51,27 +51,36 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in page.locator('[data-testid="content-gate"] button').click() if page.locator('[data-click-id="text"] button').is_visible(): - page.locator('[data-click-id="text"] button').click() # Remove "Click to see nsfw" Button in Screenshot + page.locator( + '[data-click-id="text"] button' + ).click() # Remove "Click to see nsfw" Button in Screenshot # translate code if settings.config["reddit"]["thread"]["post_lang"]: print_substep("Translating post...") - texts_in_tl = ts.google(reddit_object["thread_title"], - to_language=settings.config["reddit"]["thread"]["post_lang"], ) + texts_in_tl = ts.google( + reddit_object["thread_title"], + to_language=settings.config["reddit"]["thread"]["post_lang"], + ) page.evaluate( "tl_content => document.querySelector('[data-test-id=\"post-content\"] > div:nth-child(3) > div > div').textContent = tl_content", - texts_in_tl, ) + texts_in_tl, + ) else: print_substep("Skipping translation...") page.locator('[data-test-id="post-content"]').screenshot(path="assets/temp/png/title.png") if storymode: - page.locator('[data-click-id="text"]').screenshot(path="assets/temp/png/story_content.png") + page.locator('[data-click-id="text"]').screenshot( + path="assets/temp/png/story_content.png" + ) else: - for idx, comment in enumerate(track(reddit_object["comments"], "Downloading screenshots...")): + for idx, comment in enumerate( + track(reddit_object["comments"], "Downloading screenshots...") + ): # Stop if we have reached the screenshot_num if idx >= screenshot_num: break @@ -84,16 +93,21 @@ def download_screenshots_of_reddit_posts(reddit_object: dict, screenshot_num: in # translate code if settings.config["reddit"]["thread"]["post_lang"]: - comment_tl = ts.google(comment["comment_body"], - to_language=settings.config["reddit"]["thread"]["post_lang"], ) + comment_tl = ts.google( + comment["comment_body"], + to_language=settings.config["reddit"]["thread"]["post_lang"], + ) page.evaluate( '([tl_content, tl_id]) => document.querySelector(`#t1_${tl_id} > div:nth-child(2) > div > div[data-testid="comment"] > div`).textContent = tl_content', - [comment_tl, comment["comment_id"]], ) + [comment_tl, comment["comment_id"]], + ) try: - page.locator(f"#t1_{comment['comment_id']}").screenshot(path=f"assets/temp/png/comment_{idx}.png") + page.locator(f"#t1_{comment['comment_id']}").screenshot( + path=f"assets/temp/png/comment_{idx}.png" + ) except TimeoutError: del reddit_object["comments"] screenshot_num += 1 - print('TimeoutError: Skipping screenshot...') + print("TimeoutError: Skipping screenshot...") continue print_substep("Screenshots downloaded Successfully.", style="bold green")