From c4f32a3821317ae039dadd0043b265a2619cf49e Mon Sep 17 00:00:00 2001 From: MeDBeD1 <82283979+MeDBeD1@users.noreply.github.com> Date: Sat, 4 Jun 2022 00:48:20 +1000 Subject: [PATCH 1/9] TTS reads text below the tittle of the post Added option for TTS to read the whole post if text apart from tittle is present (useful for such threads as r/maliciouscompliance) --- subreddit.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 subreddit.py diff --git a/subreddit.py b/subreddit.py new file mode 100644 index 0000000..e9ae4fe --- /dev/null +++ b/subreddit.py @@ -0,0 +1,73 @@ +from utils.console import print_markdown, print_step, print_substep +import praw +import random +from dotenv import load_dotenv +import os + + +def get_subreddit_threads(): + + """ + Returns a list of threads from the AskReddit subreddit. + """ + + load_dotenv() + + print_step("Getting AskReddit threads...") + + if os.getenv("REDDIT_2FA").lower() == "yes": + print( + "\nEnter your two-factor authentication code from your authenticator app.\n" + ) + code = input("> ") + print() + pw = os.getenv("REDDIT_PASSWORD") + passkey = f"{pw}:{code}" + else: + passkey = os.getenv("REDDIT_PASSWORD") + + content = {} + + reddit = praw.Reddit( + client_id=os.getenv("REDDIT_CLIENT_ID"), + client_secret=os.getenv("REDDIT_CLIENT_SECRET"), + user_agent="Accessing AskReddit threads", + username=os.getenv("REDDIT_USERNAME"), + password=passkey, + ) + + if os.getenv("SUBREDDIT"): + subreddit = reddit.subreddit(os.getenv("SUBREDDIT")) + else: + # ! Prompt the user to enter a subreddit + try: + subreddit = reddit.subreddit( + input("What subreddit would you like to pull from? ") + ) + except ValueError: + subreddit = reddit.subreddit("askreddit") + print_substep("Subreddit not defined. Using AskReddit.") + + threads = subreddit.hot(limit=25) + submission = list(threads)[random.randrange(0, 25)] + print_substep(f"Video will be: {submission.title} :thumbsup:") + try: + + content["thread_url"] = submission.url + content["thread_title"] = submission.title + submission.selftext + content["comments"] = [] + + for top_level_comment in submission.comments: + content["comments"].append( + { + "comment_body": top_level_comment.body, + "comment_url": top_level_comment.permalink, + "comment_id": top_level_comment.id, + } + ) + + except AttributeError as e: + pass + print_substep("Received AskReddit threads successfully.", style="bold green") + + return content From 310006a166974580645570b8103f88cec7e25db0 Mon Sep 17 00:00:00 2001 From: MeDBeD1 <82283979+MeDBeD1@users.noreply.github.com> Date: Sat, 4 Jun 2022 00:50:36 +1000 Subject: [PATCH 2/9] Deleting becasue i commited in the wrong way, whoops --- subreddit.py | 73 ---------------------------------------------------- 1 file changed, 73 deletions(-) delete mode 100644 subreddit.py diff --git a/subreddit.py b/subreddit.py deleted file mode 100644 index e9ae4fe..0000000 --- a/subreddit.py +++ /dev/null @@ -1,73 +0,0 @@ -from utils.console import print_markdown, print_step, print_substep -import praw -import random -from dotenv import load_dotenv -import os - - -def get_subreddit_threads(): - - """ - Returns a list of threads from the AskReddit subreddit. - """ - - load_dotenv() - - print_step("Getting AskReddit threads...") - - if os.getenv("REDDIT_2FA").lower() == "yes": - print( - "\nEnter your two-factor authentication code from your authenticator app.\n" - ) - code = input("> ") - print() - pw = os.getenv("REDDIT_PASSWORD") - passkey = f"{pw}:{code}" - else: - passkey = os.getenv("REDDIT_PASSWORD") - - content = {} - - reddit = praw.Reddit( - client_id=os.getenv("REDDIT_CLIENT_ID"), - client_secret=os.getenv("REDDIT_CLIENT_SECRET"), - user_agent="Accessing AskReddit threads", - username=os.getenv("REDDIT_USERNAME"), - password=passkey, - ) - - if os.getenv("SUBREDDIT"): - subreddit = reddit.subreddit(os.getenv("SUBREDDIT")) - else: - # ! Prompt the user to enter a subreddit - try: - subreddit = reddit.subreddit( - input("What subreddit would you like to pull from? ") - ) - except ValueError: - subreddit = reddit.subreddit("askreddit") - print_substep("Subreddit not defined. Using AskReddit.") - - threads = subreddit.hot(limit=25) - submission = list(threads)[random.randrange(0, 25)] - print_substep(f"Video will be: {submission.title} :thumbsup:") - try: - - content["thread_url"] = submission.url - content["thread_title"] = submission.title + submission.selftext - content["comments"] = [] - - for top_level_comment in submission.comments: - content["comments"].append( - { - "comment_body": top_level_comment.body, - "comment_url": top_level_comment.permalink, - "comment_id": top_level_comment.id, - } - ) - - except AttributeError as e: - pass - print_substep("Received AskReddit threads successfully.", style="bold green") - - return content From 89d69ff0af09d5904406ae04096460e106a9a59b Mon Sep 17 00:00:00 2001 From: MeDBeD1 <82283979+MeDBeD1@users.noreply.github.com> Date: Sat, 4 Jun 2022 00:53:14 +1000 Subject: [PATCH 3/9] TTS update to read the text below tittle if there is any --- reddit/subreddit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 5d020fe..e9ae4fe 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -54,7 +54,7 @@ def get_subreddit_threads(): try: content["thread_url"] = submission.url - content["thread_title"] = submission.title + content["thread_title"] = submission.title + submission.selftext content["comments"] = [] for top_level_comment in submission.comments: From 6ec947523273e7ba6c09be85b2cd37e76bbe72bb Mon Sep 17 00:00:00 2001 From: MeDBeD1 <82283979+MeDBeD1@users.noreply.github.com> Date: Sat, 4 Jun 2022 08:47:24 +1000 Subject: [PATCH 4/9] First 2 files of requested changes --- video_creation/final_video.py | 3 ++- video_creation/voices.py | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index e1f71ff..7dd8d02 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -29,6 +29,7 @@ def make_final_video(number_of_clips): for i in range(0, number_of_clips): audio_clips.append(AudioFileClip(f"assets/mp3/{i}.mp3")) audio_clips.insert(0, AudioFileClip(f"assets/mp3/title.mp3")) + audio_clips.insert(1, AudioFileClip(f"assets/mp3/posttext.mp3")) audio_concat = concatenate_audioclips(audio_clips) audio_composite = CompositeAudioClip([audio_concat]) @@ -44,7 +45,7 @@ def make_final_video(number_of_clips): image_clips.insert( 0, ImageClip(f"assets/png/title.png") - .set_duration(audio_clips[0].duration) + .set_duration(audio_clips[0].duration + audio_clips[1].duration) .set_position("center") .resize(width=W - 100), ) diff --git a/video_creation/voices.py b/video_creation/voices.py index e8d8e43..b4650e6 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -21,6 +21,10 @@ def save_text_to_mp3(reddit_obj): tts.save(f"assets/mp3/title.mp3") length += MP3(f"assets/mp3/title.mp3").info.length + tts = gTTS(text=reddit_obj["thread_post"], lang="en", slow=False) + tts.save(f"assets/mp3/posttext.mp3") + length += MP3(f"assets/mp3/posttext.mp3").info.length + for idx, comment in track(enumerate(reddit_obj["comments"]), "Saving..."): # ! Stop creating mp3 files if the length is greater than 50 seconds. This can be longer, but this is just a good starting point if length > 50: From 2fbb369f8a70905f98a5b2b055e1f8073927d57c Mon Sep 17 00:00:00 2001 From: MeDBeD1 <82283979+MeDBeD1@users.noreply.github.com> Date: Sat, 4 Jun 2022 08:48:10 +1000 Subject: [PATCH 5/9] the 3rd file of requested changes --- reddit/subreddit.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index e9ae4fe..66148d5 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -54,7 +54,8 @@ def get_subreddit_threads(): try: content["thread_url"] = submission.url - content["thread_title"] = submission.title + submission.selftext + content["thread_title"] = submission.title + content["thread_post"] = submission.selftext content["comments"] = [] for top_level_comment in submission.comments: From 2ef606fe29d818cbb666fbddddf9aabd727cdf8b Mon Sep 17 00:00:00 2001 From: MeDBeD1 <82283979+MeDBeD1@users.noreply.github.com> Date: Sat, 4 Jun 2022 16:52:37 +1000 Subject: [PATCH 6/9] Fixed a bug: an error if the thread has no selftext Had to implement a few checks for the thread having selftext and for selftext tts file to exist --- video_creation/final_video.py | 31 ++++++++++++++++++++++--------- video_creation/voices.py | 7 ++++--- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 7dd8d02..6f2fbbf 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -8,7 +8,7 @@ from moviepy.editor import ( CompositeVideoClip, ) from utils.console import print_step - +from pathlib import Path W, H = 1080, 1920 @@ -29,7 +29,10 @@ def make_final_video(number_of_clips): for i in range(0, number_of_clips): audio_clips.append(AudioFileClip(f"assets/mp3/{i}.mp3")) audio_clips.insert(0, AudioFileClip(f"assets/mp3/title.mp3")) - audio_clips.insert(1, AudioFileClip(f"assets/mp3/posttext.mp3")) + try: + audio_clips.insert(1, AudioFileClip(f"assets/mp3/posttext.mp3")) + except: + OSError() audio_concat = concatenate_audioclips(audio_clips) audio_composite = CompositeAudioClip([audio_concat]) @@ -42,13 +45,23 @@ def make_final_video(number_of_clips): .set_position("center") .resize(width=W - 100), ) - image_clips.insert( - 0, - ImageClip(f"assets/png/title.png") - .set_duration(audio_clips[0].duration + audio_clips[1].duration) - .set_position("center") - .resize(width=W - 100), - ) + + if Path(f"assets/mp3/posttext.mp3").is_file(): #audio_clips[1] == AudioFileClip(f"assets/mp3/0.mp3"): + image_clips.insert( + 0, + ImageClip(f"assets/png/title.png") + .set_duration(audio_clips[0].duration + audio_clips[1].duration) + .set_position("center") + .resize(width=W - 100), + ) + else: + image_clips.insert( + 0, + ImageClip(f"assets/png/title.png") + .set_duration(audio_clips[0].duration) + .set_position("center") + .resize(width=W - 100), + ) image_concat = concatenate_videoclips(image_clips).set_position( ("center", "center") ) diff --git a/video_creation/voices.py b/video_creation/voices.py index b4650e6..56a2b23 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -21,9 +21,10 @@ def save_text_to_mp3(reddit_obj): tts.save(f"assets/mp3/title.mp3") length += MP3(f"assets/mp3/title.mp3").info.length - tts = gTTS(text=reddit_obj["thread_post"], lang="en", slow=False) - tts.save(f"assets/mp3/posttext.mp3") - length += MP3(f"assets/mp3/posttext.mp3").info.length + if reddit_obj["thread_post"] != "": + tts = gTTS(text=reddit_obj["thread_post"], lang="en", slow=False) + tts.save(f"assets/mp3/posttext.mp3") + length += MP3(f"assets/mp3/posttext.mp3").info.length for idx, comment in track(enumerate(reddit_obj["comments"]), "Saving..."): # ! Stop creating mp3 files if the length is greater than 50 seconds. This can be longer, but this is just a good starting point From 3115155d894329a4e94bb82cd18e56d3a0c96289 Mon Sep 17 00:00:00 2001 From: MeDBeD1 <82283979+MeDBeD1@users.noreply.github.com> Date: Sun, 5 Jun 2022 08:59:26 +1000 Subject: [PATCH 7/9] Added a check for selftext if selftext file exists it gets deleted and recreated if it is present in the thread --- video_creation/voices.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/video_creation/voices.py b/video_creation/voices.py index 56a2b23..c0df8b7 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -21,6 +21,11 @@ def save_text_to_mp3(reddit_obj): tts.save(f"assets/mp3/title.mp3") length += MP3(f"assets/mp3/title.mp3").info.length + try: + Path(f"assets/mp3/posttext.mp3").unlink() + except OSError as e: + pass + if reddit_obj["thread_post"] != "": tts = gTTS(text=reddit_obj["thread_post"], lang="en", slow=False) tts.save(f"assets/mp3/posttext.mp3") From 72a4bdabaf898619f92db2d2a5beb3ce5369526f Mon Sep 17 00:00:00 2001 From: Callum Leslie Date: Sun, 5 Jun 2022 22:27:04 +0100 Subject: [PATCH 8/9] Fix merge errors from other PR's --- .env.template | 6 +++--- main.py | 18 +++--------------- reddit/subreddit.py | 2 +- 3 files changed, 7 insertions(+), 19 deletions(-) diff --git a/.env.template b/.env.template index 6b55f99..e95c209 100644 --- a/.env.template +++ b/.env.template @@ -4,11 +4,11 @@ REDDIT_CLIENT_SECRET="" REDDIT_USERNAME="" REDDIT_PASSWORD="" -# Valid options are "yes" and "no" +# Valid options are "yes" and "no" REDDIT_2FA="" -#If no, it will ask you a thread link to extract the thread, if yes it will randomize it. -RANDOM_THREAD="no" +#If no, it will ask you a thread link to extract the thread, if yes it will randomize it. +RANDOM_THREAD="yes" # Valid options are "light" and "dark" THEME="" diff --git a/main.py b/main.py index 61ccbbb..9aabe78 100644 --- a/main.py +++ b/main.py @@ -54,7 +54,7 @@ if not os.path.exists(".env"): console.log("[red] Your .env file is invalid, or was never created. Standby.") for val in REQUIRED_VALUES: - print(os.getenv(val)) + #print(os.getenv(val)) if val not in os.environ or not os.getenv(val): console.log(f'[bold red]Missing Variable: "{val}"') configured = False @@ -70,8 +70,8 @@ for val in REQUIRED_VALUES: console.log("[bold green]Here goes nothing! Launching setup wizard...") time.sleep(0.5) os.system("python3 setup.py") - else: - if setup_ask == "no": + + elif setup_ask == "no": console.print("[red]Exiting...") time.sleep(0.5) exit() @@ -79,8 +79,6 @@ for val in REQUIRED_VALUES: console.print("[red]I don't understand that. Exiting...") time.sleep(0.5) exit() - - exit() try: float(os.getenv("OPACITY")) except: @@ -91,16 +89,6 @@ except: exit() console.log("[bold green]Enviroment Variables are set! Continuing...") - -length, number_of_comments = save_text_to_mp3(reddit_object) -download_screenshots_of_reddit_posts( - reddit_object, number_of_comments, os.getenv("THEME") -) -download_background() -chop_background_video(length) -final_video = make_final_video(number_of_comments) - - if configured: reddit_object = get_subreddit_threads() length, number_of_comments = save_text_to_mp3(reddit_object) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 6d5fa7a..c560293 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -33,7 +33,7 @@ def get_subreddit_threads(): username=os.getenv("REDDIT_USERNAME"), password=passkey, ) - + # If the user specifies that he doesnt want a random thread, or if he doesn't insert the "RANDOM_THREAD" variable at all, ask the thread link if not os.getenv("RANDOM_THREAD") or os.getenv("RANDOM_THREAD") == "no": print_substep("Insert the full thread link:", style="bold green") From 7ff6739fee0b694437e6464c5f9f160254cad36d Mon Sep 17 00:00:00 2001 From: MeDBeD1 <82283979+MeDBeD1@users.noreply.github.com> Date: Mon, 6 Jun 2022 07:29:00 +1000 Subject: [PATCH 9/9] Uploaded file with requested changes via DMs --- main.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/main.py b/main.py index 61ccbbb..0727e20 100644 --- a/main.py +++ b/main.py @@ -91,16 +91,6 @@ except: exit() console.log("[bold green]Enviroment Variables are set! Continuing...") - -length, number_of_comments = save_text_to_mp3(reddit_object) -download_screenshots_of_reddit_posts( - reddit_object, number_of_comments, os.getenv("THEME") -) -download_background() -chop_background_video(length) -final_video = make_final_video(number_of_comments) - - if configured: reddit_object = get_subreddit_threads() length, number_of_comments = save_text_to_mp3(reddit_object)