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
from os import getenv
from utils import settings
import praw
from praw.models import MoreComments
@ -17,20 +17,20 @@ def get_subreddit_threads(POST_ID: str):
print_substep("Logging into Reddit.")
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")
code = input("> ")
print()
pw = getenv("REDDIT_PASSWORD")
pw = settings.config["reddit"]["creds"]["password"]
passkey = f"{pw}:{code}"
else:
passkey = getenv("REDDIT_PASSWORD")
username = getenv("REDDIT_USERNAME")
passkey = settings.config["reddit"]["creds"]["password"]
username = settings.config["reddit"]["creds"]["username"]
if username.casefold().startswith("u/"):
username = username[2:]
reddit = praw.Reddit(
client_id=getenv("REDDIT_CLIENT_ID"),
client_secret=getenv("REDDIT_CLIENT_SECRET"),
client_id= settings.config["reddit"]["creds"]["client_id"],
client_secret=settings.config["reddit"]["creds"]["client_secret"],
user_agent="Accessing Reddit threads",
username=username,
passkey=passkey,
@ -39,9 +39,7 @@ def get_subreddit_threads(POST_ID: str):
# Ask user for subreddit input
print_step("Getting subreddit threads...")
if not getenv(
"SUBREDDIT"
): # note to user. you can have multiple subreddits via reddit.subreddit("redditdev+learnpython")
if not settings.config["reddit"]["thread"]["subreddit"]: # note to user. you can have multiple subreddits via reddit.subreddit("redditdev+learnpython")
try:
subreddit = reddit.subreddit(
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")
print_substep("Subreddit not defined. Using AskReddit.")
else:
print_substep(f"Using subreddit: r/{getenv('SUBREDDIT')} from environment variable config")
subreddit_choice = getenv("SUBREDDIT")
sub = settings.config["reddit"]["thread"]["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
subreddit_choice = subreddit_choice[2:]
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
submission = reddit.submission(id=POST_ID)
elif getenv("POST_ID") and len(getenv("POST_ID").split("+")) == 1:
submission = reddit.submission(id=getenv("POST_ID"))
elif settings.config["reddit"]["thread"]["post_id"] and len(settings.config["reddit"]["thread"]["post_id"].split("+")) == 1:
submission = reddit.submission(id=settings.config["reddit"]["thread"]["post_id"])
else:
threads = subreddit.hot(limit=25)
@ -91,7 +90,7 @@ def get_subreddit_threads(POST_ID: str):
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(getenv("MAX_COMMENT_LENGTH", "500")):
if len(top_level_comment.body) <= int(settings.config["reddit"]["thread"]["max_comment_length"]):
if (
top_level_comment.author is not None
): # if errors occur with this change to if not.

Loading…
Cancel
Save