From c9e497e5cfd20378d92658b0dce5b2b0f2e719d1 Mon Sep 17 00:00:00 2001 From: HallowedDust5 Date: Tue, 21 Jun 2022 21:37:08 -0400 Subject: [PATCH 1/7] Added function comment --- video_creation/voices.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/video_creation/voices.py b/video_creation/voices.py index be7da96..aaadd42 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -20,9 +20,13 @@ VIDEO_LENGTH: int = 40 # secs def save_text_to_mp3(reddit_obj): - """Saves Text to MP3 files. + """Saves text to MP3 files. Goes through the reddit_obj and generates the title MP3 file and a certain number of comments until the total amount of time exceeds VIDEO_LENGTH seconds. + Args: - reddit_obj : The reddit object you received from the reddit API in the askreddit.py file. + reddit_obj (dict[str]): Reddit object received from reddit API in askreddit.py + + Returns: + tuple[int,int]: (total length of the audio, the number of comments audio was generated for) """ print_step("Saving Text to MP3 files...") length = 0 From 3ec194510b464f0e2f4395cbf66b246ef71666b9 Mon Sep 17 00:00:00 2001 From: HallowedDust5 Date: Tue, 21 Jun 2022 21:37:41 -0400 Subject: [PATCH 2/7] Removed redundant function --- main.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 9998f44..5f01e5f 100755 --- a/main.py +++ b/main.py @@ -37,11 +37,9 @@ def main(): load_dotenv() cleanup() - def get_obj(): - reddit_obj = get_subreddit_threads() - return reddit_obj - reddit_object = get_obj() + + reddit_object = get_subreddit_threads() length, number_of_comments = save_text_to_mp3(reddit_object) download_screenshots_of_reddit_posts(reddit_object, number_of_comments) download_background() From 96d77f7ae0e32d0e44ebb9dc01a8b41200d3b391 Mon Sep 17 00:00:00 2001 From: HallowedDust5 Date: Tue, 21 Jun 2022 21:43:38 -0400 Subject: [PATCH 3/7] Added typing and redid function comment --- video_creation/screenshot_downloader.py | 12 +++++++----- video_creation/voices.py | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 6147dff..452bcd9 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -15,12 +15,14 @@ console = Console() storymode = False -def download_screenshots_of_reddit_posts(reddit_object, screenshot_num): - """Downloads screenshots of reddit posts as they are seen on the web. +def download_screenshots_of_reddit_posts(reddit_object:dict[str], screenshot_num:int): + """Downloads screenshots of reddit posts as seen on the web. Downloads to assets/temp/png + Args: - reddit_object: The Reddit Object you received in askreddit.py - screenshot_num: The number of screenshots you want to download. - """ + reddit_object (dict[str]): Reddit object received from subreddit.py + screenshot_num (int): Number of screenshots to downlaod + """ + print_step("Downloading screenshots of reddit posts...") # ! Make sure the reddit screenshots folder exists diff --git a/video_creation/voices.py b/video_creation/voices.py index aaadd42..97e5917 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -23,7 +23,7 @@ def save_text_to_mp3(reddit_obj): """Saves text to MP3 files. Goes through the reddit_obj and generates the title MP3 file and a certain number of comments until the total amount of time exceeds VIDEO_LENGTH seconds. Args: - reddit_obj (dict[str]): Reddit object received from reddit API in askreddit.py + reddit_obj (dict[str]): Reddit object received from reddit API in subreddit.py Returns: tuple[int,int]: (total length of the audio, the number of comments audio was generated for) From b38184bc32ae150651622c74fe7466f470908202 Mon Sep 17 00:00:00 2001 From: HallowedDust5 Date: Tue, 21 Jun 2022 21:59:54 -0400 Subject: [PATCH 4/7] Updated doc comments, added typing in function declaration --- video_creation/final_video.py | 67 ++++++++++++++++++++++------------- video_creation/voices.py | 2 +- 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 37b1ac2..53ad5db 100755 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -25,7 +25,13 @@ console = Console() W, H = 1080, 1920 -def make_final_video(number_of_clips, length): +def make_final_video(number_of_clips:int, length:int): + """Gathers audio clips, gathers all screenshots, stitches them together and saves the final video to assets/temp + + Args: + number_of_clips (int): Index to end at when going through the screenshots + length (int): Length of the video + """ print_step("Creating the final video 🎥") VideoFileClip.reW = lambda clip: clip.resize(width=W) VideoFileClip.reH = lambda clip: clip.resize(width=H) @@ -104,32 +110,12 @@ def make_final_video(number_of_clips, length): image_concat.audio = audio_composite final = CompositeVideoClip([background_clip, image_concat]) - def get_video_title() -> str: - title = os.getenv("VIDEO_TITLE") or "final_video" - if len(title) <= 35: - return title - else: - return title[0:30] + "..." filename = f"{get_video_title()}.mp4" - def save_data(): - with open("./video_creation/data/videos.json", "r+") as raw_vids: - done_vids = json.load(raw_vids) - if str(subreddit.submission.id) in [video["id"] for video in done_vids]: - return # video already done but was specified to continue anyway in the .env file - payload = { - "id": str(os.getenv("VIDEO_ID")), - "time": str(int(time.time())), - "background_credit": str(os.getenv("background_credit")), - "reddit_title": str(os.getenv("VIDEO_TITLE")), - "filename": filename, - } - done_vids.append(payload) - raw_vids.seek(0) - json.dump(done_vids, raw_vids, ensure_ascii=False, indent=4) - - save_data() + + save_data(filename) + if not exists("./results"): print_substep("the results folder didn't exist so I made it") os.mkdir("./results") @@ -148,3 +134,36 @@ def make_final_video(number_of_clips, length): print_step( f"Reddit title: {os.getenv('VIDEO_TITLE')} \n Background Credit: {os.getenv('background_credit')}" ) + +def save_data(filename:str): + """Saves the videos that have already been generated to a JSON file in video_creation/data/videos.json + + Args: + filename (str): The finished video title name + """ + with open("./video_creation/data/videos.json", "r+") as raw_vids: + done_vids = json.load(raw_vids) + if str(subreddit.submission.id) in [video["id"] for video in done_vids]: + return # video already done but was specified to continue anyway in the .env file + payload = { + "id": str(os.getenv("VIDEO_ID")), + "time": str(int(time.time())), + "background_credit": str(os.getenv("background_credit")), + "reddit_title": str(os.getenv("VIDEO_TITLE")), + "filename": filename, + } + done_vids.append(payload) + raw_vids.seek(0) + json.dump(done_vids, raw_vids, ensure_ascii=False, indent=4) + +def get_video_title() -> str: + """Gets video title from env variable or gives it the name "final_video" + + Returns: + str: Video title + """ + title = os.getenv("VIDEO_TITLE") or "final_video" + if len(title) <= 35: + return title + else: + return title[0:30] + "..." \ No newline at end of file diff --git a/video_creation/voices.py b/video_creation/voices.py index 97e5917..f68c20b 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -19,7 +19,7 @@ console = Console() VIDEO_LENGTH: int = 40 # secs -def save_text_to_mp3(reddit_obj): +def save_text_to_mp3(reddit_obj:dict[str])->tuple[int,int]: """Saves text to MP3 files. Goes through the reddit_obj and generates the title MP3 file and a certain number of comments until the total amount of time exceeds VIDEO_LENGTH seconds. Args: From 07466097a3eb98b44100ed026f2fda2f7300a8ef Mon Sep 17 00:00:00 2001 From: HallowedDust5 Date: Tue, 21 Jun 2022 22:58:33 -0400 Subject: [PATCH 5/7] Added docs and typing in function declarations --- utils/checker.py | 5 +++++ utils/cleanup.py | 5 +++++ utils/subreddit.py | 22 ++++++++++++++++++++-- utils/videos.py | 15 +++++++++++---- utils/voice.py | 17 +++++++++++------ video_creation/background.py | 19 ++++++++++++++++--- 6 files changed, 68 insertions(+), 15 deletions(-) diff --git a/utils/checker.py b/utils/checker.py index 07b1e15..668b40f 100755 --- a/utils/checker.py +++ b/utils/checker.py @@ -11,6 +11,11 @@ console = Console() def check_env() -> bool: + """Checks to see what's been put in .env + + Returns: + bool: Whether or not everything was put in properly + """ if not os.path.exists(".env.template"): console.print("[red]Couldn't find .env.template. Unable to check variables.") return True diff --git a/utils/cleanup.py b/utils/cleanup.py index 9490b6d..858cfe9 100644 --- a/utils/cleanup.py +++ b/utils/cleanup.py @@ -3,6 +3,11 @@ from os.path import exists def cleanup() -> int: + """Deletes all temporary assets in assets/temp + + Returns: + int: How many files were deleted + """ if exists("./assets/temp"): count = 0 files = [f for f in os.listdir(".") if f.endswith(".mp4") and "temp" in f.lower()] diff --git a/utils/subreddit.py b/utils/subreddit.py index e05c136..3c5cb22 100644 --- a/utils/subreddit.py +++ b/utils/subreddit.py @@ -4,7 +4,16 @@ from os import getenv from utils.console import print_substep -def get_subreddit_undone(submissions: List, subreddit): +def get_subreddit_undone(submissions: list, subreddit): + """_summary_ + + Args: + submissions (list): List of posts that are going to potentially be generated into a video + subreddit (praw.Reddit.SubredditHelper): Chosen subreddit + + Returns: + Any: The submission that has not been done + """ """ recursively checks if the top submission in the list was already done. """ @@ -27,7 +36,16 @@ def get_subreddit_undone(submissions: List, subreddit): ) # all of the videos in hot have already been done -def already_done(done_videos: list, submission): +def already_done(done_videos: list, submission)->bool: + """Checks to see if the given submission is in the list of videos + + Args: + done_videos (list): Finished videos + submission (Any): The submission + + Returns: + Boolean: Whether the video was found in the list + """ for video in done_videos: if video["id"] == str(submission): diff --git a/utils/videos.py b/utils/videos.py index 51a2704..e6510fe 100755 --- a/utils/videos.py +++ b/utils/videos.py @@ -5,10 +5,17 @@ from utils.console import print_step def check_done( - redditobj, -): # don't set this to be run anyplace that isn't subreddit.py bc of inspect stack - """params: - reddit_object: The Reddit Object you received in askreddit.py""" + redditobj:dict[str], +)->dict[str]|None: # don't set this to be run anyplace that isn't subreddit.py bc of inspect stack + """Checks if the chosen post has already been generated + + Args: + redditobj (dict[str]): Reddit object gotten from reddit/subreddit.py + + Returns: + dict[str]|None: Reddit object in args + """ + with open("./video_creation/data/videos.json", "r") as done_vids_raw: done_videos = json.load(done_vids_raw) for video in done_videos: diff --git a/utils/voice.py b/utils/voice.py index 120ee60..57b2b7e 100644 --- a/utils/voice.py +++ b/utils/voice.py @@ -1,12 +1,17 @@ import re -def sanitize_text(text): - """ - Sanitizes the text for tts. - What gets removed: - - following characters`^_~@!&;#:-%“”‘"%*/{}[]()\|<>?=+` - - any http or https links +def sanitize_text(text: str) -> str: + """Sanitizes the text for tts. + What gets removed: + - following characters`^_~@!&;#:-%“”‘"%*/{}[]()\|<>?=+` + - any http or https links + + Args: + text (str): Text to be sanitized + + Returns: + str: Sanitized text """ # remove any urls from the text diff --git a/video_creation/background.py b/video_creation/background.py index fb300e6..7236ecd 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -8,7 +8,16 @@ from moviepy.editor import VideoFileClip from utils.console import print_step, print_substep -def get_start_and_end_times(video_length, length_of_clip): +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 beckground of the video. + + Args: + video_length (int): Length of the video + length_of_clip (int): Length of the video to be used as the background + + Returns: + tuple[int,int]: Start and end time of the randomized interval + """ random_time = randrange(180, int(length_of_clip) - int(video_length)) return random_time, random_time + video_length @@ -43,7 +52,12 @@ def download_background(): print_substep("Background videos downloaded successfully! 🎉", style="bold green") -def chop_background_video(video_length): +def chop_background_video(video_length:int): + """Generates the background footage to be used in the video and writes it to assets/temp/background.mp4 + + Args: + video_length (int): Length of the clip where the background footage is to be taken out of + """ print_step("Finding a spot in the backgrounds video to chop...✂️") choice = random.choice(listdir("assets/backgrounds")) environ["background_credit"] = choice.split("-")[0] @@ -58,4 +72,3 @@ def chop_background_video(video_length): targetname="assets/temp/background.mp4", ) print_substep("Background video chopped successfully!", style="bold green") - return True From 520a889cd0567ef80bc3ef63afd97388aa94a571 Mon Sep 17 00:00:00 2001 From: HallowedDust5 Date: Tue, 21 Jun 2022 23:03:19 -0400 Subject: [PATCH 6/7] Replaced subreddit.py with reddit/subreddit.py in docstring comments --- video_creation/screenshot_downloader.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 452bcd9..9a5266c 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -19,7 +19,7 @@ def download_screenshots_of_reddit_posts(reddit_object:dict[str], screenshot_num """Downloads screenshots of reddit posts as seen on the web. Downloads to assets/temp/png Args: - reddit_object (dict[str]): Reddit object received from subreddit.py + reddit_object (dict[str]): Reddit object received from reddit/subreddit.py screenshot_num (int): Number of screenshots to downlaod """ From e51d0a08352b548a51a8574b0e70814b4691d8dc Mon Sep 17 00:00:00 2001 From: HallowedDust5 Date: Tue, 21 Jun 2022 23:03:34 -0400 Subject: [PATCH 7/7] Last commit was meant to be this one --- video_creation/voices.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/video_creation/voices.py b/video_creation/voices.py index f68c20b..f740e1b 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -23,7 +23,7 @@ def save_text_to_mp3(reddit_obj:dict[str])->tuple[int,int]: """Saves text to MP3 files. Goes through the reddit_obj and generates the title MP3 file and a certain number of comments until the total amount of time exceeds VIDEO_LENGTH seconds. Args: - reddit_obj (dict[str]): Reddit object received from reddit API in subreddit.py + reddit_obj (dict[str]): Reddit object received from reddit API in reddit/subreddit.py Returns: tuple[int,int]: (total length of the audio, the number of comments audio was generated for)