From 012f30eda59aa2f2e787d790a990211dd58295be Mon Sep 17 00:00:00 2001 From: iaacornus Date: Sat, 4 Jun 2022 18:34:19 +0800 Subject: [PATCH] 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