Merge pull request #440 from AsadHumayun/develop

chore: clarify `.env` variables, fix: `env.COMMENT_LENGTH_RANGE` throws errors if it includes a `str` instance
pull/347/merge
Jason 3 years ago committed by GitHub
commit 6b2ee0b8e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,3 +1,4 @@
# This can be found in the email that reddit sent you when you created the app
REDDIT_CLIENT_ID="" REDDIT_CLIENT_ID=""
REDDIT_CLIENT_SECRET="" REDDIT_CLIENT_SECRET=""
@ -5,15 +6,18 @@ REDDIT_USERNAME=""
REDDIT_PASSWORD="" REDDIT_PASSWORD=""
# Valid options are "yes" and "no" # Valid options are "yes" and "no"
# Whether or not 2FA is enabled on the reddit account. Default: "no"
REDDIT_2FA="" REDDIT_2FA=""
#If no, it will ask you a thread link to extract the thread, if yes it will randomize it. # If no, it will ask you a thread link to extract the thread, if yes it will randomize it. Default: "no"
RANDOM_THREAD="yes" RANDOM_THREAD=""
# Valid options are "light" and "dark" # Valid options are "light" and "dark". Default: "light"
THEME="" THEME=""
# Enter a subreddit, e.g. "AskReddit" # Enter a subreddit, e.g. "AskReddit". If this isn't defined, you'll be asked when
# the programme runs. Simply press Enter if you wish to use the default "AskReddit" subreddit when prompted.
SUBREDDIT="" SUBREDDIT=""
# Filters the comments by range of lenght (min and max characters) # Filters the comments by range of lenght (min and max characters)
@ -21,7 +25,7 @@ SUBREDDIT=""
# DO NOT INSERT ANY SPACES BETWEEN THE COMMA AND THE VALUES # DO NOT INSERT ANY SPACES BETWEEN THE COMMA AND THE VALUES
COMMENT_LENGTH_RANGE = "min,max" COMMENT_LENGTH_RANGE = "min,max"
# Range is 0 -> 1 # Range is 0 -> 1. Default: "0.9"
OPACITY="0.9" OPACITY="0.9"
# The absolute path of the folder where you want to save the final video # The absolute path of the folder where you want to save the final video

@ -2,14 +2,13 @@
# Main # Main
from utils.console import print_markdown from utils.console import print_markdown
from rich.console import Console from rich.console import Console
import time
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 dotenv import load_dotenv
import os import time, os
console = Console() console = Console()
@ -22,7 +21,7 @@ REQUIRED_VALUES = [
"OPACITY", "OPACITY",
] ]
#Banner may look bad or wrong in IDE/Text Editor, but looks perfect in CMD, BASH or ZSH # Banner may look bad or wrong in IDE/Text Editor, but looks perfect in CMD, BASH or ZSH
banner = ''' banner = '''
@ -41,20 +40,15 @@ banner = '''
''' '''
print(banner) print(banner)
time.sleep(0.5)
#Banner may look bad or wrong in IDE/Text Editor, but looks perfect in CMD, BASH or ZSH
time.sleep(.5)
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."
) )
""" """
Load .env file if exists. If it doesnt exist, print a warning and launch the setup wizard. Load .env file if exists. If it doesnt exist, print a warning and launch the setup wizard.
If there is a .env file, check if the required variables are set. If not, print a warning and launch the setup wizard. If there is a .env file, check if the required variables are set. If not, print a warning and launch the setup wizard.
""" """
client_id = os.getenv("REDDIT_CLIENT_ID") client_id = os.getenv("REDDIT_CLIENT_ID")
@ -68,13 +62,11 @@ load_dotenv()
console.log("[bold green]Checking environment variables...") console.log("[bold green]Checking environment variables...")
time.sleep(.5) time.sleep(.5)
if not os.path.exists(".env"): if not os.path.exists(".env"):
configured = False configured = False
console.log("[red] Your .env file is invalid, or was never created. Standby.") console.log("[red] Your .env file is invalid, or was never created. Standby.")
for val in REQUIRED_VALUES: for val in REQUIRED_VALUES:
# print(os.getenv(val))
if val not in os.environ or not os.getenv(val): if val not in os.environ or not os.getenv(val):
console.log(f'[bold red]Missing Variable: "{val}"') console.log(f'[bold red]Missing Variable: "{val}"')
configured = False configured = False

@ -2,14 +2,10 @@ from numpy import Infinity
from rich.console import Console from rich.console import Console
from utils.console import print_step, print_substep, print_markdown from utils.console import print_step, print_substep, print_markdown
from dotenv import load_dotenv from dotenv import load_dotenv
import os import os, random, praw, re
import random
import praw
import re
console = Console() console = Console()
def get_subreddit_threads(): def get_subreddit_threads():
global submission global submission
""" """
@ -49,7 +45,7 @@ def get_subreddit_threads():
if os.getenv("SUBREDDIT"): if os.getenv("SUBREDDIT"):
subreddit = reddit.subreddit(re.sub(r"r\/", "", os.getenv("SUBREDDIT"))) subreddit = reddit.subreddit(re.sub(r"r\/", "", os.getenv("SUBREDDIT")))
else: else:
# ! Prompt the user to enter a subreddit # Prompt the user to enter a subreddit
try: try:
subreddit = reddit.subreddit( subreddit = reddit.subreddit(
re.sub( re.sub(
@ -65,9 +61,12 @@ 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)]
upvotes=submission.score print_substep(f"Video will be: {submission.title}")
ratio=submission.upvote_ratio * 100 print("Getting video comments...")
num_comments=submission.num_comments
upvotes = submission.score
ratio = submission.upvote_ratio * 100
num_comments = submission.num_comments
console.log(f"[bold green] Video will be: {submission.title} :thumbsup:") console.log(f"[bold green] Video will be: {submission.title} :thumbsup:")
console.log(f"[bold blue] Thread has " + str(upvotes) + " upvotes") console.log(f"[bold blue] Thread has " + str(upvotes) + " upvotes")
@ -75,7 +74,6 @@ def get_subreddit_threads():
console.log(f"[bold blue] Thread has " + str(num_comments) + " comments") console.log(f"[bold blue] Thread has " + str(num_comments) + " comments")
console.log("Getting video comments...") console.log("Getting video comments...")
try: try:
content["thread_url"] = submission.url content["thread_url"] = submission.url
content["thread_title"] = submission.title content["thread_title"] = submission.title
@ -84,8 +82,13 @@ def get_subreddit_threads():
for top_level_comment in submission.comments: for top_level_comment in submission.comments:
COMMENT_LENGTH_RANGE = [0, Infinity] COMMENT_LENGTH_RANGE = [0, Infinity]
if os.getenv("COMMENT_LENGTH_RANGE"):
# Ensure all values are numeric before attempting to cast
if os.getenv("COMMENT_LENGTH_RANGE") and (False not in list(map(lambda arg: arg.isnumeric(), os.getenv("COMMENT_LENGTH_RANGE").split(",")))):
try:
COMMENT_LENGTH_RANGE = [int(i) for i in os.getenv("COMMENT_LENGTH_RANGE").split(",")] COMMENT_LENGTH_RANGE = [int(i) for i in os.getenv("COMMENT_LENGTH_RANGE").split(",")]
except TypeError:
pass
if COMMENT_LENGTH_RANGE[0] <= len(top_level_comment.body) <= COMMENT_LENGTH_RANGE[1]: if COMMENT_LENGTH_RANGE[0] <= len(top_level_comment.body) <= COMMENT_LENGTH_RANGE[1]:
if not top_level_comment.stickied: if not top_level_comment.stickied:
content["comments"].append( content["comments"].append(
@ -95,9 +98,8 @@ def get_subreddit_threads():
"comment_id": top_level_comment.id, "comment_id": top_level_comment.id,
} }
) )
except AttributeError: except AttributeError:
pass pass
print_substep("Received AskReddit threads successfully.", style="bold green")
print_substep("Received AskReddit threads successfully.", style="bold green")
return content return content

@ -92,7 +92,7 @@ if input("Are you sure you want to continue? > ").strip().casefold() != "yes":
# Once they confirm, move on with the script. # Once they confirm, move on with the script.
console.print("[bold green]Alright! Let's get started!") console.print("[bold green]Alright! Let's get started!")
print("\n") print()
console.log("Ensure you have the following ready to enter:") console.log("Ensure you have the following ready to enter:")
console.log("[bold green]Reddit Client ID") console.log("[bold green]Reddit Client ID")
console.log("[bold green]Reddit Client Secret") console.log("[bold green]Reddit Client Secret")

Loading…
Cancel
Save