From 9ba6dab73ce60616d6e895527a033766b947e7fb Mon Sep 17 00:00:00 2001 From: Owen Gaspard Date: Sat, 18 Jun 2022 11:06:11 -0500 Subject: [PATCH] Allow SUBREDDIT to be blank This feature was in an old version but was removed in a recent PR. It allows the env variable SUBREDDIT to be blank and the user input to be blank which will default to AskReddit instead of an error. It also automatically filters out any special characters (in case someone types r/) from the subreddit using re. --- reddit/subreddit.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/reddit/subreddit.py b/reddit/subreddit.py index dc0d8ae..3d68a64 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -7,6 +7,8 @@ from utils.subreddit import get_subreddit_undone from utils.videos import check_done from praw.models import MoreComments +import re + TEXT_WHITELIST = set("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890") @@ -44,12 +46,18 @@ def get_subreddit_threads(): print_step("Getting subreddit threads...") if not getenv( "SUBREDDIT" - ): # note to self. you can have multiple subreddits via reddit.subreddit("redditdev+learnpython") - subreddit = reddit.subreddit( - input("What subreddit would you like to pull from? ") - ) # if the env isnt set, ask user + ): # note to self. 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? ")) + ) + except ValueError: + subreddit = reddit.subreddit("askreddit") + print_substep("Subreddit not defined. Using AskReddit.") else: - print_substep(f"Using subreddit: r/{getenv('SUBREDDIT')} from environment variable config") + print_substep( + f"Using subreddit: r/{getenv('SUBREDDIT')} from environment variable config" + ) subreddit = reddit.subreddit( getenv("SUBREDDIT") ) # Allows you to specify in .env. Done for automation purposes.