code cleaning, and complied with pep8

pull/280/head
iaacornus 3 years ago
parent db1ddce57a
commit 012f30eda5
No known key found for this signature in database
GPG Key ID: 281739AE7252598C

@ -1,13 +1,16 @@
from utils.console import print_markdown import os
import time import time
from dotenv import load_dotenv
from reddit.subreddit import get_subreddit_threads from reddit.subreddit import get_subreddit_threads
from video_creation.background import download_background, chop_background_video from video_creation.background import download_background, chop_background_video
from video_creation.voices import save_text_to_mp3 from video_creation.voices import save_text_to_mp3
from video_creation.screenshot_downloader import download_screenshots_of_reddit_posts from video_creation.screenshot_downloader import download_screenshots_of_reddit_posts
from video_creation.final_video import make_final_video from video_creation.final_video import make_final_video
from dotenv import load_dotenv from utils.console import print_markdown
import os
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." "### 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) time.sleep(3)
reddit_object = get_subreddit_threads() reddit_object = get_subreddit_threads()
load_dotenv() load_dotenv()

@ -1,9 +1,10 @@
from utils.console import print_markdown, print_step, print_substep
import praw
import random import random
from dotenv import load_dotenv
import os import os
import praw
from utils.console import print_markdown, print_step, print_substep
from dotenv import load_dotenv
def get_subreddit_threads(): def get_subreddit_threads():
@ -36,10 +37,6 @@ def get_subreddit_threads():
password=passkey, password=passkey,
) )
if os.getenv("SUBREDDIT"):
subreddit = reddit.subreddit(os.getenv("SUBREDDIT"))
else:
# ! Prompt the user to enter a subreddit
try: try:
subreddit = reddit.subreddit( subreddit = reddit.subreddit(
input("What subreddit would you like to pull from? ") input("What subreddit would you like to pull from? ")
@ -51,8 +48,8 @@ def get_subreddit_threads():
threads = subreddit.hot(limit=25) threads = subreddit.hot(limit=25)
submission = list(threads)[random.randrange(0, 25)] submission = list(threads)[random.randrange(0, 25)]
print_substep(f"Video will be: {submission.title} :thumbsup:") print_substep(f"Video will be: {submission.title} :thumbsup:")
try:
try:
content["thread_url"] = submission.url content["thread_url"] = submission.url
content["thread_title"] = submission.title content["thread_title"] = submission.title
content["comments"] = [] content["comments"] = []
@ -68,6 +65,7 @@ def get_subreddit_threads():
except AttributeError as e: except AttributeError as e:
pass pass
print_substep("Received AskReddit threads successfully.", style="bold green") print_substep("Received AskReddit threads successfully.", style="bold green")
return content return content

@ -4,6 +4,7 @@ from rich.padding import Padding
from rich.panel import Panel from rich.panel import Panel
from rich.text import Text from rich.text import Text
console = Console() console = Console()

@ -1,15 +1,14 @@
from random import randrange from random import randrange
from pathlib import Path
from yt_dlp import YoutubeDL from yt_dlp import YoutubeDL
from pathlib import Path
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
from moviepy.editor import VideoFileClip from moviepy.editor import VideoFileClip
from utils.console import print_step, print_substep from utils.console import print_step, print_substep
def get_start_and_end_times(video_length, length_of_clip): def get_start_and_end_times(video_length, length_of_clip):
random_time = randrange(180, int(length_of_clip) - int(video_length)) random_time = randrange(180, int(length_of_clip) - int(video_length))
return random_time, random_time + video_length return random_time, random_time + video_length

@ -7,6 +7,7 @@ from moviepy.editor import (
CompositeAudioClip, CompositeAudioClip,
CompositeVideoClip, CompositeVideoClip,
) )
from utils.console import print_step from utils.console import print_step
@ -28,7 +29,8 @@ def make_final_video(number_of_clips):
audio_clips = [] audio_clips = []
for i in range(0, number_of_clips): for i in range(0, number_of_clips):
audio_clips.append(AudioFileClip(f"assets/mp3/{i}.mp3")) 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_concat = concatenate_audioclips(audio_clips)
audio_composite = CompositeAudioClip([audio_concat]) audio_composite = CompositeAudioClip([audio_concat])
@ -41,9 +43,10 @@ def make_final_video(number_of_clips):
.set_position("center") .set_position("center")
.resize(width=W - 100), .resize(width=W - 100),
) )
image_clips.insert( image_clips.insert(
0, 0,
ImageClip(f"assets/png/title.png") ImageClip("assets/png/title.png")
.set_duration(audio_clips[0].duration) .set_duration(audio_clips[0].duration)
.set_position("center") .set_position("center")
.resize(width=W - 100), .resize(width=W - 100),

@ -1,8 +1,10 @@
from playwright.sync_api import sync_playwright, ViewportSize import json
from pathlib import Path from pathlib import Path
from playwright.sync_api import sync_playwright, ViewportSize
from rich.progress import track from rich.progress import track
from utils.console import print_step, print_substep from utils.console import print_step, print_substep
import json
def download_screenshots_of_reddit_posts(reddit_object, screenshot_num, theme): 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() context = browser.new_context()
if theme.casefold() == "dark": if theme.casefold() == "dark":
cookie_file = open('video_creation/cookies.json') cookie_file = open("video_creation/cookies.json")
cookies = json.load(cookie_file) cookies = json.load(cookie_file)
context.add_cookies(cookies) 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" path=f"assets/png/comment_{idx}.png"
) )
print_substep("Screenshots downloaded Successfully.", print_substep(
style="bold green") "Screenshots downloaded Successfully.", style="bold green"
)

@ -1,9 +1,11 @@
from gtts import gTTS
from pathlib import Path from pathlib import Path
from gtts import gTTS
from mutagen.mp3 import MP3 from mutagen.mp3 import MP3
from utils.console import print_step, print_substep
from rich.progress import track from rich.progress import track
from utils.console import print_step, print_substep
def save_text_to_mp3(reddit_obj): def save_text_to_mp3(reddit_obj):
"""Saves Text to MP3 files. """Saves Text to MP3 files.
@ -11,15 +13,16 @@ def save_text_to_mp3(reddit_obj):
Args: Args:
reddit_obj : The reddit object you received from the reddit API in the askreddit.py file. 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 length = 0
print_step("Saving Text to MP3 files...")
# Create a folder for the mp3 files. # Create a folder for the mp3 files.
Path("assets/mp3").mkdir(parents=True, exist_ok=True) Path("assets/mp3").mkdir(parents=True, exist_ok=True)
tts = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False) tts = gTTS(text=reddit_obj["thread_title"], lang="en", slow=False)
tts.save(f"assets/mp3/title.mp3") tts.save("assets/mp3/title.mp3")
length += MP3(f"assets/mp3/title.mp3").info.length length += MP3("assets/mp3/title.mp3").info.length
for idx, comment in track(enumerate(reddit_obj["comments"]), "Saving..."): 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 # ! Stop creating mp3 files if the length is greater than 50 seconds. This can be longer, but this is just a good starting point

Loading…
Cancel
Save