Stopped using global variable for submission as well unneeded env vars

pull/729/head
Callum Leslie 3 years ago
parent 82c4353ec9
commit 0fa4a3f4df

@ -60,7 +60,7 @@ ignored-modules=
# Python code to execute, usually for sys.path manipulation such as
# pygtk.require().
#init-hook=
init-hook='import sys; sys.path.append("/")'
# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the
# number of processors available to use.

@ -42,7 +42,7 @@ def main():
download_screenshots_of_reddit_posts(reddit_object, number_of_comments)
download_background()
chop_background_video(length)
make_final_video(number_of_comments, length)
make_final_video(number_of_comments, length, reddit_object)
def run_many(times):

@ -1,5 +1,5 @@
import re
from os import getenv, environ
from os import getenv
import praw
from praw.models import MoreComments
@ -8,19 +8,6 @@ from utils.console import print_step, print_substep
from utils.subreddit import get_subreddit_undone
from utils.videos import check_done
TEXT_WHITELIST = set("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890")
def textify(text):
return "".join(filter(TEXT_WHITELIST.__contains__, text))
def try_env(param, backup):
try:
return environ[param]
except KeyError:
return backup
def get_subreddit_threads():
"""
@ -90,20 +77,20 @@ def get_subreddit_threads():
print_substep(f"Thread has {upvotes} upvotes", style="bold blue")
print_substep(f"Thread has a upvote ratio of {ratio}%", style="bold blue")
print_substep(f"Thread has {num_comments} comments", style="bold blue")
environ["VIDEO_TITLE"] = str(textify(submission.title))
environ["VIDEO_ID"] = str(textify(submission.id))
content["thread_url"] = f"https://reddit.com{submission.permalink}"
content["thread_title"] = submission.title
content["thread_post"] = submission.selftext
content["thread_id"] = submission.id
content["comments"] = []
for top_level_comment in submission.comments:
if isinstance(top_level_comment, MoreComments):
continue
if top_level_comment.body in ["[removed]", "[deleted]"]:
continue # # see https://github.com/JasonLovesDoggo/RedditVideoMakerBot/issues/78
if not top_level_comment.stickied:
if len(top_level_comment.body) <= int(try_env("MAX_COMMENT_LENGTH", 500)):
if len(top_level_comment.body) <= int(getenv("MAX_COMMENT_LENGTH", "500")):
if not top_level_comment.author is None:
content["comments"].append(
{

@ -1,8 +1,9 @@
#!/usr/bin/env python3
import json
import os
import time
import multiprocessing
import re
import os
from os.path import exists
from moviepy.editor import (
@ -17,7 +18,6 @@ from moviepy.editor import (
from moviepy.video.io import ffmpeg_tools
from rich.console import Console
from reddit import subreddit
from utils.cleanup import cleanup
from utils.console import print_step, print_substep
@ -26,7 +26,7 @@ console = Console()
W, H = 1080, 1920
def make_final_video(number_of_clips: int, length: int):
def make_final_video(number_of_clips: int, length: int, reddit_obj: dict[str]):
"""Gathers audio clips, gathers all screenshots, stitches them together and saves the final video to assets/temp
Args:
@ -116,13 +116,14 @@ def make_final_video(number_of_clips: int, length: int):
)
image_concat.audio = audio_composite
final = CompositeVideoClip([background_clip, image_concat])
title = re.sub(r"[^\w\s-]", "", reddit_obj["thread_title"])
idx = re.sub(r"[^\w\s-]", "", reddit_obj["thread_id"])
filename = f"{title}.mp4"
filename = f"{get_video_title()}.mp4"
save_data(filename)
save_data(filename, title, idx)
if not exists("./results"):
print_substep("the results folder didn't exist so I made it")
print_substep("The results folder didn't exist so I made it")
os.mkdir("./results")
final.write_videofile(
@ -144,11 +145,11 @@ def make_final_video(number_of_clips: int, length: int):
print_substep("See result in the results folder!")
print_step(
f"Reddit title: {os.getenv('VIDEO_TITLE')} \n Background Credit: {os.getenv('background_credit')}"
f'Reddit title: { reddit_obj["thread_title"] } \n Background Credit: {os.getenv("background_credit")}'
)
def save_data(filename: str):
def save_data(filename: str, reddit_title: str, reddit_id: str):
"""Saves the videos that have already been generated to a JSON file in video_creation/data/videos.json
Args:
@ -156,28 +157,15 @@ def save_data(filename: str):
"""
with open("./video_creation/data/videos.json", "r+", encoding="utf-8") as raw_vids:
done_vids = json.load(raw_vids)
if str(subreddit.submission.id) in [video["id"] for video in done_vids]:
if reddit_id in [video["id"] for video in done_vids]:
return # video already done but was specified to continue anyway in the .env file
payload = {
"id": str(os.getenv("VIDEO_ID")),
"id": reddit_id,
"time": str(int(time.time())),
"background_credit": str(os.getenv("background_credit")),
"reddit_title": str(os.getenv("VIDEO_TITLE")),
"reddit_title": reddit_title,
"filename": filename,
}
done_vids.append(payload)
raw_vids.seek(0)
json.dump(done_vids, raw_vids, ensure_ascii=False, indent=4)
def get_video_title() -> str:
"""Gets video title from env variable or gives it the name "final_video"
Returns:
str: Video title
"""
title = os.getenv("VIDEO_TITLE") or "final_video"
if len(title) <= 35:
return title
else:
return title[0:30] + "..."

Loading…
Cancel
Save