parent
5b3989611f
commit
51484ca401
@ -1,3 +1,4 @@
|
|||||||
assets/
|
assets/
|
||||||
.env
|
.env
|
||||||
reddit-bot-351418-5560ebc49cac.json
|
reddit-bot-351418-5560ebc49cac.json
|
||||||
|
/.idea
|
||||||
|
@ -0,0 +1,61 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
from utils.console import print_step, print_substep
|
||||||
|
import praw
|
||||||
|
import random
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def ascifi(text):
|
||||||
|
regrex_pattern = re.compile(pattern="["
|
||||||
|
u"\U0001F600-\U0001F64F" # emoticons
|
||||||
|
u"\U0001F300-\U0001F5FF" # symbols & pictographs
|
||||||
|
u"\U0001F680-\U0001F6FF" # transport & map symbols
|
||||||
|
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
|
||||||
|
"]+", flags=re.UNICODE)
|
||||||
|
return regrex_pattern.sub(r'', text)
|
||||||
|
|
||||||
|
|
||||||
|
def get_subreddit_threads():
|
||||||
|
"""
|
||||||
|
Returns a list of threads from the AskReddit subreddit.
|
||||||
|
"""
|
||||||
|
|
||||||
|
print_step("Getting subreddit threads...")
|
||||||
|
|
||||||
|
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"), )
|
||||||
|
"""
|
||||||
|
Ask user for subreddit input
|
||||||
|
"""
|
||||||
|
if not os.getenv("SUBREDDIT"):
|
||||||
|
subreddit = reddit.subreddit(
|
||||||
|
input("What subreddit would you like to pull from? ")) # if the env isnt set, ask user
|
||||||
|
else:
|
||||||
|
print_substep(f"Using subreddit: r/{os.getenv('SUBREDDIT')} from environment variable config")
|
||||||
|
subreddit = reddit.subreddit(
|
||||||
|
os.getenv("SUBREDDIT")) # Allows you to specify in .env. Done for automation purposes.
|
||||||
|
|
||||||
|
threads = subreddit.hot(limit=25)
|
||||||
|
submission = list(threads)[random.randrange(0, 25)]
|
||||||
|
os.environ["VIDEO_TITLE"] = str(ascifi(submission.title))
|
||||||
|
print_substep(f"Video will be: {os.getenv('VIDEO_TITLE')} :thumbsup:")
|
||||||
|
try:
|
||||||
|
|
||||||
|
content["thread_url"] = submission.url
|
||||||
|
content["thread_title"] = submission.title
|
||||||
|
content["comments"] = []
|
||||||
|
|
||||||
|
for top_level_comment in submission.comments:
|
||||||
|
content["comments"].append(
|
||||||
|
{"comment_body": top_level_comment.body, "comment_url": top_level_comment.permalink,
|
||||||
|
"comment_id": top_level_comment.id, })
|
||||||
|
|
||||||
|
except AttributeError as e:
|
||||||
|
pass
|
||||||
|
print_substep("Received subreddit threads Successfully.", style="bold green")
|
||||||
|
return content
|
@ -0,0 +1,8 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def cleanup():
|
||||||
|
files = [f for f in os.listdir('.') if f.endswith('.mp4') and 'temp' in f.lower()]
|
||||||
|
for f in files:
|
||||||
|
os.remove(f)
|
||||||
|
|
@ -1,46 +1,50 @@
|
|||||||
|
import random
|
||||||
|
from os import listdir, environ
|
||||||
|
from pathlib import Path
|
||||||
from random import randrange
|
from random import randrange
|
||||||
from pytube import YouTube
|
from pytube import YouTube
|
||||||
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 rich.progress import track
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
def download_background():
|
def download_background():
|
||||||
"""Downloads the background video from youtube.
|
"""Downloads the backgrounds/s video from youtube.
|
||||||
|
|
||||||
Shoutout to: bbswitzer (https://www.youtube.com/watch?v=n_Dv4JMiwK8)
|
Shoutout to: bbswitzer (https://www.youtube.com/watch?v=n_Dv4JMiwK8)
|
||||||
|
Shoutout to: Orbital Gameplay (https://www.youtube.com/watch?v=2X9QGY__0II)
|
||||||
"""
|
"""
|
||||||
|
Path("assets/backgronds/").mkdir(parents=True, exist_ok=True)
|
||||||
if not Path("assets/mp4/background.mp4").is_file():
|
background_options = [ # uri , filename , credit
|
||||||
print_step(
|
("https://www.youtube.com/watch?v=n_Dv4JMiwK8", "parkour.mp4", 'bbswitzer'),
|
||||||
"We need to download the Minecraft background video. This is fairly large but it's only done once. 😎"
|
# note: make sure the file name doesn't include a - in it
|
||||||
)
|
("https://www.youtube.com/watch?v=2X9QGY__0II", "rocket_league.mp4", 'Orbital Gameplay'), ]
|
||||||
print_substep("Downloading the background video... please be patient 🙏")
|
if listdir('assets/backgrounds/') != len(background_options):
|
||||||
YouTube("https://www.youtube.com/watch?v=n_Dv4JMiwK8").streams.filter(
|
print_step("We need to download the backgrounds videos. they are fairly large but it's only done once. 😎")
|
||||||
res="720p"
|
print_substep("Downloading the backgrounds video... please be patient 🙏 ")
|
||||||
).first().download(
|
|
||||||
"assets/mp4",
|
for uri, filename, credit in track(background_options, description="Downloading..."):
|
||||||
filename="background.mp4",
|
print_step(f"Downloading {filename} from {uri}")
|
||||||
)
|
YouTube(uri).streams.filter(res="720p").first().download("assets/backgrounds",
|
||||||
print_substep("Background video downloaded successfully! 🎉", style="bold green")
|
filename=f"{credit}-{filename}")
|
||||||
|
print_substep("Background videos downloaded successfully! 🎉", style="bold green")
|
||||||
|
|
||||||
|
|
||||||
def chop_background_video(video_length):
|
def chop_background_video(video_length):
|
||||||
print_step("Finding a spot in the background video to chop...✂️")
|
print_step("Finding a spot in the backgrounds video to chop...✂️")
|
||||||
background = VideoFileClip("assets/mp4/background.mp4")
|
choice = random.choice(listdir('assets/backgrounds'))
|
||||||
|
environ["background_credit"] = choice.split('-')[0]
|
||||||
|
|
||||||
|
background = VideoFileClip(f"assets/backgrounds/{choice}")
|
||||||
|
|
||||||
start_time, end_time = get_start_and_end_times(video_length, background.duration)
|
start_time, end_time = get_start_and_end_times(video_length, background.duration)
|
||||||
ffmpeg_extract_subclip(
|
ffmpeg_extract_subclip(f'assets/backgrounds/{choice}', start_time, end_time,
|
||||||
"assets/mp4/background.mp4",
|
targetname="assets/temp/backgrounds.mp4", )
|
||||||
start_time,
|
|
||||||
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")
|
||||||
|
Loading…
Reference in new issue