Skip to content

Commit

Permalink
Merge pull request elebumm#157 from reoccurcat/master
Browse files Browse the repository at this point in the history
overhaul of the functions and QoL improvements
  • Loading branch information
elebumm authored Jun 2, 2022
2 parents 4e49340 + 49c1a89 commit 6fc5d2a
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 31 deletions.
6 changes: 5 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ REDDIT_CLIENT_SECRET=""
REDDIT_USERNAME=""
REDDIT_PASSWORD=""

SUBREDDIT=""
# Valid options are "yes" and "no" for the variable below
REDDIT_2FA=""


SUBREDDIT=""
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
assets/
.env
reddit-bot-351418-5560ebc49cac.json
reddit-bot-351418-5560ebc49cac.json
__pycache__
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ These videos on TikTok, YouTube and Instagram get MILLIONS of views across all p
## Requirements

- Python 3.6+
- Playwright (this should install automatically in installation)
- 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` files.
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`
Expand Down
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from utils.console import print_markdown
import time

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

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."
)

time.sleep(3)
Expand Down
23 changes: 19 additions & 4 deletions reddit/subreddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,34 @@


def get_subreddit_threads():

"""
Returns a list of threads from the AskReddit subreddit.
"""

print_step("Getting subreddit threads...")
load_dotenv()

print_step("Getting AskReddit threads...")

if os.getenv("REDDIT_2FA").lower() == "yes":
print(
"\nEnter your two-factor authentication code from your authenticator app.\n"
)
code = input("> ")
print()
pw = os.getenv("REDDIT_PASSWORD")
passkey = f"{pw}:{code}"
else:
passkey = os.getenv("REDDIT_PASSWORD")

content = {}
load_dotenv()

reddit = praw.Reddit(
client_id=os.getenv("REDDIT_CLIENT_ID"),
client_secret=os.getenv("REDDIT_CLIENT_SECRET"),
user_agent="Accessing AskReddit threads",
username=os.getenv("REDDIT_USERNAME"),
password=os.getenv("REDDIT_PASSWORD"),
password=passkey,
)

if os.getenv("SUBREDDIT"):
Expand Down Expand Up @@ -54,5 +68,6 @@ def get_subreddit_threads():

except AttributeError as e:
pass
print_substep("Received subreddit threads Successfully.", style="bold green")
print_substep("Received AskReddit threads successfully.", style="bold green")

return content
6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ pyee==8.1.0
pyflakes==2.2.0
Pygments==2.12.0
python-dotenv==0.20.0
pytube==12.1.0
regex==2022.4.24

regex==2020.10.15

requests==2.27.1
rich==12.4.4
six==1.16.0
Expand All @@ -40,3 +41,4 @@ update-checker==0.18.0
urllib3==1.26.9
websocket-client==1.3.2
websockets==10.1
yt-dlp==2022.5.18
31 changes: 18 additions & 13 deletions video_creation/background.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from random import randrange
from pytube import YouTube
from pytube.cli import on_progress

from yt_dlp import YoutubeDL

from pathlib import Path
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
from moviepy.editor import VideoFileClip
Expand All @@ -20,20 +21,24 @@ def download_background():

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. 😎"
"We need to download the Minecraft background video. This is fairly large but it's only done once."
)
print_substep("Downloading the background video... please be patient 🙏")
YouTube("https://www.youtube.com/watch?v=n_Dv4JMiwK8", on_progress_callback=on_progress).streams.filter(
res="720p"
).first().download(
"assets/mp4",
filename="background.mp4",
)
print_substep("Background video downloaded successfully! 🎉", style="bold green")

print_substep("Downloading the background video... please be patient.")

ydl_opts = {
"outtmpl": "assets/mp4/background.mp4",
"merge_output_format": "mp4",
}

with YoutubeDL(ydl_opts) as ydl:
ydl.download("https://www.youtube.com/watch?v=n_Dv4JMiwK8")

print_substep("Background video downloaded successfully!", style="bold green")


def chop_background_video(video_length):
print_step("Finding a spot in the background video to chop...✂️")
print_step("Finding a spot in the background video to chop...")
background = VideoFileClip("assets/mp4/background.mp4")

start_time, end_time = get_start_and_end_times(video_length, background.duration)
Expand All @@ -43,4 +48,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")
2 changes: 1 addition & 1 deletion video_creation/final_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


def make_final_video(number_of_clips):
print_step("Creating the final video 🎥")
print_step("Creating the final video...")
VideoFileClip.reW = lambda clip: clip.resize(width=W)
VideoFileClip.reH = lambda clip: clip.resize(width=H)

Expand Down
4 changes: 2 additions & 2 deletions video_creation/screenshot_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num):
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... :fire:")
print_substep("Post is NSFW. You are spicy...")
page.locator('[data-testid="content-gate"] button').click()

page.locator('[data-test-id="post-content"]').screenshot(
Expand All @@ -50,4 +50,4 @@ def download_screenshots_of_reddit_posts(reddit_object, screenshot_num):
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")
Empty file removed video_creation/test.py
Empty file.
8 changes: 4 additions & 4 deletions video_creation/voices.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ 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 🎶")
print_step("Saving Text to MP3 files...")
length = 0

# 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, tld="co.uk")
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

for idx, comment in track(enumerate(reddit_obj["comments"]), "Saving..."):
# ! Stop creating mp3 files if the length is greater than 50 seconds. This can be longer, but this is just a good starting point
if length > 50:
break
tts = gTTS(text=comment["comment_body"], lang="en")
tts = gTTS(text=comment["comment_body"], lang="en", slow=False)
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

0 comments on commit 6fc5d2a

Please sign in to comment.