From 012f30eda59aa2f2e787d790a990211dd58295be Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sat, 4 Jun 2022 18:34:19 +0800 Subject: [PATCH 01/83] code cleaning, and complied with pep8 --- main.py | 10 +++++---- reddit/subreddit.py | 28 ++++++++++++------------- utils/console.py | 1 + video_creation/background.py | 3 +-- video_creation/final_video.py | 7 +++++-- video_creation/screenshot_downloader.py | 13 +++++++----- video_creation/voices.py | 13 +++++++----- 7 files changed, 42 insertions(+), 33 deletions(-) diff --git a/main.py b/main.py index 8cc3c9a..3bffbed 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,16 @@ -from utils.console import print_markdown +import os import time +from dotenv import load_dotenv + + from reddit.subreddit import get_subreddit_threads from video_creation.background import download_background, chop_background_video 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 dotenv import load_dotenv -import os +from utils.console import print_markdown + print_markdown( "### Thanks for using this tool! [Feel free to contribute to this project on GitHub!](https://lewismenelaws.com) If you have any questions, feel free to reach out to me on Twitter or submit a GitHub issue." @@ -15,7 +18,6 @@ print_markdown( time.sleep(3) - reddit_object = get_subreddit_threads() load_dotenv() diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 5d020fe..7a7a5bc 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -1,9 +1,10 @@ -from utils.console import print_markdown, print_step, print_substep -import praw import random -from dotenv import load_dotenv import os +import praw +from utils.console import print_markdown, print_step, print_substep +from dotenv import load_dotenv + def get_subreddit_threads(): @@ -36,23 +37,19 @@ def get_subreddit_threads(): 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.") + 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: + try: content["thread_url"] = submission.url content["thread_title"] = submission.title content["comments"] = [] @@ -68,6 +65,7 @@ def get_subreddit_threads(): except AttributeError as e: pass + print_substep("Received AskReddit threads successfully.", style="bold green") return content diff --git a/utils/console.py b/utils/console.py index d024dc0..b565737 100644 --- a/utils/console.py +++ b/utils/console.py @@ -4,6 +4,7 @@ from rich.padding import Padding from rich.panel import Panel from rich.text import Text + console = Console() diff --git a/video_creation/background.py b/video_creation/background.py index dce46bd..69d8709 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -1,15 +1,14 @@ from random import randrange +from pathlib import Path from yt_dlp import YoutubeDL -from pathlib import Path from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip from moviepy.editor import VideoFileClip from utils.console import print_step, print_substep def get_start_and_end_times(video_length, length_of_clip): - random_time = randrange(180, int(length_of_clip) - int(video_length)) return random_time, random_time + video_length diff --git a/video_creation/final_video.py b/video_creation/final_video.py index e1f71ff..70ee619 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -7,6 +7,7 @@ from moviepy.editor import ( CompositeAudioClip, CompositeVideoClip, ) + from utils.console import print_step @@ -28,7 +29,8 @@ def make_final_video(number_of_clips): audio_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(0, AudioFileClip("assets/mp3/title.mp3")) audio_concat = concatenate_audioclips(audio_clips) audio_composite = CompositeAudioClip([audio_concat]) @@ -41,9 +43,10 @@ def make_final_video(number_of_clips): .set_position("center") .resize(width=W - 100), ) + image_clips.insert( 0, - ImageClip(f"assets/png/title.png") + ImageClip("assets/png/title.png") .set_duration(audio_clips[0].duration) .set_position("center") .resize(width=W - 100), diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index d3d32ef..72b65ff 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -1,8 +1,10 @@ -from playwright.sync_api import sync_playwright, ViewportSize +import json from pathlib import Path + +from playwright.sync_api import sync_playwright, ViewportSize from rich.progress import track + from utils.console import print_step, print_substep -import json def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme): @@ -24,7 +26,7 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme): context = browser.new_context() if theme.casefold() == "dark": - cookie_file = open('video_creation/cookies.json') + cookie_file = open("video_creation/cookies.json") cookies = json.load(cookie_file) context.add_cookies(cookies) @@ -58,5 +60,6 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme): path=f"assets/png/comment_{idx}.png" ) - print_substep("Screenshots downloaded Successfully.", - style="bold green") + print_substep( + "Screenshots downloaded Successfully.", style="bold green" + ) diff --git a/video_creation/voices.py b/video_creation/voices.py index e8d8e43..ee0063d 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -1,9 +1,11 @@ -from gtts import gTTS from pathlib import Path + +from gtts import gTTS from mutagen.mp3 import MP3 -from utils.console import print_step, print_substep from rich.progress import track +from utils.console import print_step, print_substep + def save_text_to_mp3(reddit_obj): """Saves Text to MP3 files. @@ -11,15 +13,16 @@ def save_text_to_mp3(reddit_obj): Args: reddit_obj : The reddit object you received from the reddit API in the askreddit.py file. """ - print_step("Saving Text to MP3 files...") length = 0 + print_step("Saving Text to MP3 files...") + # Create a folder for the mp3 files. Path("assets/mp3").mkdir(parents=True, exist_ok=True) tts = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False) - tts.save(f"assets/mp3/title.mp3") - length += MP3(f"assets/mp3/title.mp3").info.length + tts.save("assets/mp3/title.mp3") + length += MP3("assets/mp3/title.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 4f605294e656aa6b362bd91cfa89ff2ecd359566 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sat, 4 Jun 2022 18:42:39 +0800 Subject: [PATCH 02/83] removed subreddit variable --- .env.template | 2 -- 1 file changed, 2 deletions(-) diff --git a/.env.template b/.env.template index 5ab4ae4..e7e16c0 100644 --- a/.env.template +++ b/.env.template @@ -7,5 +7,3 @@ REDDIT_PASSWORD="" REDDIT_2FA="" THEME="" - -SUBREDDIT="" From 0361c25e3973b8d20b12eda4c212100149c12f7b Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sat, 4 Jun 2022 18:42:50 +0800 Subject: [PATCH 03/83] initial work for cli --- cli.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 cli.py diff --git a/cli.py b/cli.py new file mode 100644 index 0000000..9abba77 --- /dev/null +++ b/cli.py @@ -0,0 +1,53 @@ +import argparse + + +def program_options(): + description = """\ + DESCRIPTION HERE. + """ + + parser = argparse.ArgumentParser( + prog="RedditVideoMakerBot", # can be renamed, just a base + usage="RedditVideoMakerBot [OPTIONS]", + description=description + ) + parser.add_argument( + "-c", + "--create", + help="Create a video.", + action="store_true" + ) + # this one accepts link as input, as well as already downloaded video, + # defaults to the minecraft video. + parser.add_argument( + "-b", + "--background", + help="Use another video background for video (accepts link).", + action="store" + ) + parser.add_argument( # only accepts the name of subreddit, not links. + "-S", + "--subreddit", + help="Use another sub-reddit.", + action="store" + ) + # show most of the background processes of the program + parser.add_argument( + "-v", + "--verbose", + help="Show the processes of program.", + action="store_true" + ) + + args = parser.parse_args() + + try: + ... + except ( + ConnectionError, + KeyboardInterrupt + ): + ... + + +program_options() From 4020c733ea6910c812b4d3364a030a59ec82caaa Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sat, 4 Jun 2022 18:44:20 +0800 Subject: [PATCH 04/83] little changes --- reddit/subreddit.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 7a7a5bc..e355d64 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -2,12 +2,12 @@ import random import os import praw -from utils.console import print_markdown, print_step, print_substep from dotenv import load_dotenv +from utils.console import print_step, print_substep -def get_subreddit_threads(): +def get_subreddit_threads(): """ Returns a list of threads from the AskReddit subreddit. """ @@ -21,7 +21,6 @@ def get_subreddit_threads(): "\nEnter your two-factor authentication code from your authenticator app.\n" ) code = input("> ") - print() pw = os.getenv("REDDIT_PASSWORD") passkey = f"{pw}:{code}" else: @@ -62,10 +61,9 @@ def get_subreddit_threads(): "comment_id": top_level_comment.id, } ) - except AttributeError as e: pass - print_substep("Received AskReddit threads successfully.", style="bold green") + print_substep("AskReddit threads retrieved successfully.", style="bold green") return content From f3e1ddd3fe434957b2df7baa0b8319e9e21ced67 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sat, 4 Jun 2022 22:54:21 +0800 Subject: [PATCH 05/83] fixes and turned main.py into function for use in cli.py --- main.py | 26 ++++++++++++++------------ utils/console.py | 4 ++-- video_creation/background.py | 2 +- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 3bffbed..1496aa6 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,6 @@ import time from dotenv import load_dotenv - from reddit.subreddit import get_subreddit_threads from video_creation.background import download_background, chop_background_video from video_creation.voices import save_text_to_mp3 @@ -12,17 +11,20 @@ from video_creation.final_video import make_final_video from utils.console import print_markdown -print_markdown( - "### Thanks for using this tool! [Feel free to contribute to this project on GitHub!](https://lewismenelaws.com) If you have any questions, feel free to reach out to me on Twitter or submit a GitHub issue." -) +def main(): + print_markdown( + "### Thanks for using this tool! [Feel free to contribute to this project on " + + "GitHub!](https://lewismenelaws.com) If you have any questions, feel free to" + + "reach out to me on Twitter or submit a GitHub issue." + ) -time.sleep(3) + time.sleep(3) -reddit_object = get_subreddit_threads() + reddit_object = get_subreddit_threads() -load_dotenv() -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) + load_dotenv() + 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) diff --git a/utils/console.py b/utils/console.py index b565737..6540732 100644 --- a/utils/console.py +++ b/utils/console.py @@ -22,6 +22,6 @@ def print_step(text): console.print(panel) -def print_substep(text, style=""): +def print_substep(text, style_): """Prints a rich info message without the panelling.""" - console.print(text, style=style) + console.print(text, style=style_) diff --git a/video_creation/background.py b/video_creation/background.py index 69d8709..fd53c04 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -2,9 +2,9 @@ from random import randrange from pathlib import Path from yt_dlp import YoutubeDL - from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip from moviepy.editor import VideoFileClip + from utils.console import print_step, print_substep From f7780a05f10d08e21225f3346e2584fdf1d9715b Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sat, 4 Jun 2022 23:33:57 +0800 Subject: [PATCH 06/83] initial setup script --- setup.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 setup.py diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..7aa2f6f --- /dev/null +++ b/setup.py @@ -0,0 +1,43 @@ +from re import M +from setuptools import setup, find_packages + + +with open("README.md", "r", encoding="utf-8") as readme: + description = readme.read() + +setup( + name="RedditVideoMakerBot", + version="1.0", # change this if it is wrong, + description="Create a Reddit video with Python!", + long_description=description, + long_description_content_type="text/markdown", + url="https://github.com/iaacornus/scpterm", + py_modules=[ + "main", + "cli", + "reddit/subreddit", + "utils/console", + "video_creation/background", + "video_creation/final_video", + "video_creation/screenshot_downloader", + "video_creation/voices" + ], + include_package_data=True, + packages=find_packages(), + python_requires=">=3.6", + install_requires=[ + "gTTS==2.2.4", + "rich<=12.4.4", + "moviepy==1.0.3", + "mutagen==1.45.1", + "playwright==1.22.0", + "praw==7.6.0", + "python-dotenv==0.20.0", + "yt_dlp==2022.5.18" + ], + entry_points={ + "console_scripts" : [ + "RedditVideoMakerBot=cli:program_options", + ] + }, +) From 64630b423b3332b602e56abbb13b4a1cc797a7e7 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sat, 4 Jun 2022 23:34:59 +0800 Subject: [PATCH 07/83] removed unncessary parameters and include the function calls --- cli.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/cli.py b/cli.py index 9abba77..e0dfcb4 100644 --- a/cli.py +++ b/cli.py @@ -1,5 +1,8 @@ import argparse +from main import main +from utils.console import print_substep + def program_options(): description = """\ @@ -17,35 +20,32 @@ def program_options(): help="Create a video.", action="store_true" ) - # this one accepts link as input, as well as already downloaded video, - # defaults to the minecraft video. - parser.add_argument( - "-b", - "--background", - help="Use another video background for video (accepts link).", - action="store" - ) parser.add_argument( # only accepts the name of subreddit, not links. "-S", "--subreddit", help="Use another sub-reddit.", action="store" ) - # show most of the background processes of the program + # this one accepts link as input, defaults to the minecraft video + # does not accept file or already downloaded background yet. parser.add_argument( - "-v", - "--verbose", - help="Show the processes of program.", - action="store_true" + "-b", + "--background", + help="Use another video background for video (accepts link).", + action="store" ) args = parser.parse_args() try: - ... + if args.create: + main(args.subreddit, args.background) + else: + print_substep("Error occured!", style="bold red") + raise SystemExit() except ( ConnectionError, - KeyboardInterrupt + KeyboardInterrupt, ): ... From 0944d3b38a8bac11917471b42e050f734fff8ed2 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sat, 4 Jun 2022 23:35:14 +0800 Subject: [PATCH 08/83] synched with the cli.py parameters --- main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 1496aa6..272e93e 100644 --- a/main.py +++ b/main.py @@ -11,7 +11,7 @@ from video_creation.final_video import make_final_video from utils.console import print_markdown -def main(): +def main(subreddit_=None, background=None): print_markdown( "### Thanks for using this tool! [Feel free to contribute to this project on " + "GitHub!](https://lewismenelaws.com) If you have any questions, feel free to" @@ -20,11 +20,11 @@ def main(): time.sleep(3) - reddit_object = get_subreddit_threads() + reddit_object = get_subreddit_threads(subreddit_) load_dotenv() 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() + download_background(background) chop_background_video(length) final_video = make_final_video(number_of_comments) From 558d9bd53119e4f98e0b230e2915ffc0aa167ddf Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sat, 4 Jun 2022 23:36:02 +0800 Subject: [PATCH 09/83] include the subreddit parameter --- reddit/subreddit.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index e355d64..34d2612 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -7,8 +7,12 @@ from dotenv import load_dotenv from utils.console import print_step, print_substep -def get_subreddit_threads(): +def get_subreddit_threads(subreddit_): """ + Takes subreddit_ as parameter which defaults to None, but in this + case since it is None, it would raise ValueError, thus defaulting + to AskReddit. + Returns a list of threads from the AskReddit subreddit. """ @@ -27,7 +31,6 @@ def get_subreddit_threads(): passkey = os.getenv("REDDIT_PASSWORD") content = {} - reddit = praw.Reddit( client_id=os.getenv("REDDIT_CLIENT_ID"), client_secret=os.getenv("REDDIT_CLIENT_SECRET"), @@ -37,9 +40,10 @@ def get_subreddit_threads(): ) try: - subreddit = reddit.subreddit( - input("What subreddit would you like to pull from? ") - ) + if subreddit_ is None: + raise ValueError + + subreddit = reddit.subreddit(subreddit_) except ValueError: subreddit = reddit.subreddit("askreddit") print_substep("Subreddit not defined. Using AskReddit.") From 2afc687d98b43142e048d2eca1f2a4c64da8de59 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sat, 4 Jun 2022 23:36:17 +0800 Subject: [PATCH 10/83] takes link as an input --- video_creation/background.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/video_creation/background.py b/video_creation/background.py index fd53c04..3bf8529 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -1,3 +1,4 @@ +import re from random import randrange from pathlib import Path @@ -12,15 +13,17 @@ def get_start_and_end_times(video_length, length_of_clip): random_time = randrange(180, int(length_of_clip) - int(video_length)) return random_time, random_time + video_length -def download_background(): + +def download_background(background): """Downloads the background video from youtube. Shoutout to: bbswitzer (https://www.youtube.com/watch?v=n_Dv4JMiwK8) """ if not Path("assets/mp4/background.mp4").is_file(): - print_step( - "We need to download the Minecraft background video. This is fairly large but it's only done once." + print_step( # removed minecraft, since the background can be changed according to user input. + "We need to download the background video. This may be" + + " large, depending on the input but it's only done once." ) print_substep("Downloading the background video... please be patient.") @@ -30,10 +33,26 @@ def download_background(): "merge_output_format": "mp4", } - with YoutubeDL(ydl_opts) as ydl: - ydl.download("https://www.youtube.com/watch?v=n_Dv4JMiwK8") + try: + with YoutubeDL(ydl_opts) as ydl: + if background is None: + ydl.download("https://www.youtube.com/watch?v=n_Dv4JMiwK8") - print_substep("Background video downloaded successfully!", style="bold green") + if ( + re.match("https://*youtube.com*", background) and background is not None + ): + ydl.download(background) + else: # if the link is not youtube link + raise ValueError + except ( + ConnectionError, + FileNotFoundError, + ValueError + # add more exceptions + ): + print_substep("The given link is not accepted!", style="bold red") + else: + print_substep("Background video downloaded successfully!", style="bold green") def chop_background_video(video_length): From c3aff9d1d0890f5219daf89528da79a093b9b0cf Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 00:18:37 +0800 Subject: [PATCH 11/83] small fixes --- reddit/subreddit.py | 3 +-- setup.py | 1 - video_creation/background.py | 2 -- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 9a4769c..508cdc5 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -50,8 +50,7 @@ def get_subreddit_threads(subreddit_): subreddit = reddit.subreddit(re.sub(r"r\/", "", os.getenv("SUBREDDIT"))) else: subreddit = reddit.subreddit("askreddit") - - print_substep("Subreddit not defined. Using AskReddit.") + print_substep("Subreddit not defined. Using AskReddit.") threads = subreddit.hot(limit=25) submission = list(threads)[random.randrange(0, 25)] diff --git a/setup.py b/setup.py index 7aa2f6f..c61aa9d 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,3 @@ -from re import M from setuptools import setup, find_packages diff --git a/video_creation/background.py b/video_creation/background.py index 3bf8529..514388b 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -45,8 +45,6 @@ def download_background(background): else: # if the link is not youtube link raise ValueError except ( - ConnectionError, - FileNotFoundError, ValueError # add more exceptions ): From 422eb38b0ba6c64ce3fc5aa89a584d8732ec3d42 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 11:04:09 +0800 Subject: [PATCH 12/83] fixed the pull request --- reddit/subreddit.py | 2 ++ video_creation/final_video.py | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 508cdc5..d549bb7 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -9,6 +9,7 @@ from utils.console import print_step, print_substep def get_subreddit_threads(subreddit_): + global submission """ Takes subreddit_ as parameter which defaults to None, but in this case since it is None, it would raise ValueError, thus defaulting @@ -62,6 +63,7 @@ def get_subreddit_threads(subreddit_): content["comments"] = [] for top_level_comment in submission.comments: + if not top_level_comment.stickied: content["comments"].append( { "comment_body": top_level_comment.body, diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 569853e..0e18d28 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -1,4 +1,5 @@ import os +import re from dotenv import load_dotenv from moviepy.editor import ( @@ -11,6 +12,7 @@ from moviepy.editor import ( CompositeVideoClip, ) +import reddit.subreddit from utils.console import print_step @@ -18,11 +20,11 @@ W, H = 1080, 1920 def make_final_video(number_of_clips): - # Calls opacity from the .env - load_dotenv() - opacity = os.getenv("OPACITY") + # Calls opacity from the .env + load_dotenv() print_step("Creating the final video...") + VideoFileClip.reW = lambda clip: clip.resize(width=W) VideoFileClip.reH = lambda clip: clip.resize(width=H) @@ -34,7 +36,7 @@ def make_final_video(number_of_clips): ) try: - float(os.getenv("OPACITY")) + opacity = float(os.getenv("OPACITY")) except: print(f"Please ensure that OPACITY is set between 0 and 1 in your .env file") configured = False @@ -56,7 +58,7 @@ def make_final_video(number_of_clips): .set_duration(audio_clips[i + 1].duration) .set_position("center") .resize(width=W - 100) - .set_opacity(float(opacity)), + .set_opacity(opacity), ) image_clips.insert( @@ -65,16 +67,16 @@ def make_final_video(number_of_clips): .set_duration(audio_clips[0].duration) .set_position("center") .resize(width=W - 100) - .set_opacity(float(opacity)), + .set_opacity(opacity), ) image_concat = concatenate_videoclips(image_clips).set_position( ("center", "center") ) image_concat.audio = audio_composite final = CompositeVideoClip([background_clip, image_concat]) - final.write_videofile( - "assets/final_video.mp4", fps=30, audio_codec="aac", audio_bitrate="192k" + filename = ( + re.sub('[?\"%*:|<>]', '', ("assets/" + reddit.subreddit.submission.title + ".mp4")) ) - + final.write_videofile(filename, fps=30, audio_codec="aac", audio_bitrate="192k") for i in range(0, number_of_clips): pass From 563eabbbf5d35df7a10443b0b82e4732c164e1dc Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 11:18:16 +0800 Subject: [PATCH 13/83] add back the default value for style --- utils/console.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/console.py b/utils/console.py index 6540732..b565737 100644 --- a/utils/console.py +++ b/utils/console.py @@ -22,6 +22,6 @@ def print_step(text): console.print(panel) -def print_substep(text, style_): +def print_substep(text, style=""): """Prints a rich info message without the panelling.""" - console.print(text, style=style_) + console.print(text, style=style) From 2541b9867bc9f437c620721b5f467dedc3db7b62 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 11:18:25 +0800 Subject: [PATCH 14/83] add back configure --- main.py | 1 + 1 file changed, 1 insertion(+) diff --git a/main.py b/main.py index 39f5c7d..980a6b2 100644 --- a/main.py +++ b/main.py @@ -27,6 +27,7 @@ def main(subreddit_=None, background=None): + "reach out to me on Twitter or submit a GitHub issue." ) + configured = True if not os.path.exists(".env"): shutil.copy(".env.template", ".env") configured = False From 7861c1a0778ecd871a69273cf49104be15cef1d4 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 11:28:50 +0800 Subject: [PATCH 15/83] fixed the conditions for downloading of background video --- video_creation/background.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/video_creation/background.py b/video_creation/background.py index 514388b..a14bd8a 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -19,20 +19,16 @@ def download_background(background): Shoutout to: bbswitzer (https://www.youtube.com/watch?v=n_Dv4JMiwK8) """ + print_step( # removed minecraft, since the background can be changed according to user input. + "Downloading the background video." + ) - if not Path("assets/mp4/background.mp4").is_file(): - print_step( # removed minecraft, since the background can be changed according to user input. - "We need to download the background video. This may be" - + " large, depending on the input but it's only done once." - ) - - print_substep("Downloading the background video... please be patient.") - - ydl_opts = { - "outtmpl": "assets/mp4/background.mp4", - "merge_output_format": "mp4", - } + ydl_opts = { + "outtmpl": "assets/mp4/background.mp4", + "merge_output_format": "mp4", + } + if background is not None or not Path("assets/mp4/background.mp4").is_file(): try: with YoutubeDL(ydl_opts) as ydl: if background is None: From dbb1617432580868bc9fc1d05a8cd038470ec680 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 11:30:19 +0800 Subject: [PATCH 16/83] fixed regex error --- video_creation/background.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/video_creation/background.py b/video_creation/background.py index a14bd8a..7c4527c 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -18,7 +18,7 @@ def download_background(background): """Downloads the background video from youtube. Shoutout to: bbswitzer (https://www.youtube.com/watch?v=n_Dv4JMiwK8) - """ + """ print_step( # removed minecraft, since the background can be changed according to user input. "Downloading the background video." ) @@ -33,13 +33,14 @@ def download_background(background): with YoutubeDL(ydl_opts) as ydl: if background is None: ydl.download("https://www.youtube.com/watch?v=n_Dv4JMiwK8") - - if ( - re.match("https://*youtube.com*", background) and background is not None - ): - ydl.download(background) - else: # if the link is not youtube link - raise ValueError + else: + if ( + re.match("https://*youtube.com*", background) + and background is not None + ): + ydl.download(background) + else: # if the link is not youtube link + raise ValueError except ( ValueError # add more exceptions From 548c06e81e3e7a2aceb020c1bb7cc88455f9bed0 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 11:35:31 +0800 Subject: [PATCH 17/83] add message to inform that background video is already downloaded this is when the user give an input to -b parameter even there is already downloaded video. Although feature to be able to store multiple background video is suggested, which I plan to work to --- video_creation/background.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/video_creation/background.py b/video_creation/background.py index 7c4527c..6ad2769 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -28,7 +28,11 @@ def download_background(background): "merge_output_format": "mp4", } - if background is not None or not Path("assets/mp4/background.mp4").is_file(): + background_check = Path("assets/mp4/background.mp4").is_file() + if background is not None or not background_check: + if background_check: + print_substep("Background video is already downloaded! Replacing ...") + try: with YoutubeDL(ydl_opts) as ydl: if background is None: From 5b4f8d0a0ded3e311d1860524dcf8fc7004e2dd8 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 11:37:02 +0800 Subject: [PATCH 18/83] little fixes --- main.py | 6 +++--- video_creation/final_video.py | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 980a6b2..36e1c1c 100644 --- a/main.py +++ b/main.py @@ -9,7 +9,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 +from utils.console import print_markdown, print_substep def main(subreddit_=None, background=None): @@ -24,7 +24,7 @@ def main(subreddit_=None, background=None): print_markdown( "### Thanks for using this tool! [Feel free to contribute to this project on " + "GitHub!](https://lewismenelaws.com) If you have any questions, feel free to" - + "reach out to me on Twitter or submit a GitHub issue." + + " reach out to me on Twitter or submit a GitHub issue." ) configured = True @@ -36,7 +36,7 @@ def main(subreddit_=None, background=None): for val in REQUIRED_VALUES: if not os.getenv(val): - print(f"Please set the variable \"{val}\" in your .env file.") + print_substep(f"Please set the variable \"{val}\" in your .env file.", style="bold red") configured = False if configured: diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 0e18d28..b3f9ae2 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -20,7 +20,6 @@ W, H = 1080, 1920 def make_final_video(number_of_clips): - # Calls opacity from the .env load_dotenv() print_step("Creating the final video...") From 8e4d6ac4bfe4d1e536d49a6e11b67948ceacaf8f Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 11:55:21 +0800 Subject: [PATCH 19/83] fixed the regex pattern --- video_creation/background.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/video_creation/background.py b/video_creation/background.py index 6ad2769..317e878 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -39,7 +39,7 @@ def download_background(background): ydl.download("https://www.youtube.com/watch?v=n_Dv4JMiwK8") else: if ( - re.match("https://*youtube.com*", background) + re.match("https://www.youtube.com/watch?v*", background) and background is not None ): ydl.download(background) From 9230abb8a0d7b90efa682d2589dabc59980b2961 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 11:59:34 +0800 Subject: [PATCH 20/83] fixed conditions to download youtube link --- video_creation/background.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/video_creation/background.py b/video_creation/background.py index 317e878..a1897c6 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -1,6 +1,7 @@ import re from random import randrange from pathlib import Path +from turtle import back from yt_dlp import YoutubeDL from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip @@ -31,20 +32,23 @@ def download_background(background): background_check = Path("assets/mp4/background.mp4").is_file() if background is not None or not background_check: if background_check: - print_substep("Background video is already downloaded! Replacing ...") + print_substep( + "Background video is already downloaded! Replacing ...", style="bold red" + ) try: with YoutubeDL(ydl_opts) as ydl: if background is None: ydl.download("https://www.youtube.com/watch?v=n_Dv4JMiwK8") - else: + elif background is not None: if ( - re.match("https://www.youtube.com/watch?v*", background) + re.match("https://www.youtube.com/watch?v*", background.strip()) and background is not None ): + print_substep(f"Downloading video from: {background}", style="bold") ydl.download(background) - else: # if the link is not youtube link - raise ValueError + else: # if the link is not youtube link + raise ValueError except ( ValueError # add more exceptions From e9d3f63edadb691a711442c047d1cceb8956dbe0 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 12:06:29 +0800 Subject: [PATCH 21/83] use os to remove the already downloaded background --- video_creation/background.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/video_creation/background.py b/video_creation/background.py index a1897c6..12f5fe8 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -1,7 +1,7 @@ import re +import os from random import randrange from pathlib import Path -from turtle import back from yt_dlp import YoutubeDL from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip @@ -31,10 +31,11 @@ def download_background(background): background_check = Path("assets/mp4/background.mp4").is_file() if background is not None or not background_check: - if background_check: + if background_check and background is not None: print_substep( "Background video is already downloaded! Replacing ...", style="bold red" ) + os.system("rm assets/mp4/background.mp4") try: with YoutubeDL(ydl_opts) as ydl: From e4bab8fb5155ed89a1a2e9f261c873a03ac25756 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 12:12:36 +0800 Subject: [PATCH 22/83] marked some tasks --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index dbb0250..374743c 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,8 @@ In its current state, this bot does exactly what it needs to do. However, lots o I have tried to simplify the code so anyone can read it and start contributing at any skill level. Don't be shy :) contribute! - [ ] Allowing users to choose a reddit thread instead of being randomized. -- [ ] Allowing users to choose a background that is picked instead of the Minecraft one. +- [X] Allowing users to choose a background that is picked instead of the Minecraft one. - [x] Allowing users to choose between any subreddit. - [ ] Allowing users to change voice. -- [ ] Creating better documentation and adding a command line interface. +- [ ] Creating better documentation. +- [X] Adding a command line interface. From d6c72876301cf02881b1d4ae2cb73d305bf20561 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 12:30:53 +0800 Subject: [PATCH 23/83] include exceptions --- video_creation/final_video.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 6035081..bdf36ad 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -12,7 +12,7 @@ from moviepy.editor import ( CompositeVideoClip, ) -import reddit.subreddit +import reddit.subreddit from utils.console import print_step @@ -23,7 +23,7 @@ def make_final_video(number_of_clips): # Calls opacity from the .env load_dotenv() opacity = os.getenv('OPACITY') - + print_step("Creating the final video...") VideoFileClip.reW = lambda clip: clip.resize(width=W) @@ -38,7 +38,11 @@ def make_final_video(number_of_clips): try: opacity = float(os.getenv("OPACITY")) - except: + except ( + ValueError, + FloatingPointError, + TypeError + ): print(f"Please ensure that OPACITY is set between 0 and 1 in your .env file") configured = False From 231a3497f5c88d89b4aef243d8ff64d603fc498a Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 12:36:49 +0800 Subject: [PATCH 24/83] improvements --- reddit/subreddit.py | 2 +- video_creation/screenshot_downloader.py | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index d549bb7..be85874 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -9,7 +9,6 @@ from utils.console import print_step, print_substep def get_subreddit_threads(subreddit_): - global submission """ Takes subreddit_ as parameter which defaults to None, but in this case since it is None, it would raise ValueError, thus defaulting @@ -18,6 +17,7 @@ def get_subreddit_threads(subreddit_): Returns a list of threads from the AskReddit subreddit. """ + global submission load_dotenv() print_step("Getting AskReddit threads...") diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 72b65ff..c7d9abd 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -19,16 +19,16 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme): # ! Make sure the reddit screenshots folder exists Path("assets/png").mkdir(parents=True, exist_ok=True) - with sync_playwright() as p: + with sync_playwright() as browser_: print_substep("Launching Headless Browser...") - browser = p.chromium.launch() + browser = browser_.chromium.launch() context = browser.new_context() if theme.casefold() == "dark": - cookie_file = open("video_creation/cookies.json") - cookies = json.load(cookie_file) - context.add_cookies(cookies) + with open("video_creation/cookies.json", encoding="utf-8") as cookie_file: + cookies = json.load(cookie_file) + context.add_cookies(cookies) # Get the thread screenshot page = context.new_page() @@ -45,8 +45,8 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme): ) for idx, comment in track( - enumerate(reddit_object["comments"]), "Downloading screenshots..." - ): + enumerate(reddit_object["comments"]), "Downloading screenshots..." + ): # Stop if we have reached the screenshot_num if idx >= screenshot_num: From f9b47d040e6407bda14202c0c2c536ce21ff13af Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 12:43:09 +0800 Subject: [PATCH 25/83] removed setup.py --- setup.py | 42 ------------------------------------------ 1 file changed, 42 deletions(-) delete mode 100644 setup.py diff --git a/setup.py b/setup.py deleted file mode 100644 index c61aa9d..0000000 --- a/setup.py +++ /dev/null @@ -1,42 +0,0 @@ -from setuptools import setup, find_packages - - -with open("README.md", "r", encoding="utf-8") as readme: - description = readme.read() - -setup( - name="RedditVideoMakerBot", - version="1.0", # change this if it is wrong, - description="Create a Reddit video with Python!", - long_description=description, - long_description_content_type="text/markdown", - url="https://github.com/iaacornus/scpterm", - py_modules=[ - "main", - "cli", - "reddit/subreddit", - "utils/console", - "video_creation/background", - "video_creation/final_video", - "video_creation/screenshot_downloader", - "video_creation/voices" - ], - include_package_data=True, - packages=find_packages(), - python_requires=">=3.6", - install_requires=[ - "gTTS==2.2.4", - "rich<=12.4.4", - "moviepy==1.0.3", - "mutagen==1.45.1", - "playwright==1.22.0", - "praw==7.6.0", - "python-dotenv==0.20.0", - "yt_dlp==2022.5.18" - ], - entry_points={ - "console_scripts" : [ - "RedditVideoMakerBot=cli:program_options", - ] - }, -) From e86481e15043db93734a0c276888454cf2815008 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 12:51:01 +0800 Subject: [PATCH 26/83] include strip() method to avoid errors due to trailing spaces --- reddit/subreddit.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index be85874..9d0c93d 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -34,11 +34,11 @@ def get_subreddit_threads(subreddit_): content = {} reddit = praw.Reddit( - client_id=os.getenv("REDDIT_CLIENT_ID"), - client_secret=os.getenv("REDDIT_CLIENT_SECRET"), + client_id=os.getenv("REDDIT_CLIENT_ID").strip(), + client_secret=os.getenv("REDDIT_CLIENT_SECRET").strip(), user_agent="Accessing AskReddit threads", - username=os.getenv("REDDIT_USERNAME"), - password=passkey, + username=os.getenv("REDDIT_USERNAME").strip(), + password=passkey.strip(), ) try: @@ -48,7 +48,7 @@ def get_subreddit_threads(subreddit_): subreddit = reddit.subreddit(subreddit_) except ValueError: if os.getenv("SUBREDDIT"): - subreddit = reddit.subreddit(re.sub(r"r\/", "", os.getenv("SUBREDDIT"))) + subreddit = reddit.subreddit(re.sub(r"r\/", "", os.getenv("SUBREDDIT").strip())) else: subreddit = reddit.subreddit("askreddit") print_substep("Subreddit not defined. Using AskReddit.") From a824182be8bea89d1c87bb1709e964f29add08d5 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 12:57:10 +0800 Subject: [PATCH 27/83] use strip() method to remove trailing whitespaces that could cause errors --- reddit/subreddit.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index be85874..9d0c93d 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -34,11 +34,11 @@ def get_subreddit_threads(subreddit_): content = {} reddit = praw.Reddit( - client_id=os.getenv("REDDIT_CLIENT_ID"), - client_secret=os.getenv("REDDIT_CLIENT_SECRET"), + client_id=os.getenv("REDDIT_CLIENT_ID").strip(), + client_secret=os.getenv("REDDIT_CLIENT_SECRET").strip(), user_agent="Accessing AskReddit threads", - username=os.getenv("REDDIT_USERNAME"), - password=passkey, + username=os.getenv("REDDIT_USERNAME").strip(), + password=passkey.strip(), ) try: @@ -48,7 +48,7 @@ def get_subreddit_threads(subreddit_): subreddit = reddit.subreddit(subreddit_) except ValueError: if os.getenv("SUBREDDIT"): - subreddit = reddit.subreddit(re.sub(r"r\/", "", os.getenv("SUBREDDIT"))) + subreddit = reddit.subreddit(re.sub(r"r\/", "", os.getenv("SUBREDDIT").strip())) else: subreddit = reddit.subreddit("askreddit") print_substep("Subreddit not defined. Using AskReddit.") From 2ad99f41eff424b73680bbff41e50bc1ed8c29bd Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 13:03:07 +0800 Subject: [PATCH 28/83] features for custom filename as listed in #225 --- cli.py | 12 +++++++++++- main.py | 9 ++++++--- video_creation/final_video.py | 10 ++++++---- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/cli.py b/cli.py index e0dfcb4..79ad3ec 100644 --- a/cli.py +++ b/cli.py @@ -34,12 +34,22 @@ def program_options(): help="Use another video background for video (accepts link).", action="store" ) + parser.add_argument( + "-f", + "--filename", + help="Set a filename for the video.", + action="store" + ) args = parser.parse_args() try: if args.create: - main(args.subreddit, args.background) + main( + args.subreddit, + args.background, + args.filename + ) else: print_substep("Error occured!", style="bold red") raise SystemExit() diff --git a/main.py b/main.py index 36e1c1c..c780dde 100644 --- a/main.py +++ b/main.py @@ -12,7 +12,7 @@ from video_creation.final_video import make_final_video from utils.console import print_markdown, print_substep -def main(subreddit_=None, background=None): +def main(subreddit_=None, background=None, filename=None): REQUIRED_VALUES = [ "REDDIT_CLIENT_ID", "REDDIT_CLIENT_SECRET", @@ -42,7 +42,10 @@ def main(subreddit_=None, background=None): if configured: reddit_object = get_subreddit_threads(subreddit_) length, number_of_comments = save_text_to_mp3(reddit_object) - download_screenshots_of_reddit_posts(reddit_object, number_of_comments, os.getenv("THEME")) + download_screenshots_of_reddit_posts( + reddit_object, number_of_comments, + os.getenv("THEME") + ) download_background(background) chop_background_video(length) - final_video = make_final_video(number_of_comments) + final_video = make_final_video(snumber_of_comments, filename) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index bdf36ad..599026c 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -19,7 +19,7 @@ from utils.console import print_step W, H = 1080, 1920 -def make_final_video(number_of_clips): +def make_final_video(number_of_clips, file_name): # Calls opacity from the .env load_dotenv() opacity = os.getenv('OPACITY') @@ -80,9 +80,11 @@ def make_final_video(number_of_clips): image_concat.audio = audio_composite final = CompositeVideoClip([background_clip, image_concat]) - filename = ( - re.sub('[?\"%*:|<>]', '', ("assets/" + reddit.subreddit.submission.title + ".mp4")) - ) + if file_name is None: + filename = ( + re.sub('[?\"%*:|<>]', '', ("assets/" + reddit.subreddit.submission.title + ".mp4")) + ) + final.write_videofile(filename, fps=30, audio_codec="aac", audio_bitrate="192k") for i in range(0, number_of_clips): pass From f589ca7b6faa8ee0c4d105fb1f5ce60d1db45a97 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 13:21:02 +0800 Subject: [PATCH 29/83] take file as custom background input --- cli.py | 2 -- video_creation/background.py | 12 ++++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/cli.py b/cli.py index e0dfcb4..ccaec75 100644 --- a/cli.py +++ b/cli.py @@ -26,8 +26,6 @@ def program_options(): help="Use another sub-reddit.", action="store" ) - # this one accepts link as input, defaults to the minecraft video - # does not accept file or already downloaded background yet. parser.add_argument( "-b", "--background", diff --git a/video_creation/background.py b/video_creation/background.py index 12f5fe8..7dfa22f 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -2,6 +2,7 @@ import re import os from random import randrange from pathlib import Path +from turtle import back from yt_dlp import YoutubeDL from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip @@ -42,12 +43,15 @@ def download_background(background): if background is None: ydl.download("https://www.youtube.com/watch?v=n_Dv4JMiwK8") elif background is not None: - if ( - re.match("https://www.youtube.com/watch?v*", background.strip()) - and background is not None - ): + check_link = re.match( + "https://www.youtube.com/watch?v*", background.strip() + ) + if check_link and background is not None: print_substep(f"Downloading video from: {background}", style="bold") ydl.download(background) + elif background is not None and not check_link: + print_substep(f"Using the given video file: {background}", style="bold") + os.replace(background.strip(), "assets/mp4/background.mp4") else: # if the link is not youtube link raise ValueError except ( From fa664369b977d91b5bc8fb94c3b35a352cf90a8d Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sun, 5 Jun 2022 13:21:19 +0800 Subject: [PATCH 30/83] fixed indentation --- reddit/subreddit.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 9d0c93d..ffeaccb 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -64,13 +64,13 @@ def get_subreddit_threads(subreddit_): for top_level_comment in submission.comments: if not top_level_comment.stickied: - content["comments"].append( - { - "comment_body": top_level_comment.body, - "comment_url": top_level_comment.permalink, - "comment_id": top_level_comment.id, - } - ) + 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 From 32e07e71181f24e84fecf635226ed2aa3357f53d Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 17:47:30 +0800 Subject: [PATCH 31/83] from merge --- .env.template | 5 +++ .gitignore | 9 +++- CONTRIBUTING.md | 109 ++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 37 +++++++++++----- 4 files changed, 149 insertions(+), 11 deletions(-) create mode 100644 CONTRIBUTING.md diff --git a/.env.template b/.env.template index 90059f9..e95c209 100644 --- a/.env.template +++ b/.env.template @@ -1,10 +1,15 @@ REDDIT_CLIENT_ID="" REDDIT_CLIENT_SECRET="" + REDDIT_USERNAME="" REDDIT_PASSWORD="" + # 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="yes" + # Valid options are "light" and "dark" THEME="" diff --git a/.gitignore b/.gitignore index 52b1988..7bacc23 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,12 @@ assets/ +reddit/__pycache__/ +utils/__pycache__/ .env reddit-bot-351418-5560ebc49cac.json +video_creation/__pycache__/ +.setup-done-before __pycache__ -out \ No newline at end of file +.idea/ +.DS_Store +out +venv diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..e17be29 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,109 @@ +# Contributing to Reddit Video Maker Bot πŸŽ₯ + +Thanks for taking the time to contribute! ❀️ + +All types of contributions are encouraged and valued. See the [Table of Contents](#table-of-contents) for different ways to help and details about how this project handles them. Please make sure to read the relevant section before making your contribution. It will make it a lot easier for the maintainers and smooth out the experience for all involved. We are looking forward to your contributions. πŸŽ‰ + +> And if you like the project, but just don't have time to contribute, that's fine. There are other easy ways to support the project and show your appreciation, which we would also be very happy about: +> +> - Star the project +> - Tweet about it +> - Refer this project in your project's readme + +## Table of Contents + +- [I Have a Question](#i-have-a-question) +- [I Want To Contribute](#i-want-to-contribute) +- [Reporting Bugs](#reporting-bugs) +- [Suggesting Enhancements](#suggesting-enhancements) +- [Your First Code Contribution](#your-first-code-contribution) +- [Improving The Documentation](#improving-the-documentation) + +## I Have a Question + +> If you want to ask a question, we assume that you have read the available [Documentation](https://luka-hietala.gitbook.io/documentation-for-the-reddit-bot/). + +Before you ask a question, it is best to search for existing [Issues](https://github.com/elebumm/RedditVideoMakerBot/issues) that might help you. In case you have found a suitable issue and still need clarification, you can write your question in this issue. It is also advisable to search the internet for answers first. + +If you then still feel the need to ask a question and need clarification, we recommend the following: + +- Open an [Issue](https://github.com/elebumm/RedditVideoMakerBot/issues/new). +- Provide as much context as you can about what you're running into. +- Provide project and platform versions (nodejs, npm, etc), depending on what seems relevant. + +We will then take care of the issue as soon as possible. + +Additionally, there is a [discord channel](https://discord.gg/swqtb7AsNQ) for any questions you may have + +## I Want To Contribute + +### Reporting Bugs + +#### Before Submitting a Bug Report + +A good bug report shouldn't leave others needing to chase you up for more information. Therefore, we ask you to investigate carefully, collect information and describe the issue in detail in your report. Please complete the following steps in advance to help us fix any potential bug as fast as possible. + +- Make sure that you are using the latest version. +- Determine if your bug is really a bug and not an error on your side e.g. using incompatible environment components/versions (Make sure that you have read the [documentation](https://luka-hietala.gitbook.io/documentation-for-the-reddit-bot/). If you are looking for support, you might want to check [this section](#i-have-a-question)). +- To see if other users have experienced (and potentially already solved) the same issue you are having, check if there is not already a bug report existing for your bug or error in the [issues](https://github.com/elebumm/RedditVideoMakerBot/). +- Also make sure to search the internet (including Stack Overflow) to see if users outside of the GitHub community have discussed the issue - you probably aren't the first to get the error! +- Collect information about the bug: + - Stack trace (Traceback) - preferably formatted in a code block. + - OS, Platform and Version (Windows, Linux, macOS, x86, ARM) + - Version of the interpreter, compiler, SDK, runtime environment, package manager, depending on what seems relevant. + - Your input and the output + - Is the issue reproducable? Does it exist in previous versions? + +#### How Do I Submit a Good Bug Report? + +We use GitHub issues to track bugs and errors. If you run into an issue with the project: + +- Open an [Issue](https://github.com/elebumm/RedditVideoMakerBot/issues/new). (Since we can't be sure at this point whether it is a bug or not, we ask you not to talk about a bug yet and not to label the issue.) +- Explain the behavior you would expect and the actual behavior. +- Please provide as much context as possible and describe the _reproduction steps_ that someone else can follow to recreate the issue on their own. This usually includes your code. For good bug reports you should isolate the problem and create a reduced test case. +- Provide the information you collected in the previous section. + +Once it's filed: + +- The project team will label the issue accordingly. +- A team member will try to reproduce the issue with your provided steps. If there are no reproduction steps or no obvious way to reproduce the issue, the team will try to support you as best as they can, but you may not recieve an instant. +- If the team discovers that this is an issue it will be marked `bug` or `error`, as well as possibly other tags relating to the nature of the error), and the issue will be left to be [implemented by someone](#your-first-code-contribution). + +### Suggesting Enhancements + +This section guides you through submitting an enhancement suggestion for Reddit Video Maker Bot, **including completely new features and minor improvements to existing functionality**. Following these guidelines will help maintainers and the community to understand your suggestion and find related suggestions. + +#### Before Submitting an Enhancement + +- Make sure that you are using the latest version. +- Read the [documentation](https://luka-hietala.gitbook.io/documentation-for-the-reddit-bot/) carefully and find out if the functionality is already covered, maybe by an individual configuration. +- Perform a [search](https://github.com/elebumm/RedditVideoMakerBot/issues) to see if the enhancement has already been suggested. If it has, add a comment to the existing issue instead of opening a new one. +- Find out whether your idea fits with the scope and aims of the project. It's up to you to make a strong case to convince the project's developers of the merits of this feature. Keep in mind that we want features that will be useful to the majority of our users and not just a small subset. + +#### How Do I Submit a Good Enhancement Suggestion? + +Enhancement suggestions are tracked as [GitHub issues](https://github.com/elebumm/RedditVideoMakerBot/issues). + +- Use a **clear and descriptive title** for the issue to identify the suggestion. +- Provide a **step-by-step description of the suggested enhancement** in as many details as possible. +- **Describe the current behavior** and **explain which behavior you expected to see instead** and why. At this point you can also tell which alternatives do not work for you. +- You may want to **include screenshots and animated GIFs** which help you demonstrate the steps or point out the part which the suggestion is related to. You can use [this tool](https://www.cockos.com/licecap/) to record GIFs on macOS and Windows, and [this tool](https://github.com/colinkeenan/silentcast) or [this tool](https://github.com/GNOME/byzanz) on Linux. +- **Explain why this enhancement would be useful** to most users. You may also want to point out the other projects that solved it better and which could serve as inspiration. + +### Your First Code Contribution + +#### Your environment + +You development environment should follow the requirements stated in the [README file](README.md). If you are not using the specified versions, **please reference this in your pull request**, so reviewers can test your code on both versions. + +#### Making your first PR + +When making your PR, follow these guidelines: + +- Your branch has a base of _develop_ **not** _master_ +- You are merging your branch into the _develop_ branch +- You link any issues that are resolved or fixed by your changes. (this is done by typing "Fixes #\") in your pull request. + +### Improving The Documentation + +All updates to the documentation should be made in a pull request to [this repo](https://github.com/LukaHietala/reddit-bot-docs) diff --git a/README.md b/README.md index 374743c..b610d03 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ All done WITHOUT video editing or asset compiling. Just pure ✨programming magi Created by Lewis Menelaws & [TMRRW](https://tmrrwinc.ca) [ + @@ -20,25 +21,31 @@ These videos on TikTok, YouTube and Instagram get MILLIONS of views across all p ## Disclaimers 🚨 -- This is purely for fun purposes. -- **At the moment**, this repository won't attempt to upload this content through this bot. It will give you a file that you will then have to upload manually. This is for the sake of avoiding any sort of community guideline issues. +- This is purely for fun purposes. +- **At the moment**, this repository won't attempt to upload this content through this bot. It will give you a file that you will then have to upload manually. This is for the sake of avoiding any sort of community guideline issues. ## Requirements -- Python 3.6+ -- Playwright (this should install automatically during installation) +- Python 3.6+ +- Playwright (this should install automatically during installation) ## Installation πŸ‘©β€πŸ’» 1. Clone this repository -2. Rename `.env.template` to `.env` and replace all values with the appropriate fields. To get Reddit keys (**required**), visit [the Reddit Apps page.](https://www.reddit.com/prefs/apps) TL;DR set up an app that is a "script". Copy your keys into the `.env` file, along with whether your account uses two-factor authentication. -3. Run `pip3 install -r requirements.txt` -4. Run `playwright install` and `playwright install-deps`. -5. Run `python3 main.py` -6. Enjoy 😎 + +2. Run `pip3 install -r requirements.txt` +3. Run `playwright install` and `playwright install-deps`. +4. + 4a **Automatic Install**: Run `python3 main.py` and type 'yes' to activate the setup assistant. + + 4b **Manual Install**: Rename `.env.template` to `.env` and replace all values with the appropriate fields. To get Reddit keys (**required**), visit [the Reddit Apps page.](https://www.reddit.com/prefs/apps) TL;DR set up an app that is a "script". Copy your keys into the `.env` file, along with whether your account uses two-factor authentication. + +5. Run `python3 main.py` (unless you chose automatic install, then the installer will automatically run main.py) +7. Enjoy 😎 + If you want to see more detailed guide, please refer to the official [documentation](https://luka-hietala.gitbook.io/documentation-for-the-reddit-bot/). -*The Documentation is still being developed and worked on, please be patient as we change / add new knowledge! +\*The Documentation is still being developed and worked on, please be patient as we change / add new knowledge! ## Contributing & Ways to improve πŸ“ˆ @@ -46,9 +53,19 @@ In its current state, this bot does exactly what it needs to do. However, lots o I have tried to simplify the code so anyone can read it and start contributing at any skill level. Don't be shy :) contribute! +<<<<<<< HEAD - [ ] Allowing users to choose a reddit thread instead of being randomized. - [X] Allowing users to choose a background that is picked instead of the Minecraft one. - [x] Allowing users to choose between any subreddit. - [ ] Allowing users to change voice. - [ ] Creating better documentation. - [X] Adding a command line interface. +======= +- [x] Allowing users to choose a reddit thread instead of being randomized. +- [ ] Allowing users to choose a background that is picked instead of the Minecraft one. +- [x] Allowing users to choose between any subreddit. +- [ ] Allowing users to change voice. +- [ ] Creating better documentation and adding a command line interface. + +Please read our [contributing guidelines](CONTRIBUTING.md) for more detailed information. +>>>>>>> cf6b3a9c44d86909f585a3111d62005cd05c8f7d From 8338e28188ec647e96171154dc0e79923f8bf1b9 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 17:59:36 +0800 Subject: [PATCH 32/83] complied with pep8, and turned the setup into function for call in cli --- setup.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 setup.py diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..09f2ae8 --- /dev/null +++ b/setup.py @@ -0,0 +1,96 @@ +# Imports +import os +import time +from os.path import exists + +from rich.console import Console + +from utils.console import print_markdown +from utils.console import print_step + +from utils.loader import Loader + + +console = Console() + + +def setup(): + if exists(".setup-done-before"): + console.log( + "[bold red]Setup was already done before! Please" + + " make sure you have to run this script again.[/bold red]" + ) + # to run the setup script again, but in better and more convenient manner. + if input("\033[1mRun the script again? [y/N] > \033[0m") not in ["y", "Y"]: + raise SystemExit() + + + print_step("Setup Assistant") + print_markdown( + "### You're in the setup wizard. Ensure you're supposed to be here, " + + "then type yes to continue. If you're not sure, type no to quit." + ) + + # This Input is used to ensure the user is sure they want to continue. + # Again, let them know they are about to erase all other setup data. + console.log( + "Ensure you have the following ready to enter:\n" + + "[bold green]Reddit Client ID\n" + + "Reddit Client Secret\n" + + "Reddit Username\n" + + "Reddit Password\n" + + "Reddit 2FA (yes or no)\n" + + "Opacity (range of 0-1, decimals are accepted.)\n" + + "Subreddit (without r/ or /r/)\n" + + "Theme (light or dark)[/bold green]" + ) + console.print( + "[bold]If you don't have these, please follow the instructions in the README.md file to " + + "set them up.\n If you do have these, type yes to continue. If you don't, go ahead and " + + "grab those quickly and come back.[/bold]" + ) + + """Begin the setup process.""" + + cliID = input("Client ID > ") + cliSec = input("Client Secret > ") + user = input("Username > ") + passw = input("Password > ") + twofactor = input("2FA Enabled? (yes/no) > ") + opacity = input("Opacity? (range of 0-1) > ") + subreddit = input("Subreddit (without r/) > ") + theme = input("Theme? (light or dark) > ") + console.log("Attempting to save your credentials...") + + loader = Loader("Saving Credentials...", "Done!").start() + # you can also put a while loop here, e.g. while VideoIsBeingMade == True: ... + time.sleep(0.5) + console.log("Removing old .env file...") + os.remove(".env") + time.sleep(0.5) + console.log("Creating new .env file...") + with open('.env', 'a') as f: + f.write(f'REDDIT_CLIENT_ID="{cliID}"\n') + time.sleep(0.5) + f.write(f'REDDIT_CLIENT_SECRET="{cliSec}"\n') + time.sleep(0.5) + f.write(f'REDDIT_USERNAME="{user}"\n') + time.sleep(0.5) + f.write(f'REDDIT_PASSWORD="{passw}"\n') + time.sleep(0.5) + f.write(f'REDDIT_2FA="{twofactor}"\n') + time.sleep(0.5) + f.write(f'THEME="{theme}"\n') + time.sleep(0.5) + f.write(f'SUBREDDIT="{subreddit}"\n') + time.sleep(0.5) + f.write(f'OPACITY="{opacity}"\n') + + with open(".setup-done-before", "a", encoding="utf-8") as f: + f.write( + "This file will stop the setup assistant from running again." + ) + + loader.stop() + + console.log("[bold green]Setup Complete![/bold green]") From e076492a55284438b2d6e77c54cc6c4c97dbf98c Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 18:00:44 +0800 Subject: [PATCH 33/83] removed the stupid time.sleep() (s) --- setup.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/setup.py b/setup.py index 09f2ae8..b9b58b2 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,5 @@ # Imports import os -import time from os.path import exists from rich.console import Console @@ -64,26 +63,17 @@ def setup(): loader = Loader("Saving Credentials...", "Done!").start() # you can also put a while loop here, e.g. while VideoIsBeingMade == True: ... - time.sleep(0.5) console.log("Removing old .env file...") os.remove(".env") - time.sleep(0.5) console.log("Creating new .env file...") - with open('.env', 'a') as f: + with open('.env', 'a', encoding="utf-8") as f: f.write(f'REDDIT_CLIENT_ID="{cliID}"\n') - time.sleep(0.5) f.write(f'REDDIT_CLIENT_SECRET="{cliSec}"\n') - time.sleep(0.5) f.write(f'REDDIT_USERNAME="{user}"\n') - time.sleep(0.5) f.write(f'REDDIT_PASSWORD="{passw}"\n') - time.sleep(0.5) f.write(f'REDDIT_2FA="{twofactor}"\n') - time.sleep(0.5) f.write(f'THEME="{theme}"\n') - time.sleep(0.5) f.write(f'SUBREDDIT="{subreddit}"\n') - time.sleep(0.5) f.write(f'OPACITY="{opacity}"\n') with open(".setup-done-before", "a", encoding="utf-8") as f: From 65f74f586ba38848c87f18dacf5477832a4e0433 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 18:09:35 +0800 Subject: [PATCH 34/83] removed the complex code about user given thread link, this would instead use the -t parameter in command line --- reddit/subreddit.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index ffeaccb..c85ba08 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -8,7 +8,7 @@ from dotenv import load_dotenv from utils.console import print_step, print_substep -def get_subreddit_threads(subreddit_): +def get_subreddit_threads(subreddit_, thread_link_): """ Takes subreddit_ as parameter which defaults to None, but in this case since it is None, it would raise ValueError, thus defaulting @@ -20,8 +20,6 @@ def get_subreddit_threads(subreddit_): global submission load_dotenv() - print_step("Getting AskReddit threads...") - if os.getenv("REDDIT_2FA", default="no").casefold() == "yes": print( "\nEnter your two-factor authentication code from your authenticator app.\n" @@ -53,17 +51,39 @@ def get_subreddit_threads(subreddit_): subreddit = reddit.subreddit("askreddit") print_substep("Subreddit not defined. Using AskReddit.") - threads = subreddit.hot(limit=25) - submission = list(threads)[random.randrange(0, 25)] + # 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 thread_link_ is not None: + thread_link = thread_link_ + print_step(f"Getting the inserted thread...") + submission = reddit.submission(url=thread_link) + else: + # Otherwise, picks a random thread from the inserted subreddit + if os.getenv("SUBREDDIT"): + subreddit = reddit.subreddit(re.sub(r"r\/", "", os.getenv("SUBREDDIT"))) + else: + # ! Prompt the user to enter a subreddit + try: + subreddit = reddit.subreddit( + re.sub(r"r\/", "",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 + content["thread_post"] = submission.selftext content["comments"] = [] for top_level_comment in submission.comments: - if not top_level_comment.stickied: + if not top_level_comment.stickied: content["comments"].append( { "comment_body": top_level_comment.body, From f1cb1a3cbca7c7f9e041bda3ecea20d32306c692 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 18:10:55 +0800 Subject: [PATCH 35/83] removed the duplicate code blocks --- reddit/subreddit.py | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index c85ba08..1c1dcd5 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -39,17 +39,7 @@ def get_subreddit_threads(subreddit_, thread_link_): password=passkey.strip(), ) - try: - if subreddit_ is None: - raise ValueError - subreddit = reddit.subreddit(subreddit_) - except ValueError: - if os.getenv("SUBREDDIT"): - subreddit = reddit.subreddit(re.sub(r"r\/", "", os.getenv("SUBREDDIT").strip())) - else: - subreddit = reddit.subreddit("askreddit") - print_substep("Subreddit not defined. Using AskReddit.") # 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 @@ -58,16 +48,17 @@ def get_subreddit_threads(subreddit_, thread_link_): print_step(f"Getting the inserted thread...") submission = reddit.submission(url=thread_link) else: - # Otherwise, picks a random thread from the inserted subreddit - if os.getenv("SUBREDDIT"): - subreddit = reddit.subreddit(re.sub(r"r\/", "", os.getenv("SUBREDDIT"))) - else: - # ! Prompt the user to enter a subreddit - try: + try: + if subreddit_ is None: + raise ValueError + + subreddit = reddit.subreddit(subreddit_) + except ValueError: + if os.getenv("SUBREDDIT"): subreddit = reddit.subreddit( - re.sub(r"r\/", "",input("What subreddit would you like to pull from? ")) + re.sub(r"r\/", "", os.getenv("SUBREDDIT").strip()) ) - except ValueError: + else: subreddit = reddit.subreddit("askreddit") print_substep("Subreddit not defined. Using AskReddit.") From 571a6b0c2fb0439a68ff048ca6f06945fcfc945d Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 18:13:34 +0800 Subject: [PATCH 36/83] used console.status() instead of loader.py, since this is more efficient and would reduce the complexity of the codebase --- setup.py | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/setup.py b/setup.py index b9b58b2..06a8d60 100644 --- a/setup.py +++ b/setup.py @@ -7,8 +7,6 @@ from rich.console import Console from utils.console import print_markdown from utils.console import print_step -from utils.loader import Loader - console = Console() @@ -61,26 +59,24 @@ def setup(): theme = input("Theme? (light or dark) > ") console.log("Attempting to save your credentials...") - loader = Loader("Saving Credentials...", "Done!").start() - # you can also put a while loop here, e.g. while VideoIsBeingMade == True: ... - console.log("Removing old .env file...") - os.remove(".env") - console.log("Creating new .env file...") - with open('.env', 'a', encoding="utf-8") as f: - f.write(f'REDDIT_CLIENT_ID="{cliID}"\n') - f.write(f'REDDIT_CLIENT_SECRET="{cliSec}"\n') - f.write(f'REDDIT_USERNAME="{user}"\n') - f.write(f'REDDIT_PASSWORD="{passw}"\n') - f.write(f'REDDIT_2FA="{twofactor}"\n') - f.write(f'THEME="{theme}"\n') - f.write(f'SUBREDDIT="{subreddit}"\n') - f.write(f'OPACITY="{opacity}"\n') - - with open(".setup-done-before", "a", encoding="utf-8") as f: - f.write( - "This file will stop the setup assistant from running again." - ) - - loader.stop() + with console.status("[bold magenta]Saving credentials...[/bold magenta]"): + # you can also put a while loop here, e.g. while VideoIsBeingMade == True: ... + console.log("Removing old .env file...") + os.remove(".env") + console.log("Creating new .env file...") + with open('.env', 'a', encoding="utf-8") as f: + f.write(f'REDDIT_CLIENT_ID="{cliID}"\n') + f.write(f'REDDIT_CLIENT_SECRET="{cliSec}"\n') + f.write(f'REDDIT_USERNAME="{user}"\n') + f.write(f'REDDIT_PASSWORD="{passw}"\n') + f.write(f'REDDIT_2FA="{twofactor}"\n') + f.write(f'THEME="{theme}"\n') + f.write(f'SUBREDDIT="{subreddit}"\n') + f.write(f'OPACITY="{opacity}"\n') + + with open(".setup-done-before", "a", encoding="utf-8") as f: + f.write( + "This file will stop the setup assistant from running again." + ) console.log("[bold green]Setup Complete![/bold green]") From 49e9c1c997bf7290a9ee431c8c68a168c18523c2 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 18:16:26 +0800 Subject: [PATCH 37/83] some improvements and fixes to the merge --- video_creation/final_video.py | 42 ++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 599026c..f4b2e8d 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -52,30 +52,40 @@ def make_final_video(number_of_clips, file_name): audio_clips.append(AudioFileClip(f"assets/mp3/{i}.mp3")) audio_clips.insert(0, AudioFileClip("assets/mp3/title.mp3")) + try: + audio_clips.insert(1, AudioFileClip(f"assets/mp3/posttext.mp3")) + except ( + OSError, + FileNotFoundError, + ): + ... + audio_concat = concatenate_audioclips(audio_clips) audio_composite = CompositeAudioClip([audio_concat]) # Gather all images image_clips = [] - for i in range(0, number_of_clips): - image_clips.append( - ImageClip(f"assets/png/comment_{i}.png") - .set_duration(audio_clips[i + 1].duration) + if os.path.exists(f"assets/mp3/posttext.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) - .set_opacity(opacity), - ) + .set_opacity(float(opacity)), + ) + else: + image_clips.insert( + 0, + ImageClip(f"assets/png/title.png") + .set_duration(audio_clips[0].duration) + .set_position("center") + .resize(width=W - 100) + .set_opacity(float(opacity)), + ) - image_clips.insert( - 0, - ImageClip("assets/png/title.png") - .set_duration(audio_clips[0].duration) - .set_position("center") - .resize(width=W - 100) - .set_opacity(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]) From 4bea2372dcc03880d5687221b8af823f1e8f75b3 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 18:17:12 +0800 Subject: [PATCH 38/83] from the merge request, no changes --- video_creation/voices.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/video_creation/voices.py b/video_creation/voices.py index 7e711b6..e2eeb68 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -24,6 +24,16 @@ def save_text_to_mp3(reddit_obj): tts.save("assets/mp3/title.mp3") length += MP3("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") + 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 0e70327bd3ed94f8013bdb254d671bf4552504ec Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 18:22:00 +0800 Subject: [PATCH 39/83] implementation of -t parameter --- cli.py | 9 ++++++++- main.py | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cli.py b/cli.py index bfded52..6377460 100644 --- a/cli.py +++ b/cli.py @@ -38,6 +38,12 @@ def program_options(): help="Set a filename for the video.", action="store" ) + parser.add_argument( + "-t", + "--thread", + help="Use the given thread link instead of randomized.", + action="store" + ) args = parser.parse_args() @@ -46,7 +52,8 @@ def program_options(): main( args.subreddit, args.background, - args.filename + args.filename, + args.thread, ) else: print_substep("Error occured!", style="bold red") diff --git a/main.py b/main.py index 8892358..3e08dcb 100644 --- a/main.py +++ b/main.py @@ -12,7 +12,7 @@ from video_creation.final_video import make_final_video from utils.console import print_markdown, print_substep -def main(subreddit_=None, background=None, filename=None): +def main(subreddit_=None, background=None, filename=None, thread_link_=None): """ Load .env file if exists. If it doesnt exist, print a warning and launch the setup wizard. If there is a .env file, check if the @@ -61,7 +61,7 @@ def main(subreddit_=None, background=None, filename=None): console.log("[bold green]Enviroment Variables are set! Continuing...[/bold green]") - reddit_object = get_subreddit_threads(subreddit_) + reddit_object = get_subreddit_threads(subreddit_, thread_link_) length, number_of_comments = save_text_to_mp3(reddit_object) download_screenshots_of_reddit_posts( reddit_object, From 08f7132f3ce340b252651cf20041498655b3016d Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 18:22:33 +0800 Subject: [PATCH 40/83] little fixes --- video_creation/background.py | 4 +--- video_creation/final_video.py | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/video_creation/background.py b/video_creation/background.py index 7dfa22f..eccb3a5 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -1,8 +1,6 @@ import re import os from random import randrange -from pathlib import Path -from turtle import back from yt_dlp import YoutubeDL from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip @@ -30,7 +28,7 @@ def download_background(background): "merge_output_format": "mp4", } - background_check = Path("assets/mp4/background.mp4").is_file() + background_check = os.path.isfile("assets/mp4/background.mp4") if background is not None or not background_check: if background_check and background is not None: print_substep( diff --git a/video_creation/final_video.py b/video_creation/final_video.py index f4b2e8d..7b1ed02 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -22,7 +22,7 @@ W, H = 1080, 1920 def make_final_video(number_of_clips, file_name): # Calls opacity from the .env load_dotenv() - opacity = os.getenv('OPACITY') + opacity = os.getenv("OPACITY") print_step("Creating the final video...") @@ -44,7 +44,6 @@ def make_final_video(number_of_clips, file_name): TypeError ): print(f"Please ensure that OPACITY is set between 0 and 1 in your .env file") - configured = False # Gather all audio clips audio_clips = [] From 414844c51f689a6fca6237a561594cca3e447ed2 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 18:25:27 +0800 Subject: [PATCH 41/83] improved exception handling --- video_creation/background.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/video_creation/background.py b/video_creation/background.py index eccb3a5..11469aa 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -18,7 +18,8 @@ def download_background(background): """Downloads the background video from youtube. Shoutout to: bbswitzer (https://www.youtube.com/watch?v=n_Dv4JMiwK8) - """ + """ + print_step( # removed minecraft, since the background can be changed according to user input. "Downloading the background video." ) @@ -34,7 +35,7 @@ def download_background(background): print_substep( "Background video is already downloaded! Replacing ...", style="bold red" ) - os.system("rm assets/mp4/background.mp4") + os.remove("assets/mp4/background.mp4") try: with YoutubeDL(ydl_opts) as ydl: @@ -52,11 +53,10 @@ def download_background(background): os.replace(background.strip(), "assets/mp4/background.mp4") else: # if the link is not youtube link raise ValueError - except ( - ValueError - # add more exceptions - ): + except ValueError: print_substep("The given link is not accepted!", style="bold red") + except ConnectionError: + print_substep("There is a connection error!", style="bold red") else: print_substep("Background video downloaded successfully!", style="bold green") From 2198cb6b2ac25e04e950f6d142f8a59a409b1e91 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 18:28:07 +0800 Subject: [PATCH 42/83] better exception handling for background.py --- utils/console.py | 1 + 1 file changed, 1 insertion(+) diff --git a/utils/console.py b/utils/console.py index b565737..3aafbb3 100644 --- a/utils/console.py +++ b/utils/console.py @@ -24,4 +24,5 @@ def print_step(text): def print_substep(text, style=""): """Prints a rich info message without the panelling.""" + console.print(text, style=style) From 1e213b8aba5a342d408e47376cefd6171be90a48 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 18:31:01 +0800 Subject: [PATCH 43/83] renamed to setup_program to avoid confusion from other modules --- setup.py => setup_program.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename setup.py => setup_program.py (100%) diff --git a/setup.py b/setup_program.py similarity index 100% rename from setup.py rename to setup_program.py From 589d4270657e2894a434de480f1c54b0b8c7da0e Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 18:31:12 +0800 Subject: [PATCH 44/83] --setup parameter to setup the program --- cli.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/cli.py b/cli.py index 6377460..8055fa3 100644 --- a/cli.py +++ b/cli.py @@ -1,6 +1,7 @@ import argparse from main import main +from setup_program import setup from utils.console import print_substep @@ -21,7 +22,7 @@ def program_options(): action="store_true" ) parser.add_argument( # only accepts the name of subreddit, not links. - "-S", + "-s", "--subreddit", help="Use another sub-reddit.", action="store" @@ -44,6 +45,12 @@ def program_options(): help="Use the given thread link instead of randomized.", action="store" ) + parser.add_argument( + "--setup", + "--setup", + help="Setup the program.", + action="store_true" + ) args = parser.parse_args() @@ -55,6 +62,8 @@ def program_options(): args.filename, args.thread, ) + elif args.setup: + setup() else: print_substep("Error occured!", style="bold red") raise SystemExit() @@ -62,7 +71,8 @@ def program_options(): ConnectionError, KeyboardInterrupt, ): - ... + print_substep("Error occured!", style="bold red") -program_options() +if __name__ == "__main__": + program_options() From 9e3f3d783007e48b1fd98f7021ec5e6e50ac1d42 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 18:31:41 +0800 Subject: [PATCH 45/83] improved exception handling --- video_creation/final_video.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 7b1ed02..e6fa7da 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -13,14 +13,13 @@ from moviepy.editor import ( ) import reddit.subreddit -from utils.console import print_step +from utils.console import print_step, print_substep W, H = 1080, 1920 def make_final_video(number_of_clips, file_name): - # Calls opacity from the .env load_dotenv() opacity = os.getenv("OPACITY") @@ -57,10 +56,11 @@ def make_final_video(number_of_clips, file_name): OSError, FileNotFoundError, ): - ... - - audio_concat = concatenate_audioclips(audio_clips) - audio_composite = CompositeAudioClip([audio_concat]) + print_substep("An error occured! Aborting.", style="bold red") + raise SystemExit() + else: + audio_concat = concatenate_audioclips(audio_clips) + audio_composite = CompositeAudioClip([audio_concat]) # Gather all images image_clips = [] From fcd5fa19334551f7899c3435554ed3cf3dabeb18 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 20:29:54 +0800 Subject: [PATCH 46/83] add exception handling for OAuthException of prawcore, that it would give better stderr --- cli.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cli.py b/cli.py index 8055fa3..75d8349 100644 --- a/cli.py +++ b/cli.py @@ -1,11 +1,16 @@ import argparse +from prawcore.exceptions import OAuthException +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. """ @@ -72,6 +77,13 @@ def program_options(): KeyboardInterrupt, ): print_substep("Error occured!", style="bold red") + except OAuthException: + console.print( + "There is something wrong with the .env file, kindly check:[/bold]\n" + + "1. ClientID\n" + + "2. ClientSecret\n" + + "3. If these variables are fine, kindly check other variables." + ) if __name__ == "__main__": From cdc757ccb0125115382ee12a6a7c3a6ba3221783 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 20:34:44 +0800 Subject: [PATCH 47/83] include request and response exception to give better output for issues like #295 --- cli.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/cli.py b/cli.py index 75d8349..2e873c7 100644 --- a/cli.py +++ b/cli.py @@ -1,6 +1,10 @@ import argparse -from prawcore.exceptions import OAuthException +from prawcore.exceptions import ( + OAuthException, + ResponseException, + RequestException +) from rich.console import Console from main import main @@ -79,11 +83,18 @@ def program_options(): print_substep("Error occured!", style="bold red") except OAuthException: console.print( - "There is something wrong with the .env file, kindly check:[/bold]\n" + "[bold red]There is something wrong with the .env file, kindly check:[/bold red]\n" + "1. ClientID\n" + "2. ClientSecret\n" + "3. If these variables are fine, kindly check other variables." ) + except ( + RequestException, + ResponseException, + ): + console.print( + "[bold red]Kindly check the kind of application created, it must be script.[/bold red]" + ) if __name__ == "__main__": From d78b0a0c1cfc7af3495f7f6631a243567d896bb4 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 20:41:35 +0800 Subject: [PATCH 48/83] handled downloaderror as reported in #322 although this commit does not address it directly, instead of proceeding further, this would discontinue other processes by raising systemexit --- video_creation/background.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/video_creation/background.py b/video_creation/background.py index 11469aa..2bfca61 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -3,6 +3,7 @@ import os from random import randrange from yt_dlp import YoutubeDL +from yt_dlp.utils import DownloadError from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip from moviepy.editor import VideoFileClip @@ -27,6 +28,7 @@ def download_background(background): ydl_opts = { "outtmpl": "assets/mp4/background.mp4", "merge_output_format": "mp4", + "retries": 3, } background_check = os.path.isfile("assets/mp4/background.mp4") @@ -37,6 +39,7 @@ def download_background(background): ) os.remove("assets/mp4/background.mp4") + cancel = True try: with YoutubeDL(ydl_opts) as ydl: if background is None: @@ -57,8 +60,15 @@ def download_background(background): print_substep("The given link is not accepted!", style="bold red") except ConnectionError: print_substep("There is a connection error!", style="bold red") + except DownloadError: + print_substep("There is a download error!", style="bold red") else: print_substep("Background video downloaded successfully!", style="bold green") + cancel = False + + if cancel: + # to prevent further error and processes from happening + raise SystemExit() def chop_background_video(video_length): From b05143e9551657e58a1fe14aa629f57a56807db7 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 21:08:04 +0800 Subject: [PATCH 49/83] moved the exceptions directly to the modules --- cli.py | 19 ------------------- reddit/subreddit.py | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/cli.py b/cli.py index 2e873c7..9c0fba4 100644 --- a/cli.py +++ b/cli.py @@ -1,10 +1,5 @@ import argparse -from prawcore.exceptions import ( - OAuthException, - ResponseException, - RequestException -) from rich.console import Console from main import main @@ -81,20 +76,6 @@ def program_options(): KeyboardInterrupt, ): print_substep("Error occured!", style="bold red") - except OAuthException: - console.print( - "[bold red]There is something wrong with the .env file, kindly check:[/bold red]\n" - + "1. ClientID\n" - + "2. ClientSecret\n" - + "3. If these variables are fine, kindly check other variables." - ) - except ( - RequestException, - ResponseException, - ): - console.print( - "[bold red]Kindly check the kind of application created, it must be script.[/bold red]" - ) if __name__ == "__main__": diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 1c1dcd5..329b22f 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -3,7 +3,14 @@ import os import re import praw +from prawcore.exceptions import ( + OAuthException, + ResponseException, + RequestException, + BadRequest +) from dotenv import load_dotenv +from rich.console import Console from utils.console import print_step, print_substep @@ -17,12 +24,14 @@ def get_subreddit_threads(subreddit_, thread_link_): Returns a list of threads from the AskReddit subreddit. """ + console = Console() + global submission load_dotenv() if os.getenv("REDDIT_2FA", default="no").casefold() == "yes": print( - "\nEnter your two-factor authentication code from your authenticator app.\n" + "\nEnter your two-factor authentication code from your authenticator app.\n", end=" " ) code = input("> ") pw = os.getenv("REDDIT_PASSWORD") @@ -31,14 +40,27 @@ def get_subreddit_threads(subreddit_, thread_link_): passkey = os.getenv("REDDIT_PASSWORD") content = {} - reddit = praw.Reddit( - client_id=os.getenv("REDDIT_CLIENT_ID").strip(), - client_secret=os.getenv("REDDIT_CLIENT_SECRET").strip(), - user_agent="Accessing AskReddit threads", - username=os.getenv("REDDIT_USERNAME").strip(), - password=passkey.strip(), - ) - + try: + reddit = praw.Reddit( + client_id=os.getenv("REDDIT_CLIENT_ID").strip(), + client_secret=os.getenv("REDDIT_CLIENT_SECRET").strip(), + user_agent="Accessing AskReddit threads", + username=os.getenv("REDDIT_USERNAME").strip(), + password=passkey.strip(), + ) + except ( + OAuthException, + ResponseException, + RequestException, + BadRequest + ): + console.print( + "[bold red]There is something wrong with the .env file, kindly check:[/bold red]\n" + + "1. ClientID\n" + + "2. ClientSecret\n" + + "3. If these variables are fine, kindly check other variables." + + "4. Check if the type of Reddit app created is script (personal use script)." + ) # If the user specifies that he doesnt want a random thread, or if From 44671535539780735e00c34296ea0144b73c2b2a Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 21:17:39 +0800 Subject: [PATCH 50/83] 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") From ef6792de90bb40192adeaddae6c552ec52fe25f0 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 21:44:03 +0800 Subject: [PATCH 51/83] fixed exception handling, removed the connection error --- cli.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/cli.py b/cli.py index 4c6179d..3b479a0 100644 --- a/cli.py +++ b/cli.py @@ -68,11 +68,8 @@ def program_options(): else: print_substep("Error occured!", style="bold red") raise SystemExit() - except ( - ConnectionError, - KeyboardInterrupt, - ): - print_substep("Error occured!", style="bold red") + except KeyboardInterrupt: + print_substep("\nOperation Aborted!", style="bold red") if __name__ == "__main__": From 70a34402d1b49053f4dfdc61c8a935e8679031f5 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 21:44:43 +0800 Subject: [PATCH 52/83] little fixes --- main.py | 2 +- reddit/subreddit.py | 2 +- setup_program.py | 2 +- video_creation/screenshot_downloader.py | 3 +-- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index f1607f8..f1d8c89 100644 --- a/main.py +++ b/main.py @@ -45,7 +45,7 @@ def main(subreddit_=None, background=None, filename=None, thread_link_=None): "[bold red] Your .env file is invalid, or was never created. Standby.[/bold red]" ) - console.print("[bold green]Checking environment variables...[/bol green]") + console.print("[bold green]Checking environment variables...[/bold green]") for val in REQUIRED_VALUES: if not os.getenv(val): diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 329b22f..13293df 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -87,7 +87,7 @@ def get_subreddit_threads(subreddit_, thread_link_): threads = subreddit.hot(limit=25) submission = list(threads)[random.randrange(0, 25)] - print_substep(f"Video will be: {submission.title} :thumbsup:") + print_substep(f"Video will be: [cyan]{submission.title}[/cyan] :thumbsup:") try: content["thread_url"] = submission.url diff --git a/setup_program.py b/setup_program.py index 06a8d60..41cf51b 100644 --- a/setup_program.py +++ b/setup_program.py @@ -43,7 +43,7 @@ def setup(): ) console.print( "[bold]If you don't have these, please follow the instructions in the README.md file to " - + "set them up.\n If you do have these, type yes to continue. If you don't, go ahead and " + + "set them up.\nIf you do have these, type yes to continue. If you don't, go ahead and " + "grab those quickly and come back.[/bold]" ) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 136deda..18fe5c5 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -19,9 +19,8 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme): # ! Make sure the reddit screenshots folder exists Path("assets/png").mkdir(parents=True, exist_ok=True) + print_substep("Launching Headless Browser...") with sync_playwright() as browser_: - print_substep("Launching Headless Browser...") - browser = browser_.chromium.launch() context = browser.new_context() From 8f1e2f18c79a137791861f954d68bcdd1483d7b8 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 21:54:01 +0800 Subject: [PATCH 53/83] since the setup is happening incredibly fast, loading bars or spinners are deemed unnecessary --- setup_program.py | 53 +++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/setup_program.py b/setup_program.py index 41cf51b..a5bdd74 100644 --- a/setup_program.py +++ b/setup_program.py @@ -14,15 +14,13 @@ console = Console() def setup(): if exists(".setup-done-before"): console.log( - "[bold red]Setup was already done before! Please" - + " make sure you have to run this script again.[/bold red]" + "[bold red]Setup was already done before! Please make" + + " sure you have to run this script again.[/bold red]" ) # to run the setup script again, but in better and more convenient manner. if input("\033[1mRun the script again? [y/N] > \033[0m") not in ["y", "Y"]: raise SystemExit() - - print_step("Setup Assistant") print_markdown( "### You're in the setup wizard. Ensure you're supposed to be here, " + "then type yes to continue. If you're not sure, type no to quit." @@ -30,7 +28,7 @@ def setup(): # This Input is used to ensure the user is sure they want to continue. # Again, let them know they are about to erase all other setup data. - console.log( + console.print( "Ensure you have the following ready to enter:\n" + "[bold green]Reddit Client ID\n" + "Reddit Client Secret\n" @@ -40,11 +38,9 @@ def setup(): + "Opacity (range of 0-1, decimals are accepted.)\n" + "Subreddit (without r/ or /r/)\n" + "Theme (light or dark)[/bold green]" - ) - console.print( - "[bold]If you don't have these, please follow the instructions in the README.md file to " - + "set them up.\nIf you do have these, type yes to continue. If you don't, go ahead and " - + "grab those quickly and come back.[/bold]" + + "[bold]If you don't have these, please follow the instructions in the README.md file to" + + " set them up.\nIf you do have these, type y or Y to continue. If you don't, go ahead and" + + " grab those quickly and come back.[/bold]" ) """Begin the setup process.""" @@ -57,26 +53,23 @@ def setup(): opacity = input("Opacity? (range of 0-1) > ") subreddit = input("Subreddit (without r/) > ") theme = input("Theme? (light or dark) > ") - console.log("Attempting to save your credentials...") - with console.status("[bold magenta]Saving credentials...[/bold magenta]"): - # you can also put a while loop here, e.g. while VideoIsBeingMade == True: ... - console.log("Removing old .env file...") - os.remove(".env") - console.log("Creating new .env file...") - with open('.env', 'a', encoding="utf-8") as f: - f.write(f'REDDIT_CLIENT_ID="{cliID}"\n') - f.write(f'REDDIT_CLIENT_SECRET="{cliSec}"\n') - f.write(f'REDDIT_USERNAME="{user}"\n') - f.write(f'REDDIT_PASSWORD="{passw}"\n') - f.write(f'REDDIT_2FA="{twofactor}"\n') - f.write(f'THEME="{theme}"\n') - f.write(f'SUBREDDIT="{subreddit}"\n') - f.write(f'OPACITY="{opacity}"\n') + # you can also put a while loop here, e.g. while VideoIsBeingMade == True: ... + console.log("[bold]Saving details...[/bold]") + os.remove(".env") + with open('.env', 'a', encoding="utf-8") as f: + f.write(f'REDDIT_CLIENT_ID="{cliID}"\n') + f.write(f'REDDIT_CLIENT_SECRET="{cliSec}"\n') + f.write(f'REDDIT_USERNAME="{user}"\n') + f.write(f'REDDIT_PASSWORD="{passw}"\n') + f.write(f'REDDIT_2FA="{twofactor}"\n') + f.write(f'THEME="{theme}"\n') + f.write(f'SUBREDDIT="{subreddit}"\n') + f.write(f'OPACITY="{opacity}"\n') - with open(".setup-done-before", "a", encoding="utf-8") as f: - f.write( - "This file will stop the setup assistant from running again." - ) + with open(".setup-done-before", "a", encoding="utf-8") as f: + f.write( + "This file will stop the setup assistant from running again." + ) - console.log("[bold green]Setup Complete![/bold green]") + console.print("[bold green]Setup Complete![/bold green]") From a8e30105403d601596f654ef57c0feef5f1f9614 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 22:03:16 +0800 Subject: [PATCH 54/83] improved the conditions and allow file as an input --- video_creation/background.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/video_creation/background.py b/video_creation/background.py index 2bfca61..9596613 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -21,10 +21,7 @@ def download_background(background): Shoutout to: bbswitzer (https://www.youtube.com/watch?v=n_Dv4JMiwK8) """ - print_step( # removed minecraft, since the background can be changed according to user input. - "Downloading the background video." - ) - + print_step("Downloading the background video.") ydl_opts = { "outtmpl": "assets/mp4/background.mp4", "merge_output_format": "mp4", @@ -47,17 +44,20 @@ def download_background(background): elif background is not None: check_link = re.match( "https://www.youtube.com/watch?v*", background.strip() + ) or re.match( + "https://youtu.be/*", background.strip() ) - if check_link and background is not None: + + if check_link: print_substep(f"Downloading video from: {background}", style="bold") ydl.download(background) - elif background is not None and not check_link: + elif re.match(background[-4], "mp4"): print_substep(f"Using the given video file: {background}", style="bold") os.replace(background.strip(), "assets/mp4/background.mp4") - else: # if the link is not youtube link - raise ValueError + else: # if the link is not youtube link or a file + raise ValueError except ValueError: - print_substep("The given link is not accepted!", style="bold red") + print_substep("Invalid input!", style="bold red") except ConnectionError: print_substep("There is a connection error!", style="bold red") except DownloadError: From ad8fa7b63b93e67bedf4064fd680e1debc30488e Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 22:07:53 +0800 Subject: [PATCH 55/83] rearranged the code to avoid further errors --- main.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index f1d8c89..9270177 100644 --- a/main.py +++ b/main.py @@ -38,15 +38,15 @@ def main(subreddit_=None, background=None, filename=None, thread_link_=None): + " reach out to me on Twitter or submit a GitHub issue." ) - configured = True if not os.path.exists(".env"): shutil.copy(".env.template", ".env") console.print( "[bold red] Your .env file is invalid, or was never created. Standby.[/bold red]" ) - console.print("[bold green]Checking environment variables...[/bold green]") + console.print("[bold]Checking environment variables...[/bold]") + configured = True for val in REQUIRED_VALUES: if not os.getenv(val): print_substep( @@ -54,15 +54,15 @@ def main(subreddit_=None, background=None, filename=None, thread_link_=None): ) configured = False - if configured: - try: - float(os.getenv("OPACITY")) - except: - console.print( - f"[bold red]Please ensure that OPACITY is between 0 and 1 in .env file.[/bold red]" - ) - raise SystemExit() + try: + float(os.getenv("OPACITY")) + except: + console.print( + f"[bold red]Please ensure that OPACITY is between 0 and 1 in .env file.[/bold red]" + ) + raise SystemExit() + if configured: console.print("[bold green]Enviroment Variables are set! Continuing...[/bold green]") reddit_object = get_subreddit_threads(subreddit_, thread_link_) From 0e1076549fcb9c6a550558131ea1e5355f2c055e Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 22:08:28 +0800 Subject: [PATCH 56/83] little changes --- setup_program.py | 7 +++---- video_creation/final_video.py | 13 +++++-------- video_creation/voices.py | 2 +- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/setup_program.py b/setup_program.py index a5bdd74..6800c14 100644 --- a/setup_program.py +++ b/setup_program.py @@ -8,10 +8,9 @@ from utils.console import print_markdown from utils.console import print_step -console = Console() - - def setup(): + console = Console() + if exists(".setup-done-before"): console.log( "[bold red]Setup was already done before! Please make" @@ -55,7 +54,7 @@ def setup(): theme = input("Theme? (light or dark) > ") # you can also put a while loop here, e.g. while VideoIsBeingMade == True: ... - console.log("[bold]Saving details...[/bold]") + console.log("Saving credentials...") os.remove(".env") with open('.env', 'a', encoding="utf-8") as f: f.write(f'REDDIT_CLIENT_ID="{cliID}"\n') diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 0ad5fee..eba61b8 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -16,22 +16,19 @@ import reddit.subreddit from utils.console import print_step, print_substep -W, H = 1080, 1920 - - def make_final_video(number_of_clips, file_name): load_dotenv() opacity = os.getenv("OPACITY") print_step("Creating the final video...") - VideoFileClip.reW = lambda clip: clip.resize(width=W) - VideoFileClip.reH = lambda clip: clip.resize(width=H) + VideoFileClip.reW = lambda clip: clip.resize(width=1080) + VideoFileClip.reH = lambda clip: clip.resize(width=1920) background_clip = ( VideoFileClip("assets/mp4/clip.mp4") .without_audio() - .resize(height=H) + .resize(height=1920) .crop(x1=1166.6, y1=0, x2=2246.6, y2=1920) ) @@ -72,7 +69,7 @@ def make_final_video(number_of_clips, file_name): ImageClip(f"assets/png/title.png") .set_duration(audio_clips[0].duration + audio_clips[1].duration) .set_position("center") - .resize(width=W - 100) + .resize(width=1080 - 100) .set_opacity(float(opacity)), ) else: @@ -81,7 +78,7 @@ def make_final_video(number_of_clips, file_name): ImageClip(f"assets/png/title.png") .set_duration(audio_clips[0].duration) .set_position("center") - .resize(width=W - 100) + .resize(width=1080 - 100) .set_opacity(float(opacity)), ) diff --git a/video_creation/voices.py b/video_creation/voices.py index e2eeb68..80372f1 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -26,7 +26,7 @@ def save_text_to_mp3(reddit_obj): try: Path(f"assets/mp3/posttext.mp3").unlink() - except OSError as e: + except OSError: pass if reddit_obj["thread_post"] != "": From 376f30d709127e9858d3c0e0509473da78eedd2c Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 22:34:25 +0800 Subject: [PATCH 57/83] specified exceptions --- main.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 9270177..f3ba98f 100644 --- a/main.py +++ b/main.py @@ -56,7 +56,11 @@ def main(subreddit_=None, background=None, filename=None, thread_link_=None): try: float(os.getenv("OPACITY")) - except: + except ( + ValueError, + FloatingPointError, + TypeError, + ): console.print( f"[bold red]Please ensure that OPACITY is between 0 and 1 in .env file.[/bold red]" ) From a695e74821a85cb883b6b98b0bb2921723916b0d Mon Sep 17 00:00:00 2001 From: iaacornus Date: Mon, 6 Jun 2022 22:39:42 +0800 Subject: [PATCH 58/83] codiga suggestions --- main.py | 2 +- reddit/subreddit.py | 4 ++-- setup_program.py | 2 +- video_creation/final_video.py | 10 +++++----- video_creation/voices.py | 6 +++--- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index f3ba98f..893d0e2 100644 --- a/main.py +++ b/main.py @@ -62,7 +62,7 @@ def main(subreddit_=None, background=None, filename=None, thread_link_=None): TypeError, ): console.print( - f"[bold red]Please ensure that OPACITY is between 0 and 1 in .env file.[/bold red]" + "[bold red]Please ensure that OPACITY is between 0 and 1 in .env file.[/bold red]" ) raise SystemExit() diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 13293df..09e19fa 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -67,7 +67,7 @@ def get_subreddit_threads(subreddit_, thread_link_): # he doesn't insert the "RANDOM_THREAD" variable at all, ask the thread link if thread_link_ is not None: thread_link = thread_link_ - print_step(f"Getting the inserted thread...") + print_step("Getting the inserted thread...") submission = reddit.submission(url=thread_link) else: try: @@ -104,7 +104,7 @@ def get_subreddit_threads(subreddit_, thread_link_): "comment_id": top_level_comment.id, } ) - except AttributeError as e: + except AttributeError: pass print_substep("AskReddit threads retrieved successfully.", style="bold green") diff --git a/setup_program.py b/setup_program.py index 6800c14..d44c317 100644 --- a/setup_program.py +++ b/setup_program.py @@ -42,7 +42,7 @@ def setup(): + " grab those quickly and come back.[/bold]" ) - """Begin the setup process.""" + # Begin the setup process. cliID = input("Client ID > ") cliSec = input("Client Secret > ") diff --git a/video_creation/final_video.py b/video_creation/final_video.py index eba61b8..c00936e 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -40,7 +40,7 @@ def make_final_video(number_of_clips, file_name): TypeError ): print_substep( - f"Please ensure that OPACITY is between 0 and 1 in .env file", style="bold red" + "Please ensure that OPACITY is between 0 and 1 in .env file", style="bold red" ) # Gather all audio clips @@ -50,7 +50,7 @@ def make_final_video(number_of_clips, file_name): audio_clips.insert(0, AudioFileClip("assets/mp3/title.mp3")) try: - audio_clips.insert(1, AudioFileClip(f"assets/mp3/posttext.mp3")) + audio_clips.insert(1, AudioFileClip("assets/mp3/posttext.mp3")) except ( OSError, FileNotFoundError, @@ -63,10 +63,10 @@ def make_final_video(number_of_clips, file_name): # Gather all images image_clips = [] - if os.path.exists(f"assets/mp3/posttext.mp3"): + if os.path.exists("assets/mp3/posttext.mp3"): image_clips.insert( 0, - ImageClip(f"assets/png/title.png") + ImageClip("assets/png/title.png") .set_duration(audio_clips[0].duration + audio_clips[1].duration) .set_position("center") .resize(width=1080 - 100) @@ -75,7 +75,7 @@ def make_final_video(number_of_clips, file_name): else: image_clips.insert( 0, - ImageClip(f"assets/png/title.png") + ImageClip("assets/png/title.png") .set_duration(audio_clips[0].duration) .set_position("center") .resize(width=1080 - 100) diff --git a/video_creation/voices.py b/video_creation/voices.py index 80372f1..0889ac6 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -25,14 +25,14 @@ def save_text_to_mp3(reddit_obj): length += MP3("assets/mp3/title.mp3").info.length try: - Path(f"assets/mp3/posttext.mp3").unlink() + Path("assets/mp3/posttext.mp3").unlink() except OSError: pass 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 + tts.save("assets/mp3/posttext.mp3") + length += MP3("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. From eac2ed7a37427804f619619dfb26b055c2305513 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 13:13:02 +0800 Subject: [PATCH 59/83] from develop branch --- .gitignore | 170 +++++++++++++- .pylintrc | 614 ++++++++++++++++++++++++++++++++++++++++++++++++ CONTRIBUTING.md | 16 +- README.md | 3 +- 4 files changed, 786 insertions(+), 17 deletions(-) create mode 100644 .pylintrc diff --git a/.gitignore b/.gitignore index 7bacc23..c00d66e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,166 @@ -assets/ -reddit/__pycache__/ -utils/__pycache__/ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments .env -reddit-bot-351418-5560ebc49cac.json -video_creation/__pycache__/ -.setup-done-before -__pycache__ +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. .idea/ -.DS_Store + +assets/ out venv +.DS_Store +.setup-done-before diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 0000000..e3fead7 --- /dev/null +++ b/.pylintrc @@ -0,0 +1,614 @@ +[MAIN] + +# Analyse import fallback blocks. This can be used to support both Python 2 and +# 3 compatible code, which means that the block might have code that exists +# only in one or another interpreter, leading to false positives when analysed. +analyse-fallback-blocks=no + +# Load and enable all available extensions. Use --list-extensions to see a list +# all available extensions. +#enable-all-extensions= + +# In error mode, checkers without error messages are disabled and for others, +# only the ERROR messages are displayed, and no reports are done by default. +#errors-only= + +# Always return a 0 (non-error) status code, even if lint errors are found. +# This is primarily useful in continuous integration scripts. +#exit-zero= + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. +extension-pkg-allow-list= + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. (This is an alternative name to extension-pkg-allow-list +# for backward compatibility.) +extension-pkg-whitelist= + +# Return non-zero exit code if any of these messages/categories are detected, +# even if score is above --fail-under value. Syntax same as enable. Messages +# specified are enabled, while categories only check already-enabled messages. +fail-on= + +# Specify a score threshold to be exceeded before program exits with error. +fail-under=10 + +# Interpret the stdin as a python script, whose filename needs to be passed as +# the module_or_package argument. +#from-stdin= + +# Files or directories to be skipped. They should be base names, not paths. +ignore=CVS + +# Add files or directories matching the regex patterns to the ignore-list. The +# regex matches against paths and can be in Posix or Windows format. +ignore-paths= + +# Files or directories matching the regex patterns are skipped. The regex +# matches against base names, not paths. The default value ignores Emacs file +# locks +ignore-patterns=^\.# + +# List of module names for which member attributes should not be checked +# (useful for modules/projects where namespaces are manipulated during runtime +# and thus existing member attributes cannot be deduced by static analysis). It +# supports qualified module names, as well as Unix pattern matching. +ignored-modules= + +# Python code to execute, usually for sys.path manipulation such as +# pygtk.require(). +#init-hook= + +# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the +# number of processors available to use. +jobs=1 + +# Control the amount of potential inferred values when inferring a single +# object. This can help the performance when dealing with large functions or +# complex, nested conditions. +limit-inference-results=100 + +# List of plugins (as comma separated values of python module names) to load, +# usually to register additional checkers. +load-plugins= + +# Pickle collected data for later comparisons. +persistent=yes + +# Minimum Python version to use for version dependent checks. Will default to +# the version used to run pylint. +py-version=3.6 + +# Discover python modules and packages in the file system subtree. +recursive=no + +# When enabled, pylint would attempt to guess common misconfiguration and emit +# user-friendly hints instead of false-positive error messages. +suggestion-mode=yes + +# Allow loading of arbitrary C extensions. Extensions are imported into the +# active Python interpreter and may run arbitrary code. +unsafe-load-any-extension=no + +# In verbose mode, extra non-checker-related info will be displayed. +#verbose= + + +[REPORTS] + +# Python expression which should return a score less than or equal to 10. You +# have access to the variables 'fatal', 'error', 'warning', 'refactor', +# 'convention', and 'info' which contain the number of messages in each +# category, as well as 'statement' which is the total number of statements +# analyzed. This score is used by the global evaluation report (RP0004). +evaluation=max(0, 0 if fatal else 10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10)) + +# Template used to display messages. This is a python new-style format string +# used to format the message information. See doc for all details. +msg-template= + +# Set the output format. Available formats are text, parseable, colorized, json +# and msvs (visual studio). You can also give a reporter class, e.g. +# mypackage.mymodule.MyReporterClass. +#output-format= + +# Tells whether to display a full report or only the messages. +reports=no + +# Activate the evaluation score. +score=yes + + +[MESSAGES CONTROL] + +# Only show warnings with the listed confidence levels. Leave empty to show +# all. Valid levels: HIGH, CONTROL_FLOW, INFERENCE, INFERENCE_FAILURE, +# UNDEFINED. +confidence=HIGH, + CONTROL_FLOW, + INFERENCE, + INFERENCE_FAILURE, + UNDEFINED + +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once). You can also use "--disable=all" to +# disable everything first and then re-enable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use "--disable=all --enable=classes +# --disable=W". +disable=raw-checker-failed, + bad-inline-option, + locally-disabled, + file-ignored, + suppressed-message, + useless-suppression, + deprecated-pragma, + use-symbolic-message-instead + attribute-defined-outside-init, + invalid-name, + missing-docstring, + protected-access, + too-few-public-methods, + format, # handled by black + +# Enable the message, report, category or checker with the given id(s). You can +# either give multiple identifier separated by comma (,) or put this option +# multiple time (only on the command line, not in the configuration file where +# it should appear only once). See also the "--disable" option for examples. +enable=c-extension-no-member + + +[BASIC] + +# Naming style matching correct argument names. +argument-naming-style=snake_case + +# Regular expression matching correct argument names. Overrides argument- +# naming-style. If left empty, argument names will be checked with the set +# naming style. +#argument-rgx= + +# Naming style matching correct attribute names. +attr-naming-style=snake_case + +# Regular expression matching correct attribute names. Overrides attr-naming- +# style. If left empty, attribute names will be checked with the set naming +# style. +#attr-rgx= + +# Bad variable names which should always be refused, separated by a comma. +bad-names=foo, + bar, + baz, + toto, + tutu, + tata + +# Bad variable names regexes, separated by a comma. If names match any regex, +# they will always be refused +bad-names-rgxs= + +# Naming style matching correct class attribute names. +class-attribute-naming-style=any + +# Regular expression matching correct class attribute names. Overrides class- +# attribute-naming-style. If left empty, class attribute names will be checked +# with the set naming style. +#class-attribute-rgx= + +# Naming style matching correct class constant names. +class-const-naming-style=UPPER_CASE + +# Regular expression matching correct class constant names. Overrides class- +# const-naming-style. If left empty, class constant names will be checked with +# the set naming style. +#class-const-rgx= + +# Naming style matching correct class names. +class-naming-style=PascalCase + +# Regular expression matching correct class names. Overrides class-naming- +# style. If left empty, class names will be checked with the set naming style. +#class-rgx= + +# Naming style matching correct constant names. +const-naming-style=UPPER_CASE + +# Regular expression matching correct constant names. Overrides const-naming- +# style. If left empty, constant names will be checked with the set naming +# style. +#const-rgx= + +# Minimum line length for functions/classes that require docstrings, shorter +# ones are exempt. +docstring-min-length=-1 + +# Naming style matching correct function names. +function-naming-style=snake_case + +# Regular expression matching correct function names. Overrides function- +# naming-style. If left empty, function names will be checked with the set +# naming style. +#function-rgx= + +# Good variable names which should always be accepted, separated by a comma. +good-names=i, + j, + k, + ex, + Run, + _ + +# Good variable names regexes, separated by a comma. If names match any regex, +# they will always be accepted +good-names-rgxs= + +# Include a hint for the correct naming format with invalid-name. +include-naming-hint=no + +# Naming style matching correct inline iteration names. +inlinevar-naming-style=any + +# Regular expression matching correct inline iteration names. Overrides +# inlinevar-naming-style. If left empty, inline iteration names will be checked +# with the set naming style. +#inlinevar-rgx= + +# Naming style matching correct method names. +method-naming-style=snake_case + +# Regular expression matching correct method names. Overrides method-naming- +# style. If left empty, method names will be checked with the set naming style. +#method-rgx= + +# Naming style matching correct module names. +module-naming-style=snake_case + +# Regular expression matching correct module names. Overrides module-naming- +# style. If left empty, module names will be checked with the set naming style. +#module-rgx= + +# Colon-delimited sets of names that determine each other's naming style when +# the name regexes allow several styles. +name-group= + +# Regular expression which should only match function or class names that do +# not require a docstring. +no-docstring-rgx=^_ + +# List of decorators that produce properties, such as abc.abstractproperty. Add +# to this list to register other decorators that produce valid properties. +# These decorators are taken in consideration only for invalid-name. +property-classes=abc.abstractproperty + +# Regular expression matching correct type variable names. If left empty, type +# variable names will be checked with the set naming style. +#typevar-rgx= + +# Naming style matching correct variable names. +variable-naming-style=snake_case + +# Regular expression matching correct variable names. Overrides variable- +# naming-style. If left empty, variable names will be checked with the set +# naming style. +#variable-rgx= + + +[CLASSES] + +# Warn about protected attribute access inside special methods +check-protected-access-in-special-methods=no + +# List of method names used to declare (i.e. assign) instance attributes. +defining-attr-methods=__init__, + __new__, + setUp, + __post_init__ + +# List of member names, which should be excluded from the protected access +# warning. +exclude-protected=_asdict, + _fields, + _replace, + _source, + _make + +# List of valid names for the first argument in a class method. +valid-classmethod-first-arg=cls + +# List of valid names for the first argument in a metaclass class method. +valid-metaclass-classmethod-first-arg=cls + + +[DESIGN] + +# List of regular expressions of class ancestor names to ignore when counting +# public methods (see R0903) +exclude-too-few-public-methods= + +# List of qualified class names to ignore when counting class parents (see +# R0901) +ignored-parents= + +# Maximum number of arguments for function / method. +max-args=5 + +# Maximum number of attributes for a class (see R0902). +max-attributes=7 + +# Maximum number of boolean expressions in an if statement (see R0916). +max-bool-expr=5 + +# Maximum number of branch for function / method body. +max-branches=12 + +# Maximum number of locals for function / method body. +max-locals=15 + +# Maximum number of parents for a class (see R0901). +max-parents=7 + +# Maximum number of public methods for a class (see R0904). +max-public-methods=20 + +# Maximum number of return / yield for function / method body. +max-returns=6 + +# Maximum number of statements in function / method body. +max-statements=50 + +# Minimum number of public methods for a class (see R0903). +min-public-methods=2 + + +[EXCEPTIONS] + +# Exceptions that will emit a warning when caught. +overgeneral-exceptions=BaseException, + Exception + + +[FORMAT] + +# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. +expected-line-ending-format= + +# Regexp for a line that is allowed to be longer than the limit. +ignore-long-lines=^\s*(# )??$ + +# Number of spaces of indent required inside a hanging or continued line. +indent-after-paren=4 + +# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 +# tab). +indent-string=' ' + +# Maximum number of characters on a single line. +max-line-length=100 + +# Maximum number of lines in a module. +max-module-lines=1000 + +# Allow the body of a class to be on the same line as the declaration if body +# contains single statement. +single-line-class-stmt=no + +# Allow the body of an if to be on the same line as the test if there is no +# else. +single-line-if-stmt=no + + +[IMPORTS] + +# List of modules that can be imported at any level, not just the top level +# one. +allow-any-import-level= + +# Allow wildcard imports from modules that define __all__. +allow-wildcard-with-all=no + +# Deprecated modules which should not be used, separated by a comma. +deprecated-modules= + +# Output a graph (.gv or any supported image format) of external dependencies +# to the given file (report RP0402 must not be disabled). +ext-import-graph= + +# Output a graph (.gv or any supported image format) of all (i.e. internal and +# external) dependencies to the given file (report RP0402 must not be +# disabled). +import-graph= + +# Output a graph (.gv or any supported image format) of internal dependencies +# to the given file (report RP0402 must not be disabled). +int-import-graph= + +# Force import order to recognize a module as part of the standard +# compatibility libraries. +known-standard-library= + +# Force import order to recognize a module as part of a third party library. +known-third-party=enchant + +# Couples of modules and preferred modules, separated by a comma. +preferred-modules= + + +[LOGGING] + +# The type of string formatting that logging methods do. `old` means using % +# formatting, `new` is for `{}` formatting. +logging-format-style=old + +# Logging modules to check that the string format arguments are in logging +# function parameter format. +logging-modules=logging + + +[MISCELLANEOUS] + +# List of note tags to take in consideration, separated by a comma. +notes=FIXME, + XXX, + TODO + +# Regular expression of note tags to take in consideration. +notes-rgx= + + +[REFACTORING] + +# Maximum number of nested blocks for function / method body +max-nested-blocks=5 + +# Complete name of functions that never returns. When checking for +# inconsistent-return-statements if a never returning function is called then +# it will be considered as an explicit return statement and no message will be +# printed. +never-returning-functions=sys.exit,argparse.parse_error + + +[SIMILARITIES] + +# Comments are removed from the similarity computation +ignore-comments=yes + +# Docstrings are removed from the similarity computation +ignore-docstrings=yes + +# Imports are removed from the similarity computation +ignore-imports=yes + +# Signatures are removed from the similarity computation +ignore-signatures=yes + +# Minimum lines number of a similarity. +min-similarity-lines=4 + + +[SPELLING] + +# Limits count of emitted suggestions for spelling mistakes. +max-spelling-suggestions=4 + +# Spelling dictionary name. Available dictionaries: none. To make it work, +# install the 'python-enchant' package. +spelling-dict= + +# List of comma separated words that should be considered directives if they +# appear at the beginning of a comment and should not be checked. +spelling-ignore-comment-directives=fmt: on,fmt: off,noqa:,noqa,nosec,isort:skip,mypy: + +# List of comma separated words that should not be checked. +spelling-ignore-words= + +# A path to a file that contains the private dictionary; one word per line. +spelling-private-dict-file= + +# Tells whether to store unknown words to the private dictionary (see the +# --spelling-private-dict-file option) instead of raising a message. +spelling-store-unknown-words=no + + +[STRING] + +# This flag controls whether inconsistent-quotes generates a warning when the +# character used as a quote delimiter is used inconsistently within a module. +check-quote-consistency=no + +# This flag controls whether the implicit-str-concat should generate a warning +# on implicit string concatenation in sequences defined over several lines. +check-str-concat-over-line-jumps=no + + +[TYPECHECK] + +# List of decorators that produce context managers, such as +# contextlib.contextmanager. Add to this list to register other decorators that +# produce valid context managers. +contextmanager-decorators=contextlib.contextmanager + +# List of members which are set dynamically and missed by pylint inference +# system, and so shouldn't trigger E1101 when accessed. Python regular +# expressions are accepted. +generated-members= + +# Tells whether to warn about missing members when the owner of the attribute +# is inferred to be None. +ignore-none=yes + +# This flag controls whether pylint should warn about no-member and similar +# checks whenever an opaque object is returned when inferring. The inference +# can return multiple potential results while evaluating a Python object, but +# some branches might not be evaluated, which results in partial inference. In +# that case, it might be useful to still emit no-member and other checks for +# the rest of the inferred objects. +ignore-on-opaque-inference=yes + +# List of symbolic message names to ignore for Mixin members. +ignored-checks-for-mixins=no-member, + not-async-context-manager, + not-context-manager, + attribute-defined-outside-init + +# List of class names for which member attributes should not be checked (useful +# for classes with dynamically set attributes). This supports the use of +# qualified names. +ignored-classes=optparse.Values,thread._local,_thread._local,argparse.Namespace + +# Show a hint with possible names when a member name was not found. The aspect +# of finding the hint is based on edit distance. +missing-member-hint=yes + +# The minimum edit distance a name should have in order to be considered a +# similar match for a missing member name. +missing-member-hint-distance=1 + +# The total number of similar names that should be taken in consideration when +# showing a hint for a missing member. +missing-member-max-choices=1 + +# Regex pattern to define which classes are considered mixins. +mixin-class-rgx=.*[Mm]ixin + +# List of decorators that change the signature of a decorated function. +signature-mutators= + + +[VARIABLES] + +# List of additional names supposed to be defined in builtins. Remember that +# you should avoid defining new builtins when possible. +additional-builtins= + +# Tells whether unused global variables should be treated as a violation. +allow-global-unused-variables=yes + +# List of names allowed to shadow builtins +allowed-redefined-builtins= + +# List of strings which can identify a callback function by name. A callback +# name must start or end with one of those strings. +callbacks=cb_, + _cb + +# A regular expression matching the name of dummy variables (i.e. expected to +# not be used). +dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ + +# Argument names that match this expression will be ignored. Default to name +# with leading underscore. +ignored-argument-names=_.*|^ignored_|^unused_ + +# Tells whether we should check for unused import in __init__ files. +init-import=no + +# List of qualified module names which can have objects that can redefine +# builtins. +redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ad3adb0..71a9aa8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -6,8 +6,8 @@ All types of contributions are encouraged and valued. See the [Table of Contents > And if you like the project, but just don't have time to contribute, that's fine. There are other easy ways to support the project and show your appreciation, which we would also be very happy about: > -> - ⭐ Star the project -> - πŸ“£ Tweet about it +> - ⭐ Star the project +> - πŸ“£ Tweet about it > - 🌲 Refer this project in your project's readme ## Table of Contents @@ -38,8 +38,7 @@ Additionally, there is a [Discord Server](https://discord.gg/swqtb7AsNQ) for any ## I Want To Contribute ### Reporting Bugs - -#### Before Submitting a Bug Report +

Before Submitting a Bug Report

A good bug report shouldn't leave others needing to chase you up for more information. Therefore, we ask you to investigate carefully, collect information and describe the issue in detail in your report. Please complete the following steps in advance to help us fix any potential bug as fast as possible. @@ -53,8 +52,8 @@ A good bug report shouldn't leave others needing to chase you up for more inform - Version of the interpreter, compiler, SDK, runtime environment, package manager, depending on what seems relevant. - Your input and the output - Is the issue reproducable? Does it exist in previous versions? - -#### How Do I Submit a Good Bug Report? +
+

How Do I Submit a Good Bug Report?

We use GitHub issues to track bugs and errors. If you run into an issue with the project: @@ -68,12 +67,13 @@ Once it's filed: - The project team will label the issue accordingly. - A team member will try to reproduce the issue with your provided steps. If there are no reproduction steps or no obvious way to reproduce the issue, the team will try to support you as best as they can, but you may not recieve an instant. - If the team discovers that this is an issue it will be marked `bug` or `error`, as well as possibly other tags relating to the nature of the error), and the issue will be left to be [implemented by someone](#your-first-code-contribution). +
### Suggesting Enhancements This section guides you through submitting an enhancement suggestion for Reddit Video Maker Bot, **including completely new features and minor improvements to existing functionality**. Following these guidelines will help maintainers and the community to understand your suggestion and find related suggestions. -#### Before Submitting an Enhancement +

Before Submitting an Enhancement

- Make sure that you are using the latest version. - Read the [documentation](https://luka-hietala.gitbook.io/documentation-for-the-reddit-bot/) carefully and find out if the functionality is already covered, maybe by an individual configuration. @@ -90,6 +90,8 @@ Enhancement suggestions are tracked as [GitHub issues](https://github.com/elebum - You may want to **include screenshots and animated GIFs** which help you demonstrate the steps or point out the part which the suggestion is related to. You can use [this tool](https://www.cockos.com/licecap/) to record GIFs on macOS and Windows, and [this tool](https://github.com/colinkeenan/silentcast) or [this tool](https://github.com/GNOME/byzanz) on Linux. - **Explain why this enhancement would be useful** to most users. You may also want to point out the other projects that solved it better and which could serve as inspiration. +
+ ### Your First Code Contribution #### Your environment diff --git a/README.md b/README.md index 658f6fa..7fd3a63 100644 --- a/README.md +++ b/README.md @@ -32,9 +32,8 @@ These videos on TikTok, YouTube and Instagram get MILLIONS of views across all p ## Installation πŸ‘©β€πŸ’» 1. Clone this repository - 2. Run `pip3 install -r requirements.txt` -3. Run `playwright install` and `playwright install-deps`. +3. Run `python3 -m playwright install` and `python3 -m playwright install-deps`. 4. 4a **Automatic Install**: Run `python3 main.py` and type 'yes' to activate the setup assistant. From 5388d6cfd11bc2df41f1c24f1980a7189c65a279 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 13:13:18 +0800 Subject: [PATCH 60/83] limited the use of f.write() --- setup_program.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/setup_program.py b/setup_program.py index d44c317..67346e7 100644 --- a/setup_program.py +++ b/setup_program.py @@ -56,15 +56,17 @@ def setup(): # you can also put a while loop here, e.g. while VideoIsBeingMade == True: ... console.log("Saving credentials...") os.remove(".env") - with open('.env', 'a', encoding="utf-8") as f: - f.write(f'REDDIT_CLIENT_ID="{cliID}"\n') - f.write(f'REDDIT_CLIENT_SECRET="{cliSec}"\n') - f.write(f'REDDIT_USERNAME="{user}"\n') - f.write(f'REDDIT_PASSWORD="{passw}"\n') - f.write(f'REDDIT_2FA="{twofactor}"\n') - f.write(f'THEME="{theme}"\n') - f.write(f'SUBREDDIT="{subreddit}"\n') - f.write(f'OPACITY="{opacity}"\n') + with open(".env", "a", encoding="utf-8") as f: + f.write( + f'REDDIT_CLIENT_ID="{cliID}"\n' + + f'REDDIT_CLIENT_SECRET="{cliSec}"\n' + + f'REDDIT_USERNAME="{user}"\n' + + f'REDDIT_PASSWORD="{passw}"\n' + + f'REDDIT_2FA="{twofactor}"\n' + + f'THEME="{theme}"\n' + + f'SUBREDDIT="{subreddit}"\n' + + f'OPACITY="{opacity}"\n' + ) with open(".setup-done-before", "a", encoding="utf-8") as f: f.write( From 6f243bc0e4cb0fa27f50af6b06395575ed6f1856 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 13:28:26 +0800 Subject: [PATCH 61/83] refactor and merge with develop branch --- video_creation/final_video.py | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index c00936e..5758146 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -61,36 +61,26 @@ def make_final_video(number_of_clips, file_name): audio_concat = concatenate_audioclips(audio_clips) audio_composite = CompositeAudioClip([audio_concat]) + # Gather all images image_clips = [] - if os.path.exists("assets/mp3/posttext.mp3"): - image_clips.insert( - 0, - ImageClip("assets/png/title.png") - .set_duration(audio_clips[0].duration + audio_clips[1].duration) - .set_position("center") - .resize(width=1080 - 100) - .set_opacity(float(opacity)), - ) - else: - image_clips.insert( - 0, - ImageClip("assets/png/title.png") - .set_duration(audio_clips[0].duration) + for i in range(0, number_of_clips): + image_clips.append( + ImageClip(f"assets/png/comment_{i}.png") + .set_duration(audio_clips[i + 1].duration) .set_position("center") .resize(width=1080 - 100) .set_opacity(float(opacity)), ) + image_concat = concatenate_videoclips(image_clips).set_position(("center", "center")) image_concat.audio = audio_composite final = CompositeVideoClip([background_clip, image_concat]) if file_name is None: - filename = ( - re.sub('[?\"%*:|<>]', '', ("assets/" + reddit.subreddit.submission.title + ".mp4")) + filename = re.sub( + '[?\"%*:|<>]', '', (f"assets/{reddit.subreddit.submission.title}.mp4") ) final.write_videofile(filename, fps=30, audio_codec="aac", audio_bitrate="192k") - for i in range(0, number_of_clips): - pass From df66dc14232efc88a72a81f7ae383a9d6f657f1a Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 13:30:36 +0800 Subject: [PATCH 62/83] removed the shebangs and merged with develop branch --- build.sh | 2 +- run.sh | 2 +- setup_program.py | 2 -- video_creation/voices.py | 5 ++++- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/build.sh b/build.sh index 7d4dfc6..165357f 100755 --- a/build.sh +++ b/build.sh @@ -1 +1 @@ -docker build -t rvmt . \ No newline at end of file +docker build -t rvmt . diff --git a/run.sh b/run.sh index 4dcb69a..1f86c99 100755 --- a/run.sh +++ b/run.sh @@ -1 +1 @@ -docker run -v $(pwd)/out/:/app/assets -v $(pwd)/.env:/app/.env -it rvmt \ No newline at end of file +docker run -v $(pwd)/out/:/app/assets -v $(pwd)/.env:/app/.env -it rvmt diff --git a/setup_program.py b/setup_program.py index 67346e7..924c34e 100644 --- a/setup_program.py +++ b/setup_program.py @@ -1,11 +1,9 @@ -# Imports import os from os.path import exists from rich.console import Console from utils.console import print_markdown -from utils.console import print_step def setup(): diff --git a/video_creation/voices.py b/video_creation/voices.py index 0889ac6..f8b304e 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -3,6 +3,7 @@ from pathlib import Path from gtts import gTTS from mutagen.mp3 import MP3 from rich.progress import track +import re from utils.console import print_step, print_substep @@ -39,7 +40,9 @@ def save_text_to_mp3(reddit_obj): # ! This can be longer, but this is just a good starting point if length > 50: break - tts = gTTS(text=comment["comment_body"], lang="en", slow=False) + comment=comment["comment_body"] + text=re.sub('((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])*', '', comment) + tts = gTTS(text, lang="en", slow=False) tts.save(f"assets/mp3/{idx}.mp3") length += MP3(f"assets/mp3/{idx}.mp3").info.length From 010fa6d89d928feaa68bc2f4841a3ab6fa91f7da Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 19:47:43 +0800 Subject: [PATCH 63/83] removed the style parameter for more dynamic styling --- utils/console.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/console.py b/utils/console.py index 3aafbb3..9488a69 100644 --- a/utils/console.py +++ b/utils/console.py @@ -22,7 +22,7 @@ def print_step(text): console.print(panel) -def print_substep(text, style=""): +def print_substep(text): """Prints a rich info message without the panelling.""" - console.print(text, style=style) + console.print(text) From f6f45a6fd45d6b606ddbf3d819a3381b04e172b0 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 19:49:16 +0800 Subject: [PATCH 64/83] add backstyle, but preserved the dynamic styling via conditions --- utils/console.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/utils/console.py b/utils/console.py index 9488a69..95ba1d7 100644 --- a/utils/console.py +++ b/utils/console.py @@ -22,7 +22,9 @@ def print_step(text): console.print(panel) -def print_substep(text): +def print_substep(text, style_=None): """Prints a rich info message without the panelling.""" + if style_ is not None: + console.print(text, style=style_) console.print(text) From 7235d2075bcb0d3e83155368f7a6dcbf0b960528 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 19:50:17 +0800 Subject: [PATCH 65/83] removed duplicate code block --- main.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/main.py b/main.py index 25569b0..ef96dba 100644 --- a/main.py +++ b/main.py @@ -54,18 +54,6 @@ def main(subreddit_=None, background=None, filename=None, thread_link_=None): ) configured = False - try: - float(os.getenv("OPACITY")) - except ( - ValueError, - FloatingPointError, - TypeError, - ): - console.print( - "[bold red]Please ensure that OPACITY is between 0 and 1 in .env file.[/bold red]" - ) - raise SystemExit() - if configured: console.print("[bold green]Enviroment Variables are set! Continuing...[/bold green]") From 297c614c045556a509e91b8bccb7d7b531907983 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 19:57:40 +0800 Subject: [PATCH 66/83] bug fixes --- setup_program.py | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/setup_program.py b/setup_program.py index 924c34e..1b7d6dd 100644 --- a/setup_program.py +++ b/setup_program.py @@ -1,21 +1,18 @@ import os from os.path import exists -from rich.console import Console - -from utils.console import print_markdown +from utils.console import print_markdown, print_substep def setup(): - console = Console() - if exists(".setup-done-before"): - console.log( - "[bold red]Setup was already done before! Please make" - + " sure you have to run this script again.[/bold red]" + print_substep( + "Setup was already done before! Please make sure you have " + + "to run this script again.", style_="bold red" ) # to run the setup script again, but in better and more convenient manner. - if input("\033[1mRun the script again? [y/N] > \033[0m") not in ["y", "Y"]: + if str(input("\033[1mRun the script again? [y/N] > \033[0m")).strip() not in ["y", "Y"]: + print_substep("Permission denied!", style_="bold red") raise SystemExit() print_markdown( @@ -25,7 +22,7 @@ def setup(): # This Input is used to ensure the user is sure they want to continue. # Again, let them know they are about to erase all other setup data. - console.print( + print_substep( "Ensure you have the following ready to enter:\n" + "[bold green]Reddit Client ID\n" + "Reddit Client Secret\n" @@ -35,9 +32,9 @@ def setup(): + "Opacity (range of 0-1, decimals are accepted.)\n" + "Subreddit (without r/ or /r/)\n" + "Theme (light or dark)[/bold green]" - + "[bold]If you don't have these, please follow the instructions in the README.md file to" - + " set them up.\nIf you do have these, type y or Y to continue. If you don't, go ahead and" - + " grab those quickly and come back.[/bold]" + + "[bold]If you don't have these, please follow the instructions in the README.md " + + "set them up.\nIf you do have these, type y or Y to continue. If you don't, " + + "go ahead and grab those quickly and come back.[/bold]" ) # Begin the setup process. @@ -51,9 +48,9 @@ def setup(): subreddit = input("Subreddit (without r/) > ") theme = input("Theme? (light or dark) > ") - # you can also put a while loop here, e.g. while VideoIsBeingMade == True: ... - console.log("Saving credentials...") - os.remove(".env") + if exists(".env"): + os.remove(".env") + with open(".env", "a", encoding="utf-8") as f: f.write( f'REDDIT_CLIENT_ID="{cliID}"\n' @@ -67,8 +64,6 @@ def setup(): ) with open(".setup-done-before", "a", encoding="utf-8") as f: - f.write( - "This file will stop the setup assistant from running again." - ) + f.write("This file will stop the setup assistant from running again.") - console.print("[bold green]Setup Complete![/bold green]") + print_substep("[bold green]Setup Complete![/bold green]") From 6bc60773263e60bd3628544d28f11dc10ff72ae7 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 20:03:36 +0800 Subject: [PATCH 67/83] repeat 3 times after fail --- cli.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/cli.py b/cli.py index 3b479a0..021eba1 100644 --- a/cli.py +++ b/cli.py @@ -1,4 +1,5 @@ import argparse +from venv import create from main import main from setup_program import setup @@ -57,12 +58,21 @@ def program_options(): try: if args.create: - main( - args.subreddit, - args.background, - args.filename, - args.thread, - ) + trial = 0 + while trial < 3: + create = main( + args.subreddit, + args.background, + args.filename, + args.thread, + ) + if not create: + try_again = input("Something went wrong! Try again? [y/N] > ").strip() + if try_again in ["y", "Y"]: + trial += 1 + continue + + break elif args.setup: setup() else: From 81bb39fecce89ceffa65781e450a3475765391b5 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 20:04:27 +0800 Subject: [PATCH 68/83] removed the unnecessary conditions --- video_creation/background.py | 67 ++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/video_creation/background.py b/video_creation/background.py index 9596613..b5ca442 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -29,42 +29,41 @@ def download_background(background): } background_check = os.path.isfile("assets/mp4/background.mp4") - if background is not None or not background_check: - if background_check and background is not None: - print_substep( - "Background video is already downloaded! Replacing ...", style="bold red" - ) - os.remove("assets/mp4/background.mp4") + if background_check and background is not None: + print_substep( + "Background video is already downloaded! Replacing ...", style="bold red" + ) + os.remove("assets/mp4/background.mp4") - cancel = True - try: - with YoutubeDL(ydl_opts) as ydl: - if background is None: - ydl.download("https://www.youtube.com/watch?v=n_Dv4JMiwK8") - elif background is not None: - check_link = re.match( - "https://www.youtube.com/watch?v*", background.strip() - ) or re.match( - "https://youtu.be/*", background.strip() - ) + cancel = True + try: + with YoutubeDL(ydl_opts) as ydl: + if background is None: + ydl.download("https://www.youtube.com/watch?v=n_Dv4JMiwK8") + elif background is not None: + check_link = re.match( + "https://www.youtube.com/watch?v*", background.strip() + ) or re.match( + "https://youtu.be/*", background.strip() + ) - if check_link: - print_substep(f"Downloading video from: {background}", style="bold") - ydl.download(background) - elif re.match(background[-4], "mp4"): - print_substep(f"Using the given video file: {background}", style="bold") - os.replace(background.strip(), "assets/mp4/background.mp4") - else: # if the link is not youtube link or a file - raise ValueError - except ValueError: - print_substep("Invalid input!", style="bold red") - except ConnectionError: - print_substep("There is a connection error!", style="bold red") - except DownloadError: - print_substep("There is a download error!", style="bold red") - else: - print_substep("Background video downloaded successfully!", style="bold green") - cancel = False + if check_link: + print_substep(f"Downloading video from: {background}", style="bold") + ydl.download(background) + elif re.match(background[-4], "mp4"): + print_substep(f"Using the given video file: {background}", style="bold") + os.replace(background.strip(), "assets/mp4/background.mp4") + else: # if the link is not youtube link or a file + raise ValueError + except ValueError: + print_substep("Invalid input!", style="bold red") + except ConnectionError: + print_substep("There is a connection error!", style="bold red") + except DownloadError: + print_substep("There is a download error!", style="bold red") + else: + print_substep("Background video downloaded successfully!", style="bold green") + cancel = False if cancel: # to prevent further error and processes from happening From cce278ab831c69d492e9b2fa9d0048f9199670ed Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 20:05:32 +0800 Subject: [PATCH 69/83] include spinners or loading bar from console that people would know if the program is stuck or not this would help issues like #358, that they would not be waiting for nothing --- video_creation/screenshot_downloader.py | 61 +++++++++++++------------ 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 18fe5c5..3aca45f 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -3,6 +3,7 @@ from pathlib import Path from playwright.sync_api import sync_playwright, ViewportSize from rich.progress import track +from rich.console import Console from utils.console import print_step, print_substep @@ -14,46 +15,48 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme): reddit_object: The Reddit Object you received in askreddit.py screenshot_num: The number of screenshots you want to download. """ + console = Console() + print_step("Downloading Screenshots of Reddit Posts πŸ“·") # ! Make sure the reddit screenshots folder exists Path("assets/png").mkdir(parents=True, exist_ok=True) - print_substep("Launching Headless Browser...") - with sync_playwright() as browser_: - browser = browser_.chromium.launch() - context = browser.new_context() + with console.status("[bold]Launching Headless Browser ...", spinner="simpleDots"): + with sync_playwright() as browser_: + browser = browser_.chromium.launch() + context = browser.new_context() - if theme.casefold() == "dark": - with open("video_creation/cookies.json", encoding="utf-8") as cookie_file: - cookies = json.load(cookie_file) - context.add_cookies(cookies) + if theme.casefold() == "dark": + with open("video_creation/cookies.json", encoding="utf-8") as cookie_file: + cookies = json.load(cookie_file) + context.add_cookies(cookies) - # Get the thread screenshot - page = context.new_page() - page.goto(reddit_object["thread_url"]) - page.set_viewport_size(ViewportSize(width=1920, height=1080)) - if page.locator('[data-testid="content-gate"]').is_visible(): - # This means the post is NSFW and requires to click the proceed button. + # Get the thread screenshot + page = context.new_page() + page.goto(reddit_object["thread_url"]) + page.set_viewport_size(ViewportSize(width=1920, height=1080)) + if page.locator('[data-testid="content-gate"]').is_visible(): + # This means the post is NSFW and requires to click the proceed button. - print_substep("Post is NSFW. You are spicy...") - page.locator('[data-testid="content-gate"] button').click() + 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 + for idx, comment in track( + enumerate(reddit_object["comments"]), "Downloading screenshots..." + ): + # Stop if we have reached the screenshot_num + if idx >= screenshot_num: + break - if page.locator('[data-testid="content-gate"]').is_visible(): - page.locator('[data-testid="content-gate"] button').click() + if page.locator('[data-testid="content-gate"]').is_visible(): + 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.goto(f'https://reddit.com{comment["comment_url"]}') + page.locator( + f"#t1_{comment['comment_id']}" + ).screenshot(path=f"assets/png/comment_{idx}.png") print_substep("Screenshots downloaded Successfully.", style="bold green") From 11450e4896496be362fefd9211e3ac621ef078ed Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 20:06:11 +0800 Subject: [PATCH 70/83] include returns for evaluation in cli.py --- main.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index ef96dba..c41fc89 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,6 @@ import os import shutil from dotenv import load_dotenv -from rich.console import Console from reddit.subreddit import get_subreddit_threads from video_creation.background import download_background, chop_background_video @@ -20,8 +19,6 @@ def main(subreddit_=None, background=None, filename=None, thread_link_=None): required variables are set. If not, print a warning and launch the setup wizard. """ - - console = Console() load_dotenv() REQUIRED_VALUES = [ @@ -40,11 +37,9 @@ def main(subreddit_=None, background=None, filename=None, thread_link_=None): if not os.path.exists(".env"): shutil.copy(".env.template", ".env") - console.print( - "[bold red] The .env file is invalid. Creating .env file.[/bold red]" - ) + print_substep("The .env file is invalid. Creating .env file.", style_="bold red") - console.print("[bold]Checking environment variables...[/bold]") + print_substep("Checking environment variables ...", style_="bold") configured = True for val in REQUIRED_VALUES: @@ -55,7 +50,7 @@ def main(subreddit_=None, background=None, filename=None, thread_link_=None): configured = False if configured: - console.print("[bold green]Enviroment Variables are set! Continuing...[/bold green]") + print_substep("Enviroment Variables are set! Continuing ...", style_="bold green") reddit_object = get_subreddit_threads(subreddit_, thread_link_) length, number_of_comments = save_text_to_mp3(reddit_object) @@ -67,11 +62,13 @@ def main(subreddit_=None, background=None, filename=None, thread_link_=None): download_background(background) chop_background_video(length) make_final_video(number_of_comments, filename) - else: - console.print( - "[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"]: - setup() + return True + + print_substep( + "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.", style_="bold red" + ) + if input("\033[1mLaunch setup wizard? [y/N] > \033[0m").strip() in ["y", "Y"]: + setup() + + return False From 8994258e821a78b797dded5a949a0e577498239e Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 20:37:02 +0800 Subject: [PATCH 71/83] improved regex pattern --- video_creation/background.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/video_creation/background.py b/video_creation/background.py index b5ca442..192a686 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -42,9 +42,10 @@ def download_background(background): ydl.download("https://www.youtube.com/watch?v=n_Dv4JMiwK8") elif background is not None: check_link = re.match( - "https://www.youtube.com/watch?v*", background.strip() - ) or re.match( - "https://youtu.be/*", background.strip() + "(?:https?:\/\/)?(?:www\.)?youtu(?:\.be\/|be.com\/" + + "\S*(?:watch|embed)(?:(?:(?=\/[-a-zA-Z0-9_]{11,}(?!" + + "\S))\/)|(?:\S*v=|v\/)))([-a-zA-Z0-9_]{11,})" + , background.strip() ) if check_link: From c85d1c77cb9bb14f0d8d6af6cdae943188e8b2a3 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 20:39:45 +0800 Subject: [PATCH 72/83] little changes --- video_creation/final_video.py | 2 +- video_creation/voices.py | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 5758146..3ea3101 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -80,7 +80,7 @@ def make_final_video(number_of_clips, file_name): if file_name is None: filename = re.sub( - '[?\"%*:|<>]', '', (f"assets/{reddit.subreddit.submission.title}.mp4") + "[?\"%*:|<>]", "", (f"assets/{reddit.subreddit.submission.title}.mp4") ) final.write_videofile(filename, fps=30, audio_codec="aac", audio_bitrate="192k") diff --git a/video_creation/voices.py b/video_creation/voices.py index f8b304e..c6df660 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -1,9 +1,9 @@ +import re from pathlib import Path from gtts import gTTS from mutagen.mp3 import MP3 from rich.progress import track -import re from utils.console import print_step, print_substep @@ -40,8 +40,13 @@ def save_text_to_mp3(reddit_obj): # ! This can be longer, but this is just a good starting point if length > 50: break - comment=comment["comment_body"] - text=re.sub('((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])*', '', comment) + comment = comment["comment_body"] + text = re.sub( + "((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+" + + "\.([a-zA-Z]){2,6}([a-zA-Z0-9\.\&\/\?\:@\-_=#])*", + "", + comment + ) tts = gTTS(text, lang="en", slow=False) tts.save(f"assets/mp3/{idx}.mp3") length += MP3(f"assets/mp3/{idx}.mp3").info.length From 1f4c1835acaf221bb1745b35b5e181d75dd3a16e Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 20:47:57 +0800 Subject: [PATCH 73/83] avoid already finished threads from as requested from #225 --- main.py | 5 +++++ reddit/subreddit.py | 55 ++++++++++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/main.py b/main.py index c41fc89..7f55595 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ import shutil from dotenv import load_dotenv +import reddit.subreddit from reddit.subreddit import get_subreddit_threads from video_creation.background import download_background, chop_background_video from video_creation.voices import save_text_to_mp3 @@ -62,6 +63,10 @@ def main(subreddit_=None, background=None, filename=None, thread_link_=None): download_background(background) chop_background_video(length) make_final_video(number_of_comments, filename) + + with open("created_videos", "a", encoding="utf-8") as video_lists: + video_lists.write(reddit.subreddit.submission.title) + return True print_substep( diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 09e19fa..4813af6 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -65,27 +65,40 @@ def get_subreddit_threads(subreddit_, thread_link_): # 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 thread_link_ is not None: - thread_link = thread_link_ - print_step("Getting the inserted thread...") - submission = reddit.submission(url=thread_link) - else: - try: - if subreddit_ is None: - raise ValueError - - subreddit = reddit.subreddit(subreddit_) - except ValueError: - if os.getenv("SUBREDDIT"): - subreddit = reddit.subreddit( - re.sub(r"r\/", "", os.getenv("SUBREDDIT").strip()) - ) - else: - subreddit = reddit.subreddit("askreddit") - print_substep("Subreddit not defined. Using AskReddit.") - - threads = subreddit.hot(limit=25) - submission = list(threads)[random.randrange(0, 25)] + while True: + with open("created_videos", "r", encoding="utf-8") as reference: + videos = list(reference.readlines()) + + if thread_link_ is not None: + thread_link = thread_link_ + print_step("Getting the inserted thread...") + submission = reddit.submission(url=thread_link) + else: + try: + if subreddit_ is None: + raise ValueError + + subreddit = reddit.subreddit(subreddit_) + except ValueError: + if os.getenv("SUBREDDIT"): + subreddit = reddit.subreddit( + re.sub(r"r\/", "", os.getenv("SUBREDDIT").strip()) + ) + else: + subreddit = reddit.subreddit("askreddit") + print_substep("Subreddit not defined. Using AskReddit.") + + threads = subreddit.hot(limit=25) + submission = list(threads)[random.randrange(0, 25)] + + if submission.title in videos: + print_substep( + "[bold]There is already a video for thread: [cyan]" + + f"{submission.title}[/cyan]. Finding another one.[/bold]" + ) + continue + + break print_substep(f"Video will be: [cyan]{submission.title}[/cyan] :thumbsup:") From 556e6e4a3f8a398c32623363f6917e2abf235be4 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Tue, 7 Jun 2022 20:50:29 +0800 Subject: [PATCH 74/83] exception handling to avoid errors --- reddit/subreddit.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 4813af6..c3042ed 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -66,9 +66,6 @@ def get_subreddit_threads(subreddit_, thread_link_): # 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 while True: - with open("created_videos", "r", encoding="utf-8") as reference: - videos = list(reference.readlines()) - if thread_link_ is not None: thread_link = thread_link_ print_step("Getting the inserted thread...") @@ -91,14 +88,18 @@ def get_subreddit_threads(subreddit_, thread_link_): threads = subreddit.hot(limit=25) submission = list(threads)[random.randrange(0, 25)] - if submission.title in videos: - print_substep( - "[bold]There is already a video for thread: [cyan]" - + f"{submission.title}[/cyan]. Finding another one.[/bold]" - ) - continue + try: + with open("created_videos", "r", encoding="utf-8") as reference: + videos = list(reference.readlines()) - break + if submission.title in videos: + print_substep( + "[bold]There is already a video for thread: [cyan]" + + f"{submission.title}[/cyan]. Finding another one.[/bold]" + ) + continue + except FileNotFoundError: + break print_substep(f"Video will be: [cyan]{submission.title}[/cyan] :thumbsup:") From 9010d14a129a045136ed946dc09fc3537ee28c94 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Wed, 8 Jun 2022 00:05:38 +0800 Subject: [PATCH 75/83] include / in regex pattern to avoid confusing ffmpeg --- video_creation/final_video.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 3ea3101..2d23bc8 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -80,7 +80,7 @@ def make_final_video(number_of_clips, file_name): if file_name is None: filename = re.sub( - "[?\"%*:|<>]", "", (f"assets/{reddit.subreddit.submission.title}.mp4") + "[?\"%*:|<>]/", "", (f"assets/{reddit.subreddit.submission.title}.mp4") ) final.write_videofile(filename, fps=30, audio_codec="aac", audio_bitrate="192k") From e1ecf4fd7a7fdd95344d1651475b1e99c5e3eaba Mon Sep 17 00:00:00 2001 From: iaacornus Date: Wed, 8 Jun 2022 00:10:24 +0800 Subject: [PATCH 76/83] allow users to specify how many of the comments should be included --- cli.py | 7 +++++++ main.py | 14 ++++++++++++-- reddit/subreddit.py | 7 ++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/cli.py b/cli.py index 021eba1..0faba9e 100644 --- a/cli.py +++ b/cli.py @@ -47,6 +47,12 @@ def program_options(): help="Use the given thread link instead of randomized.", action="store" ) + parser.add_argument( + "-n", + "--number", + help="Number of comments to include.", + action="store" + ) parser.add_argument( "--setup", "--setup", @@ -65,6 +71,7 @@ def program_options(): args.background, args.filename, args.thread, + args.number, ) if not create: try_again = input("Something went wrong! Try again? [y/N] > ").strip() diff --git a/main.py b/main.py index 7f55595..d470aa1 100644 --- a/main.py +++ b/main.py @@ -13,7 +13,13 @@ from utils.console import print_markdown, print_substep from setup_program import setup -def main(subreddit_=None, background=None, filename=None, thread_link_=None): +def main( + subreddit_=None, + background=None, + filename=None, + thread_link_=None, + number_of_comments=None + ): """ Load .env file if exists. If it doesnt exist, print a warning and launch the setup wizard. If there is a .env file, check if the @@ -53,7 +59,11 @@ def main(subreddit_=None, background=None, filename=None, thread_link_=None): if configured: print_substep("Enviroment Variables are set! Continuing ...", style_="bold green") - reddit_object = get_subreddit_threads(subreddit_, thread_link_) + reddit_object = get_subreddit_threads( + subreddit_, + thread_link_, + number_of_comments + ) length, number_of_comments = save_text_to_mp3(reddit_object) download_screenshots_of_reddit_posts( reddit_object, diff --git a/reddit/subreddit.py b/reddit/subreddit.py index c3042ed..0eb953b 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -15,7 +15,7 @@ from rich.console import Console from utils.console import print_step, print_substep -def get_subreddit_threads(subreddit_, thread_link_): +def get_subreddit_threads(subreddit_, thread_link_, number_of_comments): """ Takes subreddit_ as parameter which defaults to None, but in this case since it is None, it would raise ValueError, thus defaulting @@ -109,7 +109,12 @@ def get_subreddit_threads(subreddit_, thread_link_): content["thread_post"] = submission.selftext content["comments"] = [] + comment_count = 0 for top_level_comment in submission.comments: + if number_of_comments is not None: + if comment_count > number_of_comments: + break + if not top_level_comment.stickied: content["comments"].append( { From 016349ea210aa387ee84cae6dc5e004acdd55114 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Wed, 8 Jun 2022 00:17:51 +0800 Subject: [PATCH 77/83] updated the information about app use, and little fixes --- README.md | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7fd3a63..b0556bd 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,7 @@ All done WITHOUT video editing or asset compiling. Just pure ✨programming magi Created by Lewis Menelaws & [TMRRW](https://tmrrwinc.ca) -[ - - - - -](https://tmrrwinc.ca) +[ ](https://tmrrwinc.ca) ## Motivation πŸ€” @@ -34,12 +29,12 @@ These videos on TikTok, YouTube and Instagram get MILLIONS of views across all p 1. Clone this repository 2. Run `pip3 install -r requirements.txt` 3. Run `python3 -m playwright install` and `python3 -m playwright install-deps`. -4. - 4a **Automatic Install**: Run `python3 main.py` and type 'yes' to activate the setup assistant. +4. + 4a **Install 1**: Run `python3 -c`, this would activate the setup assistant if `.env` is not yet configure, or run `python3 cli.py --setup`, this command would also work for reconfiguration or re-setup. 4b **Manual Install**: Rename `.env.template` to `.env` and replace all values with the appropriate fields. To get Reddit keys (**required**), visit [the Reddit Apps page.](https://www.reddit.com/prefs/apps) TL;DR set up an app that is a "script". Copy your keys into the `.env` file, along with whether your account uses two-factor authentication. -5. Run `python3 main.py` (unless you chose automatic install, then the installer will automatically run main.py) +5. Run `python3 cli.py [OPTIONS (arguments)]` to do the task, do `python3 cli.py -h` for help. 7. Enjoy 😎 From f75b59d5a2634770f9740ac911e287bc86c8c62c Mon Sep 17 00:00:00 2001 From: iaacornus Date: Wed, 8 Jun 2022 00:21:03 +0800 Subject: [PATCH 78/83] use regex to replace r/, that it would not cause errors --- reddit/subreddit.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 0eb953b..f7bfecf 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -75,7 +75,9 @@ def get_subreddit_threads(subreddit_, thread_link_, number_of_comments): if subreddit_ is None: raise ValueError - subreddit = reddit.subreddit(subreddit_) + subreddit = reddit.subreddit( + re.sub(r"r\/", "", subreddit_.strip()) + ) except ValueError: if os.getenv("SUBREDDIT"): subreddit = reddit.subreddit( From 4221444b2cbc95646faf07284bf25e566e589e15 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Wed, 8 Jun 2022 00:23:23 +0800 Subject: [PATCH 79/83] little changes --- cli.py | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/cli.py b/cli.py index 0faba9e..b6cc850 100644 --- a/cli.py +++ b/cli.py @@ -1,5 +1,4 @@ import argparse -from venv import create from main import main from setup_program import setup @@ -9,7 +8,7 @@ from utils.console import print_substep def program_options(): description = """\ - DESCRIPTION HERE. + Create Reddit Videos with one command. """ parser = argparse.ArgumentParser( @@ -18,45 +17,38 @@ def program_options(): description=description ) parser.add_argument( - "-c", - "--create", - help="Create a video.", + "-c", "--create", + help="Create a video (uses the defaults).", action="store_true" ) parser.add_argument( # only accepts the name of subreddit, not links. - "-s", - "--subreddit", - help="Use another sub-reddit.", + "-s", "--subreddit", + help="Specify a subreddit.", action="store" ) parser.add_argument( - "-b", - "--background", - help="Use another video background for video (accepts link).", + "-b", "--background", + help="Specify a video background for video (accepts link and file).", action="store" ) parser.add_argument( - "-f", - "--filename", - help="Set a filename for the video.", + "-f", "--filename", + help="Specify a filename for the video.", action="store" ) parser.add_argument( - "-t", - "--thread", - help="Use the given thread link instead of randomized.", + "-t", "--thread", + help="Use the given thread link instead of random.", action="store" ) parser.add_argument( - "-n", - "--number", - help="Number of comments to include.", + "-n", "--number", + help="Specify number of comments to include in the video.", action="store" ) parser.add_argument( - "--setup", - "--setup", - help="Setup the program.", + "--setup", "--setup", + help="(Re)setup the program.", action="store_true" ) From 5d597ca9e8fa7b9f0142294b5fb84c8d95eaa7b0 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Wed, 8 Jun 2022 00:27:23 +0800 Subject: [PATCH 80/83] bug fixes --- cli.py | 4 ++-- main.py | 2 +- reddit/subreddit.py | 2 +- video_creation/background.py | 16 ++++++++-------- video_creation/final_video.py | 4 ++-- video_creation/screenshot_downloader.py | 2 +- video_creation/voices.py | 2 +- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/cli.py b/cli.py index b6cc850..1851243 100644 --- a/cli.py +++ b/cli.py @@ -75,10 +75,10 @@ def program_options(): elif args.setup: setup() else: - print_substep("Error occured!", style="bold red") + print_substep("Error occured!", style_="bold red") raise SystemExit() except KeyboardInterrupt: - print_substep("\nOperation Aborted!", style="bold red") + print_substep("\nOperation Aborted!", style_="bold red") if __name__ == "__main__": diff --git a/main.py b/main.py index d470aa1..6937cf8 100644 --- a/main.py +++ b/main.py @@ -52,7 +52,7 @@ def main( 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" + f"Please set the variable \"{val}\" in your .env file.", style_="bold red" ) configured = False diff --git a/reddit/subreddit.py b/reddit/subreddit.py index f7bfecf..2bd1f8a 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -128,6 +128,6 @@ def get_subreddit_threads(subreddit_, thread_link_, number_of_comments): except AttributeError: pass - print_substep("AskReddit threads retrieved successfully.", style="bold green") + print_substep("AskReddit threads retrieved successfully.", style_="bold green") return content diff --git a/video_creation/background.py b/video_creation/background.py index 192a686..d328f82 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -31,7 +31,7 @@ def download_background(background): background_check = os.path.isfile("assets/mp4/background.mp4") if background_check and background is not None: print_substep( - "Background video is already downloaded! Replacing ...", style="bold red" + "Background video is already downloaded! Replacing ...", style_="bold red" ) os.remove("assets/mp4/background.mp4") @@ -49,21 +49,21 @@ def download_background(background): ) if check_link: - print_substep(f"Downloading video from: {background}", style="bold") + print_substep(f"Downloading video from: {background}", style_="bold") ydl.download(background) elif re.match(background[-4], "mp4"): - print_substep(f"Using the given video file: {background}", style="bold") + print_substep(f"Using the given video file: {background}", style_="bold") os.replace(background.strip(), "assets/mp4/background.mp4") else: # if the link is not youtube link or a file raise ValueError except ValueError: - print_substep("Invalid input!", style="bold red") + print_substep("Invalid input!", style_="bold red") except ConnectionError: - print_substep("There is a connection error!", style="bold red") + print_substep("There is a connection error!", style_="bold red") except DownloadError: - print_substep("There is a download error!", style="bold red") + print_substep("There is a download error!", style_="bold red") else: - print_substep("Background video downloaded successfully!", style="bold green") + print_substep("Background video downloaded successfully!", style_="bold green") cancel = False if cancel: @@ -82,4 +82,4 @@ def chop_background_video(video_length): end_time, targetname="assets/mp4/clip.mp4", ) - print_substep("Background video chopped successfully!", style="bold green") + print_substep("Background video chopped successfully!", style_="bold green") diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 2d23bc8..585e6bf 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -40,7 +40,7 @@ def make_final_video(number_of_clips, file_name): TypeError ): print_substep( - "Please ensure that OPACITY is between 0 and 1 in .env file", style="bold red" + "Please ensure that OPACITY is between 0 and 1 in .env file", style_="bold red" ) # Gather all audio clips @@ -55,7 +55,7 @@ def make_final_video(number_of_clips, file_name): OSError, FileNotFoundError, ): - print_substep("An error occured! Aborting.", style="bold red") + print_substep("An error occured! Aborting.", style_="bold red") raise SystemExit() else: audio_concat = concatenate_audioclips(audio_clips) diff --git a/video_creation/screenshot_downloader.py b/video_creation/screenshot_downloader.py index 3aca45f..eb1bbba 100644 --- a/video_creation/screenshot_downloader.py +++ b/video_creation/screenshot_downloader.py @@ -59,4 +59,4 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme): 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") diff --git a/video_creation/voices.py b/video_creation/voices.py index c6df660..c22e9c5 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -51,6 +51,6 @@ def save_text_to_mp3(reddit_obj): tts.save(f"assets/mp3/{idx}.mp3") length += MP3(f"assets/mp3/{idx}.mp3").info.length - print_substep("Saved Text to MP3 files successfully.", style="bold green") + print_substep("Saved Text to MP3 files successfully.", style_="bold green") # ! Return the index so we know how many screenshots of comments we need to make. return length, idx From ed448130f1c5e708f4f2b84537fc24445b100a4a Mon Sep 17 00:00:00 2001 From: iaacornus Date: Wed, 8 Jun 2022 12:07:35 +0800 Subject: [PATCH 81/83] bug fixes and improvements --- cli.py | 4 +--- reddit/subreddit.py | 7 ++----- video_creation/background.py | 6 +++--- video_creation/final_video.py | 1 - 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/cli.py b/cli.py index 1851243..893e3c2 100644 --- a/cli.py +++ b/cli.py @@ -56,8 +56,7 @@ def program_options(): try: if args.create: - trial = 0 - while trial < 3: + while True: create = main( args.subreddit, args.background, @@ -68,7 +67,6 @@ def program_options(): if not create: try_again = input("Something went wrong! Try again? [y/N] > ").strip() if try_again in ["y", "Y"]: - trial += 1 continue break diff --git a/reddit/subreddit.py b/reddit/subreddit.py index 2ee9235..aac0b90 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -10,7 +10,6 @@ from prawcore.exceptions import ( BadRequest ) from dotenv import load_dotenv -from rich.console import Console from utils.console import print_step, print_substep @@ -24,8 +23,6 @@ def get_subreddit_threads(subreddit_, thread_link_, number_of_comments): Returns a list of threads from the AskReddit subreddit. """ - console = Console() - global submission load_dotenv() @@ -54,11 +51,11 @@ def get_subreddit_threads(subreddit_, thread_link_, number_of_comments): RequestException, BadRequest ): - console.print( + print_substep( "[bold red]There is something wrong with the .env file, kindly check:[/bold red]\n" + "1. ClientID\n" + "2. ClientSecret\n" - + "3. If these variables are fine, kindly check other variables." + + "3. If these variables are fine, kindly check other variables.\n" + "4. Check if the type of Reddit app created is script (personal use script)." ) diff --git a/video_creation/background.py b/video_creation/background.py index d328f82..74f1062 100644 --- a/video_creation/background.py +++ b/video_creation/background.py @@ -66,9 +66,9 @@ def download_background(background): print_substep("Background video downloaded successfully!", style_="bold green") cancel = False - if cancel: - # to prevent further error and processes from happening - raise SystemExit() + if cancel: + # to prevent further error and processes from happening + raise SystemExit() def chop_background_video(video_length): diff --git a/video_creation/final_video.py b/video_creation/final_video.py index 585e6bf..3a738ae 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -18,7 +18,6 @@ from utils.console import print_step, print_substep def make_final_video(number_of_clips, file_name): load_dotenv() - opacity = os.getenv("OPACITY") print_step("Creating the final video...") From 6d128a79fbcacd1e633bf312b9a48e80c2dc4655 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Wed, 8 Jun 2022 12:10:08 +0800 Subject: [PATCH 82/83] used pass instead of break to avoid #380 --- video_creation/voices.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/video_creation/voices.py b/video_creation/voices.py index c22e9c5..7bb0213 100644 --- a/video_creation/voices.py +++ b/video_creation/voices.py @@ -39,7 +39,8 @@ def save_text_to_mp3(reddit_obj): # ! 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: - break + pass + comment = comment["comment_body"] text = re.sub( "((http|https)\:\/\/)?[a-zA-Z0-9\.\/\?\:@\-_=#]+" From c45a2262b390c8996aff0c85433c248f1432e1f4 Mon Sep 17 00:00:00 2001 From: iaacornus Date: Wed, 8 Jun 2022 12:14:12 +0800 Subject: [PATCH 83/83] avoid thread with zero comments as perhaps the reason to #376 --- reddit/subreddit.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index aac0b90..e74a385 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -100,6 +100,11 @@ def get_subreddit_threads(subreddit_, thread_link_, number_of_comments): except FileNotFoundError: break + if len(submission.comments) == 0: + print_substep( + "The thread do not contain any comments. Searching for new one.", style_="bold" + ) + print_substep(f"Video will be: [cyan]{submission.title}[/cyan] :thumbsup:") try: