refactor: reddit/subreddit.py uses toml config

pull/861/head
Callum Leslie 3 years ago
parent b1d27a3b84
commit bc631c57af
No known key found for this signature in database
GPG Key ID: D382C4AFEECEAA90

@ -1,6 +1,6 @@
import re import re
from os import getenv
from utils import settings
import praw import praw
from praw.models import MoreComments from praw.models import MoreComments
@ -17,20 +17,20 @@ def get_subreddit_threads(POST_ID: str):
print_substep("Logging into Reddit.") print_substep("Logging into Reddit.")
content = {} content = {}
if str(getenv("REDDIT_2FA")).casefold() == "yes": if settings.config["reddit"]["creds"]["2fa"] == "true":
print("\nEnter your two-factor authentication code from your authenticator app.\n") print("\nEnter your two-factor authentication code from your authenticator app.\n")
code = input("> ") code = input("> ")
print() print()
pw = getenv("REDDIT_PASSWORD") pw = settings.config["reddit"]["creds"]["password"]
passkey = f"{pw}:{code}" passkey = f"{pw}:{code}"
else: else:
passkey = getenv("REDDIT_PASSWORD") passkey = settings.config["reddit"]["creds"]["password"]
username = getenv("REDDIT_USERNAME") username = settings.config["reddit"]["creds"]["username"]
if username.casefold().startswith("u/"): if username.casefold().startswith("u/"):
username = username[2:] username = username[2:]
reddit = praw.Reddit( reddit = praw.Reddit(
client_id=getenv("REDDIT_CLIENT_ID"), client_id= settings.config["reddit"]["creds"]["client_id"],
client_secret=getenv("REDDIT_CLIENT_SECRET"), client_secret=settings.config["reddit"]["creds"]["client_secret"],
user_agent="Accessing Reddit threads", user_agent="Accessing Reddit threads",
username=username, username=username,
passkey=passkey, passkey=passkey,
@ -39,9 +39,7 @@ def get_subreddit_threads(POST_ID: str):
# Ask user for subreddit input # Ask user for subreddit input
print_step("Getting subreddit threads...") print_step("Getting subreddit threads...")
if not getenv( if not settings.config["reddit"]["thread"]["subreddit"]: # note to user. you can have multiple subreddits via reddit.subreddit("redditdev+learnpython")
"SUBREDDIT"
): # note to user. you can have multiple subreddits via reddit.subreddit("redditdev+learnpython")
try: try:
subreddit = reddit.subreddit( subreddit = reddit.subreddit(
re.sub(r"r\/", "", input("What subreddit would you like to pull from? ")) re.sub(r"r\/", "", input("What subreddit would you like to pull from? "))
@ -51,8 +49,9 @@ def get_subreddit_threads(POST_ID: str):
subreddit = reddit.subreddit("askreddit") subreddit = reddit.subreddit("askreddit")
print_substep("Subreddit not defined. Using AskReddit.") print_substep("Subreddit not defined. Using AskReddit.")
else: else:
print_substep(f"Using subreddit: r/{getenv('SUBREDDIT')} from environment variable config") sub = settings.config["reddit"]["thread"]["subreddit"]
subreddit_choice = getenv("SUBREDDIT") print_substep(f"Using subreddit: r/{sub} from TOML config")
subreddit_choice = sub
if subreddit_choice.casefold().startswith("r/"): # removes the r/ from the input if subreddit_choice.casefold().startswith("r/"): # removes the r/ from the input
subreddit_choice = subreddit_choice[2:] subreddit_choice = subreddit_choice[2:]
subreddit = reddit.subreddit( subreddit = reddit.subreddit(
@ -61,8 +60,8 @@ def get_subreddit_threads(POST_ID: str):
if POST_ID: # would only be called if there are multiple queued posts if POST_ID: # would only be called if there are multiple queued posts
submission = reddit.submission(id=POST_ID) submission = reddit.submission(id=POST_ID)
elif getenv("POST_ID") and len(getenv("POST_ID").split("+")) == 1: elif settings.config["reddit"]["thread"]["post_id"] and len(settings.config["reddit"]["thread"]["post_id"].split("+")) == 1:
submission = reddit.submission(id=getenv("POST_ID")) submission = reddit.submission(id=settings.config["reddit"]["thread"]["post_id"])
else: else:
threads = subreddit.hot(limit=25) threads = subreddit.hot(limit=25)
@ -91,7 +90,7 @@ def get_subreddit_threads(POST_ID: str):
if top_level_comment.body in ["[removed]", "[deleted]"]: if top_level_comment.body in ["[removed]", "[deleted]"]:
continue # # see https://github.com/JasonLovesDoggo/RedditVideoMakerBot/issues/78 continue # # see https://github.com/JasonLovesDoggo/RedditVideoMakerBot/issues/78
if not top_level_comment.stickied: if not top_level_comment.stickied:
if len(top_level_comment.body) <= int(getenv("MAX_COMMENT_LENGTH", "500")): if len(top_level_comment.body) <= int(settings.config["reddit"]["thread"]["max_comment_length"]):
if ( if (
top_level_comment.author is not None top_level_comment.author is not None
): # if errors occur with this change to if not. ): # if errors occur with this change to if not.

Loading…
Cancel
Save